Debian Codegraph @

contrib/gencode.pl

raw
1 #!/usr/bin/perl
2 # Public Domain
3
4 # simple script to generate perl code from yt-dl.org python code
5 # perl gencode.pl < youtube.py
6 # or
7 # youtube-dl -v \
8 # --no-cache-dir \
9 # --skip-download \
10 # --youtube-print-sig-code http://youtu.be/cmSbXsFE3l8 | \
11 # tail -n 3 | perl gencode.pl
12
13 use warnings;
14 use strict;
15
16 my $len;
17 my $found;
18
19 while (<STDIN>) {
20 if (m/if\s+len.\w+.\s+==\s+(\d+)\:/) {
21 $len = $1;
22 printf(" \x7d elsif (\@s == %d) \x7b\n", $len);
23 next;
24 }
25
26 if ($len && m/return\s+(.*)\s*$/) {
27 my @items = ();
28 for (split(/\s*\+\s*/, $1)) {
29 if (m/^\w+\[(\d+)\]$/) {
30 push @items, "\$s[$1]";
31 } elsif (m/^\w+\[:(\d+)\]$/) {
32 my $i = $1 - 1;
33 push @items, "\@s[0..$i]";
34 } elsif (m/^\w+\[(\d+):(\d+)\]$/) {
35 my $i = $2 - 1;
36 push @items, "\@s[$1..$i]";
37 } elsif (m/^\w+\[(\d+):(\d+):-1\]$/) {
38 my $i = $2 + 1;
39 push @items, "reverse(\@s[$i..$1])";
40 } elsif (m/^\w+\[(\d+)::-1\]$/) {
41 push @items, "reverse(\@s[0..$1])";
42 } elsif (m/^\w+\[:(\d+)\]\[::-1\]$/) {
43 my $i = $1 - 1;
44 push @items, "reverse(\@s[0..$i])";
45 } elsif (m/^\w+\[(\d+):\]$/) {
46 my $i = $len - 1;
47 push @items, "\@s[$1..$i]";
48 } else {
49 die "Unable to parse: $_";
50 }
51 }
52 my $str = join(', ', @items);
53 printf(" return (%s);\n", $str);
54 $found = 1 if $str;
55 undef $len;
56 next;
57 } elsif ($found && m/ExtractorError/) {
58 printf(" \x7d\n");
59 last;
60 }
61 }