-01-
12
2009
12
2009
Twitterbot in Perl
Trackbacks
Trackback für spezifische URI dieses Eintrags
Keine Trackbacks
#!/usr/bin/perl
use strict;
use DateTime;
use Net::Twitter::Lite;
use XML::LibXML;
#Get date
my $today = DateTime->today();
die "No working day!\n" if($today->day_of_week()>5);
#Fetch url and load document as DOM tree
my $url = sprintf("http://www.uni-ulm.de/mensaplan/%04d-%02d-%02d.html",$today->year, $today->month,$today->day);
my $doc = XML::LibXML->new()->parse_html_file($url) or die "Error while fetching/parsing document!\n";
#Gather all list entries and extract mealtype and actual meal
my @meals = $doc->findnodes('//div[@class="meal"]');
my @tweets;
foreach (@meals)
{
my ($mealtype, $meal) = ($_->find('./div[@class="mealtype"]'),$_->find('./div[@class="item"]'));
$meal = (length($meal)+length($mealtype) > 138 ? substr($meal,0,136-length($mealtype)).".." : $meal);
push(@tweets, $mealtype.": ".$meal);
}
die "Error while fetching meals!\n" if(length(@tweets) == 0);
#Login to twitter and post entries in reverse order
my $nt = Net::Twitter::Lite->new(username => 'username', password => '...', clientname => "MensaBot",source => "web") or die "Error during twitter login procedure!\n";
$nt->update("="x 40);
foreach (reverse(@tweets))
{
my $result = $nt->update($_);
}
$nt->update("Mensaplan Uni Ulm am ".$today->day.".".$today->month.".".$today->year.": #uni #ulm #uulm #mensa #mensaplan");
exit;
0 10 * * 1-5 /path/to/script.pl
#1 - Florian Seiffert besagt:
09.12.2009 15:47 - (Antwort)
Danke der Beitrag hat sehr geholfen. Jezt twittert die ZBSport die neuen Werke des Hochschulschriftenservers automatisch.
|
|
September '10 | |||||
| Mo | Di | Mi | Do | Fr | Sa | So |
| 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 | |||
© 2002 - 2010 Benjamin Erb