Last active
October 29, 2015 00:11
-
-
Save thomascoe/48f322f52290e03313e9 to your computer and use it in GitHub Desktop.
Checks Reddit for new posts to /r/buildapcsales matching a specified tag, sends email when it finds something new. Depends on XML::RSS::Parser and the Linux 'mail' command (tested with mailx)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use IO::File; | |
use Date::Parse; | |
use XML::RSS::Parser; | |
my $tag = "GPU"; | |
my $time_file = "/home/username/pricemonitor_lasttime.txt"; | |
# Email source and destination | |
my $src_addr = "source@example.com"; | |
my $dst_addr = "destination@example.com"; | |
# Download RSS Feed | |
my $url = "https://www.reddit.com/r/buildapcsales/new.rss"; | |
my $xml = qx{curl --silent $url}; | |
die "Couldn't get data from reddit!" unless defined $xml; | |
# Parse the XML | |
my $rp = XML::RSS::Parser->new; | |
my $feed = $rp->parse_string($xml); | |
# Check program last run time | |
open(TIMEFH, "<$time_file"); | |
my $last_time = <TIMEFH>; | |
close (TIMEFH); | |
if (!defined $last_time) { | |
$last_time = 0; | |
} | |
# Keep track of the greatest deal time seen | |
my $greatest_time = $last_time; | |
# Loop through results | |
foreach my $i ($feed->query('//item')) { | |
my $title = $i->query('title')->text_content; | |
my $url = $i->query('link')->text_content; | |
my $date = $i->query('pubDate')->text_content; | |
my $unix = str2time($date); | |
#print $title . " " . $url . "\n"; | |
# Check if title matches tag | |
if ($title =~ /\[.*$tag.*\]/i) { | |
# Check if this is new since last run | |
if ($unix > $last_time) { | |
qx{echo -n '$title $url' | mail -r "$src_addr" $dst_addr}; | |
if ($unix > $greatest_time) { | |
$greatest_time = $unix; | |
} | |
} | |
} | |
} | |
# Set program last run time (write to file) | |
open(TIMEFH, ">$time_file") or die "Couldn't open $time_file for write!"; | |
print TIMEFH "$greatest_time"; | |
close(TIMEFH); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment