]> git.gir.st - subscriptionfeed.git/blob - app/refresh-cipher.pl
initial commit
[subscriptionfeed.git] / app / refresh-cipher.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use 5.014;
6 use HTTP::Tiny;
7 use Config::Tiny;
8 use DBI; # reqires perl-DBD-SQLite, perl-DBI
9
10 my $ytdown = "/tmp/youtubedown.pm";
11
12 if (! -f $ytdown || (stat($ytdown))[9] < time-7*24*60*60) { # update youtubedown once a week
13 system(qq{curl -sAMozilla https://www.jwz.org/hacks/youtubedown|sed '\$d'|sed '\$c1\\;' > $ytdown});
14 } # delete "exit 0;"---^ ^---replace "main();" with "1;"
15
16 require $ytdown; # note: can't use 'use', as that parses at compile time (before youtuebdown is downloaded)
17
18 sub find_player_url {
19 # request a random video (this is the first ever uploaded one, "me at
20 # the zoo". it shouldn't go away any time soon) note that the string
21 # we're looking for contains our locale, so specify it to avoid it
22 # changing.
23 my $response = HTTP::Tiny->new
24 ->get("https://www.youtube-nocookie.com/embed/jNQXAC9IVRw?hl=en&gl=US")
25 ->{content};
26
27 # extract the src attribute from the script tag whose name attribute
28 # has value "player_ias/base":
29 if ($response =~ m{(<script .*?\bname=["']player_ias/base["'].*?>)}) {
30 if ($1 =~ m{src=["'](.*?)["']}) {
31 return $1;
32 }
33 }
34 return undef; # unable to extract player/base.js URL
35 }
36
37 my $cf = Config::Tiny->read( $ENV{YT_CONFIG} // "/etc/yt/config.ini", 'utf8' );
38 my $new_url = find_player_url();
39
40 my $dbh = DBI->connect("dbi:SQLite:dbname=$cf->{global}->{database}", undef, undef, {
41 AutoCommit => 1,
42 sqlite_unicode => 1,
43 sqlite_open_flags => 1, # read-only
44 });
45
46 my $sth = $dbh->prepare("SELECT url FROM cipher");
47 $sth->execute();
48 my ($player_url) = @{$sth->fetchrow_arrayref()};
49
50 exit 1 unless $new_url; # failed
51 exit 0 if $player_url eq $new_url; # unchanged
52
53 my ($cipher_id) = $player_url =~ m@/s/player/(.*?)\.js@;
54 my ($sts, $algo) = split / /, guess_cipher($cipher_id, 0, 0), 2;
55
56 $dbh->prepare("
57 INSERT OR REPLACE INTO cipher (url, sts, algorithm)
58 VALUES (?, ?, ?)
59 ")->execute($new_url, $sts, $algo);
Imprint / Impressum