Skip to content

Instantly share code, notes, and snippets.

@sharifulin
Created June 23, 2010 16:14
Show Gist options
  • Select an option

  • Save sharifulin/450143 to your computer and use it in GitHub Desktop.

Select an option

Save sharifulin/450143 to your computer and use it in GitHub Desktop.
Mojo::Client for moskva.fm and piter.fm
#!/usr/bin/perl
use common::sense;
use lib qw(/tk/lib /tk/mojo/lib);
use Mojo::Client;
use Data::Dumper;
die "Usage: $0 moskva|piter stations|onair"
unless @ARGV && $ARGV[0] ~~ ['moskva', 'piter'] && $ARGV[1] ~~ ['stations', 'onair'];
my $type = shift;
my $client = Mojo::Client->new;
given (shift) {
when ('stations') {
my $body = $client->get( "http://m.$type.fm/$_/" )->res->body;
say Dumper my $data = [
map {
state $i;
[ ++$i, m{<a href="([^"]+)"><strong>(.*?)</.*?<span>(\S+ \w+)} ];
# http://img.moskva.fm/img/stations/msk/101_2_85x85.gif
# http://img.moskva.fm/img/stations/spb/107_4_85x85.gif
}
$body =~ m{<p>(\d+.*?)</p>}sg
];
}
when ('onair') {
my $body = $client->get( "http://m.$type.fm/$_/" )->res->body;
my $data = [
map {
state $i;
[ ++$i, m{
<small>(\d+:\d+)
.*?
<a \ href="([^"]+)">(.*?)</a>
\
(\S+ \ \w+)
.*?
<a \ href="([^"]+)">(.*?)</a>
.*?
<a \ href="([^"]+)">(.*?)</a>
}xs ];
}
$body =~ m{<p>(<small>.*?)</p>}sg
];
for my $d (@$data) {
$client->get($d->[-2] => sub {
my($self, $tx) = @_;
# warn $tx->req->url;
$tx->res->body =~ m{</h3>.*?<img src="(.*?)".*?<em>(.*?)</em>&nbsp; (\d+)}s;
push @$d, [ $1, $2, $3 ];
});
}
$client->process;
say Dumper $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment