r/ScriptSwap • u/ajrisi • Mar 02 '12
[Perl] Script to check for an orangered, and politely notify you
Description:
This script will check for any PMs - if you have one, it will pop up a polite notification. This script requires libnotify-bin to be installed.
Usage:
Place the script somewhere you can access it, and add the following line to your crontab via 'crontab -e'
*/5 * * * * DISPLAY=:0 /path/to/script.pl > /dev/null 2>&1
This will check every 5 minutes.
You also need to have a picture for the popup message - I am using the orangered.png found here. Modify the script to point at the right image path.
Lastly, you will need to modify the script to have your reddit session cookie. You can grab this from your browser fairly easily.
Note: If you run the script outside of a cron job, you can check to make sure it works by looking at the output.
Script:
#!/usr/bin/perl
use strict;
use LWP::UserAgent;
use HTTP::Cookies;
my $browser = LWP::UserAgent->new;
my $cookie_jar = HTTP::Cookies->new( {} );
$cookie_jar->set_cookie(
1, # version
'reddit_session', # key
'--PLACE YOUR REDDIT SESSION KEY HERE. YOU CAN GET THIS VALUE FROM YOUR BROWSERS COOKIE CACHE--', # value
'/', # path
'.reddit.com', # domain
'80', # port
'/', # path_spec
0, # secure
3600, # maxage
0, # discard
);
$browser->cookie_jar( $cookie_jar );
$_= $browser->get('http://www.reddit.com/api/me.json')->decoded_content;
if (m/"has_mail": true/) {
print "You have mail!\n";
system("/usr/bin/notify-send", "--icon=/path/to/your/orangered/picture/orangered.png", "Reddit", 'You have mail!');
exit(1);
} elsif (m/"has_mail": false/) {
print "No mail\n";
exit(0);
} else {
print "Unknown mail status.";
exit(-1);
}
3
u/[deleted] Mar 02 '12
nice!
do you know how long it takes for the session key to time out?
2
u/Alca_Pwn Mar 02 '12
Forbidden
You don't have permission to access /orangered.png on this server.
2
u/GT_Wallace Mar 05 '12
I may be doing this wrong... how do you find the session key in a browser like chromium or luakit?
thanks
2
u/terremoto Mar 02 '12
Seems like it would be less work to just check how much data is in the rss or json feeds.
0
u/[deleted] Mar 02 '12
[deleted] — view deleted comment