-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread_pager.pl
324 lines (256 loc) · 10 KB
/
thread_pager.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/perl
#---------------------------------------------------------------------------------------------------
# Thread Pager
#---------------------------------------------------------------------------------------------------
# This script checks for new posts in a thread by the author (OP/original poster). If a new post
# from the author is found, the thread information is emailed, and a text message is sent.
# Posts made by any other users are ignored.
#
# Requirements:
# - Turn on "Allow less secure apps" in your Google Account preferences to enable sending
# over Gmail SMTP.
# - Modify the values in the "REQUIRED CUSTOMIZATIONS" section below.
#
# Procedure:
# - Run the script using Perl 5 version 28 or higher.
#
# Usage: thread_pager.pl
#
#---------------------------------------------------------------------------------------------------
use strict;
use warnings;
use LWP::UserAgent;
use Web::Query;
use Email::Send::SMTP::Gmail;
#---------------------------------------------------------------------------------------------------
# REQUIRED CUSTOMIZATIONS
#---------------------------------------------------------------------------------------------------
use constant SENDER_USERNAME => '[email protected]';
use constant SENDER_PASSWORD => 'password';
my @recipients = (
'[email protected]', # Email for text message
);
my @urls = ('http://website/forum/');
my $watched_thread = q/thread_name/;
my $watched_author = q/author_username/;
# Use CSS selectors to search elements
my $selector_class = '.class';
my $selector_thread = '[thread]';
my $selector_author = '[author]';
my $selector_poster = '[poster]';
#---------------------------------------------------------------------------------------------------
# Optional Customization
#---------------------------------------------------------------------------------------------------
use constant MAIL_SERVER => 'smtp.gmail.com';
use constant MAIL_LAYER => 'ssl';
use constant MAIL_PORT => '465';
use constant MAIL_DELAY => 5; # Seconds
use constant MAIL_SUBJECT => 'New post detected';
use constant HTML_FILE => 'page.html';
use constant TIMESTAMP_FILE => 'timestamp.txt';
&main();
#---------------------------------------------------------------------------------------------------
# main
#---------------------------------------------------------------------------------------------------
sub main
{
my $body = ''; # Email body
my $fh = undef; # Filehandle
my $sendEmail = 0; # Flag for sending emails
my @thread_array = (); # Contains all thread data
my $datestring = localtime();
print "$datestring\n";
# Avoid different output encodings
binmode STDOUT, ":encoding(UTF-8)";
# Begin loop through all urls
for my $url (@urls)
{
my $ua = new LWP::UserAgent;
my $req = new HTTP::Request GET => $url;
# Request web data
my $res = $ua->request($req);
# Check the response
if ($res->is_success)
{
# Write content to HTML file for debugging purposes
open($fh, ">", HTML_FILE) or die "Unable to open file: ", HTML_FILE, "\n";
print $fh $res->content;
close($fh);
}
else
{
die "Could not get content from $url\n";
}
# Create a new instance of the web query
my $q = Web::Query->new_from_file(HTML_FILE) or
die "Unable to create a new instance of Web::Query\n";
# Get thread title
$q->find($selector_class)->find($selector_thread)
->each(
sub {
my $i = shift;
$thread_array[$i]{title} = $_->text;
}
);
# Get author
$q->find($selector_class)->find($selector_author)
->each(
sub {
my $i = shift;
$thread_array[$i]{author} = $_->text;
}
);
# Get timestamp and last poster
$q->find($selector_class)->find($selector_poster)
->each(
sub {
my $i = shift;
$thread_array[$i]{timestamp} = $_->text;
$_->attr('title') =~ /poster \((.*)\)/;
$thread_array[$i]{poster} = $1;
}
);
# Begin loop through all thread data
for my $i (0 .. $#thread_array)
{
# Display data
printf "%3d: { ", $i;
for my $role (sort keys %{$thread_array[$i]})
{
print "$role=$thread_array[$i]{$role} ";
}
print "}\n";
# Search for the watched thread and author
if (($thread_array[$i]->{title} =~ /$watched_thread/i) &&
($thread_array[$i]->{author} eq $watched_author) &&
($thread_array[$i]->{poster} eq $watched_author))
{
print "\nFound a match! => $thread_array[$i]->{title} by $thread_array[$i]->{author}\n";
# Handle date strings
if ($thread_array[$i]->{timestamp} =~ /\d+\-\w+/)
{
print "Date timestamp found: $thread_array[$i]->{timestamp}\n";
# Only create the date timestamp once
if (&isTimestampNew($thread_array[$i]->{timestamp}) == 1)
{
print "Writing date timestamp to file ", TIMESTAMP_FILE, " ...\n";
# Force a new timestamp
&writeTimestampFile($thread_array[$i]->{timestamp},
$thread_array[$i]->{title},
$thread_array[$i]->{poster});
}
}
# Handle time strings
else
{
# Timestamp is new
if (&isTimestampNew($thread_array[$i]->{timestamp}) == 1)
{
print "Timestamp is NEW\n";
# Build the email body
$body = $body . "Thread: $thread_array[$i]->{title}\n";
$body = $body . "Poster: $thread_array[$i]->{poster}\n";
$body = $body . "Timestamp: $thread_array[$i]->{timestamp}\n\n";
&writeTimestampFile($thread_array[$i]->{timestamp},
$thread_array[$i]->{title},
$thread_array[$i]->{poster});
# Set email flag
$sendEmail = 1;
print "Email body: $body";
}
# Timestamp is old
else
{
print "Timestamp is OLD\n";
}
}
}
} # End loop through all thread data
} # End loop through all urls
# Check if email should be sent
if ($sendEmail == 1)
{
# Begin loop through all email recipients
for my $recipient (@recipients)
{
&send_mail($recipient, MAIL_SUBJECT, $body);
print "\nNotified: $recipient";
sleep(MAIL_DELAY);
} # End loop through all email recipients
print "\n";
}
} # End of main()
#---------------------------------------------------------------------------------------------------
# Subroutine send_mail
#---------------------------------------------------------------------------------------------------
sub send_mail
{
my ($to, $subject, $body) = @_;
my ($mail, $error) = Email::Send::SMTP::Gmail->new(-smtp=>MAIL_SERVER,
-layer=>MAIL_LAYER,
-port=>MAIL_PORT,
-login=>SENDER_USERNAME,
-pass=>SENDER_PASSWORD);
print "session error: $error\n" unless ($mail != -1);
$mail->send(-to=>$to, -subject=>$subject, -body=>$body);
$mail->bye;
} # End of send_mail()
#---------------------------------------------------------------------------------------------------
# Subroutine getSavedTimestamp
#
# Returns timestamp string
#---------------------------------------------------------------------------------------------------
sub getSavedTimestamp
{
my $fh = undef; # Filehandle
my $line = '';
# Check for timestamp file existence
if (-e TIMESTAMP_FILE)
{
open($fh, "<", TIMESTAMP_FILE) or die "Unable to open file: ", TIMESTAMP_FILE, "\n";
# First line is the timestamp
$line = <$fh>;
close($fh);
# Strip leading and trailing whitespace
$line =~ s/^\s+//;
$line =~ s/\s+$//;
}
else
{
print "File not found: ", TIMESTAMP_FILE, "\n";
}
return $line;
} # End of getSavedTimestamp()
#---------------------------------------------------------------------------------------------------
# Subroutine isTimeStampNew
#
# Returns 1 if timestamp is new
# Returns 0 if timestamp is old
#---------------------------------------------------------------------------------------------------
sub isTimestampNew
{
my $timestamp = $_[0];
my $line = &getSavedTimestamp();
# Check timestamp
if ($timestamp eq $line)
{
# Timestamp is old
return 0;
}
# Timestamp must be new
return 1;
} # End of isTimestampNew()
#---------------------------------------------------------------------------------------------------
# Subroutine writeTimestampFile
#---------------------------------------------------------------------------------------------------
sub writeTimestampFile
{
my ($timestamp, $title, $poster) = @_;
my $fh = undef; # Filehandle
open($fh, ">", TIMESTAMP_FILE) or die "Unable to open file: ", TIMESTAMP_FILE, "\n";
print $fh "$timestamp\n";
print $fh "$title\n";
print $fh "$poster\n";
close($fh);
} # End of writeTimestampFile()