Skip to content

Instantly share code, notes, and snippets.

@miyagawa
Created March 2, 2011 18:55
Show Gist options
  • Save miyagawa/851470 to your computer and use it in GitHub Desktop.
Save miyagawa/851470 to your computer and use it in GitHub Desktop.
scrape engadget images and post to lingr
#!/usr/bin/perl
use strict;
use Web::Scraper;
use HTTP::Request::Common;
use LWP::UserAgent;
use Digest::SHA1;
use URI;
our $BotName = "livechatbot";
our $RoomName = "lingr";
our $BotSecret = "SECRET";
my $uri = URI->new("http://www.engadget.com/2011/03/02/live-from-apples-ipad-2-event/?sort=newest");
my $scraper = scraper {
process ".live_update img", "images[]" => '@src';
};
my %seen;
my $first_run = 1;
while (1) {
my $res = $scraper->scrape($uri);
my @images;
for my $image (@{$res->{images}}) {
next unless !$seen{$image}++;
push @images, $image unless $first_run;
}
for my $img (reverse @images) {
post_to_lingr($img);
}
$first_run = 0;
sleep 10;
}
sub post_to_lingr {
my $img = shift;
warn "posting $img";
my $uri = URI->new("http://lingr.com/api/room/say");
$uri->query_form(
room => $RoomName,
bot => $BotName,
text => "$img",
bot_verifier => Digest::SHA1::sha1_hex($BotName . $BotSecret),
);
my $res = LWP::UserAgent->new->get($uri);
warn $res->as_string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment