]>
git.gir.st - subscriptionfeed.git/blob - app/utils/refresh-cipher.pl
8 use DBI
; # reqires perl-DBD-SQLite, perl-DBI
10 my $ytdown = "/tmp/youtubedown.pm";
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;"
16 require $ytdown; # note: can't use 'use', as that parses at compile time (before youtuebdown is downloaded)
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
23 my $response = HTTP
::Tiny-
>new
24 ->get("https://www.youtube-nocookie.com/embed/jNQXAC9IVRw?hl=en&gl=US")
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=["'](.*?)["']}) {
34 return undef; # unable to extract player/base.js URL
37 my $cf = Config
::Tiny-
>read( $ENV{YT_CONFIG
} // "/etc/yt/config.ini", 'utf8' );
38 die("config not found or invalid: @{[Config::Tiny->errstr]}") unless defined $cf->{global
}->{database
};
40 my $new_url = find_player_url
();
41 exit 1 unless $new_url; # failed
43 my $dbh = DBI-
>connect("dbi:SQLite:dbname=$cf->{global}->{database}", undef, undef, {
48 my $sth = $dbh->prepare("SELECT url FROM cipher");
51 my ($player_url) = @{$sth->fetchrow_arrayref()};
52 exit 0 if $player_url eq $new_url; # unchanged
55 my ($cipher_id) = $new_url =~ m
@/s/player/(.*?
)\
.js
@;
56 my ($sts, $algo) = split / /, guess_cipher
($cipher_id, 0, 0), 2;
58 # Note: since sqlite only got UPSERT in 2018, we use REPLACE INTO rowid=1 for the same result.
59 $dbh->prepare("INSERT OR REPLACE INTO cipher (rowid, url, sts, algorithm) VALUES (0,?,?,?)")
60 ->execute($new_url, $sts, $algo);