Created
December 14, 2016 02:48
-
-
Save BlueCocoa/699500b78dd92399434b9200e705e963 to your computer and use it in GitHub Desktop.
Get bilibili download links in Perl6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl6 | |
use HTTP::UserAgent; | |
use HTTP::Request; | |
use JSON::Tiny; | |
sub MAIN($bilibili_link) { | |
if $bilibili_link ~~ /^[http\:\/\/]www\.bilibili\.com\/video\/av(\d+)[\/index_(\d+)\.html[\#page\=(\d+)]?]? / { | |
my $aid = ~$0; | |
my $page = 1; | |
if $2 { | |
$page = ~$2; | |
} elsif $/[1] { | |
$page = ~$1; | |
} | |
my $url = "http://api.bilibili.com/playurl?callback=&aid=$aid&page=$page&platform=html5&quality=1&vtype=mp4&type=jsonp&token=d41d8cd98f00b204e9800998ecf8427e"; | |
say $url; | |
my $req = HTTP::Request.new(GET => $url); | |
my $ua = HTTP::UserAgent.new(UserAgent => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/602.2.14 (KHTML, like Gecko) Version/10.0.1 Safari/602.2.14'); | |
my $res = $ua.request($req); | |
my %json = from-json $res.content.substr(1, *-2); | |
sub array_durl(@durl) { | |
for @durl -> %info { | |
say %info<url>; | |
if %info<backup_url>:exists { | |
for %info<backup_url>[0] -> $backup { | |
say $backup; | |
} | |
} | |
} | |
} | |
sub hash_durl(%durl) { | |
for %durl.kv -> $key, $value { | |
say $value<url>; | |
if $value<backup_url>:exists { | |
for $value<backup_url>.values -> $url { | |
say $url; | |
} | |
} | |
} | |
} | |
given ~ %json{'durl'}.WHAT.^name { | |
when $_ ~~ /Array/ { | |
my @durl = %json{'durl'}[0]; | |
array_durl(@durl); | |
} | |
when $_ ~~ /Hash/ { | |
my %durl = %json{'durl'}; | |
hash_durl(%durl); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment