Skip to content

Instantly share code, notes, and snippets.

@bvr
Created January 14, 2013 05:54
Show Gist options
  • Save bvr/4527990 to your computer and use it in GitHub Desktop.
Save bvr/4527990 to your computer and use it in GitHub Desktop.
Find title and stream URL on dailymotion page
use 5.010; use strict; use warnings;
# ppm inst Mojolicious
use Mojo::UserAgent;
use Mojo::Cookie::Response;
use Mojo::Util 'url_unescape';
use Mojo::JSON;
# debugging
use Path::Class;
use DDP;
my $url = shift or die "syntax: $0 url\n";
my $ua = Mojo::UserAgent->new(max_redirects => 5);
$ua->cookie_jar->add(
Mojo::Cookie::Response->new(
domain => '.dailymotion.com',
path => '/',
name => 'family_filter',
value => 'off'
)
);
my $res = $ua->get($url)->res;
my $sequence = do {
$res->body =~ /"sequence":"(.*?)"/
? url_unescape $1
: die 'cannot find the sequence'
};
# file('seq.json')->spew($sequence);
# get the title
say $res->dom->at('meta[property="og:title"]')->attrs('content');
# list of urls
my $iter = struct_iter(Mojo::JSON->new->decode($sequence));
while(my ($prefix, $value) = $iter->()) {
if($prefix =~ /URL$/ && $value =~ /(\d+)x(\d+)\/video\/.*?\.([a-z0-9]+)/) {
say "$1 $2 $3: $value";
}
}
sub struct_iter {
my ($item_ref) = @_;
my @blocks = (['', $item_ref]);
return sub {
while(my $blk = shift @blocks) {
my ($prefix, $item) = @$blk;
for(ref $item) {
when('ARRAY') {
push @blocks,
map { ["$prefix/[$_]", $item->[$_]] } 0 .. $#$item;
}
when('HASH') {
push @blocks,
map { ["$prefix/$_", $item->{$_}] } sort keys %$item;
}
return ($prefix, $item);
}
}
return;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment