-
Notifications
You must be signed in to change notification settings - Fork 34
/
wimp-viewer
executable file
·201 lines (159 loc) · 4.73 KB
/
wimp-viewer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/perl
# List and play the most recent videos from: https://www.wimp.com/
# Requires 'youtube-viewer' and 'mpv'
use 5.010;
use strict;
use warnings;
use open ':std' => ':utf8';
use Encode qw(encode_utf8);
use XML::Fast qw(xml2hash);
use Term::ANSIColor qw(colored);
use Getopt::Std qw(getopts);
my $appname = 'wimp-viewer';
my $version = '0.33';
my $BASE_URL = 'https://www.wimp.com';
require Term::ReadLine;
my $term = Term::ReadLine->new($appname);
require WWW::Mechanize;
my $mech = WWW::Mechanize->new(
autocheck => 1,
env_proxy => 1,
show_progress => 0,
agent => 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36',
);
$mech->default_header('Accept-Encoding' => scalar HTTP::Message::decodable());
sub output_usage {
print <<"HELP";
usage: $0 [options] [url]
options:
-f : fullscreen mode
-r <i> : play i of random videos and exit
-v : print the version number and exit
-h : print this help message and exit
HELP
}
sub output_version {
say "$appname $version";
}
my %opt;
if (@ARGV) {
getopts('r:fvh', \%opt);
}
if ($opt{h}) {
output_usage();
exit 0;
}
if ($opt{v}) {
output_version();
exit 0;
}
if (exists $opt{r}) {
if (defined($opt{r}) and $opt{r} > 0) {
for my $i (1 .. $opt{r}) {
play_random_video();
}
}
else {
die "error: option '-r' requires a positive integer!\n";
}
exit;
}
# Play the command-line URIs
foreach my $url (@ARGV) {
play($url);
exit;
}
sub play {
my ($url) = @_;
my $resp = $mech->get($url);
my $content = $resp->decoded_content // $resp->content;
my $real_url = $mech->uri;
if ( $content =~ m{\byoutube\.com/watch\?v=([\w-]{11})"}
or $content =~ m{<div data-autoplay='1' data-start='0' data-id='([\w-]{11})'}
or $content =~ m{src="https://www.youtube.com/embed/([\w-]{11})}) {
system 'youtube-viewer', "--no-interactive", "--id=$1", ($opt{f} ? '-fs' : ());
}
elsif ( $content =~ /"file"\h*,\h*"(.*?)"/
or $content =~ m{source type="video/mp4" src="(https://.*?)"}) {
system('mpv', ($opt{f} ? '--fullscreen' : ()), $1);
}
else {
warn "error: can't find any streaming URL for: $real_url\n";
return;
}
return 1;
}
my @results;
foreach my $url ("$BASE_URL/feed/?hot=1", "$BASE_URL/feed/") {
my $hash_xml = xml2hash(encode_utf8($mech->get($url)->decoded_content));
push @results, @{$hash_xml->{rss}{channel}{item}};
}
sub play_picked_videos {
my (@list) = @_;
$#list >= 0 or return;
foreach my $num (@list) {
play($results[$num - 1]->{link});
}
return 1;
}
sub play_random_video {
play("$BASE_URL/random/");
return 1;
}
sub parse_date {
my ($date) = @_;
# Turns "Mon, 06 Feb 2012 00:00:00 -0600" into "Feb 06"
if ($date =~ /^\S+ (\d+) (\S+)/) {
return "$2 $1";
}
return $date // '';
}
{
print "\n";
my $num = 0;
foreach my $video (@results) {
$video->{title} =~ s/\s*\[VIDEO\]//;
printf "%s. %s [%s]\n", colored(sprintf("%2d", ++$num), 'bold'), $video->{title}, parse_date($video->{pubDate});
}
{
my $line = $term->readline(colored("\n=>> Insert a number ('?' for help)", 'bold') . "\n> ");
if ($line eq 'help' or $line eq '?') {
print "\n", <<'STDIN_HELP';
i : play the corresponding video
all : play all the video results
3-8, 3..8 : same as 3 4 5 6 7 8
/my?[regex]*$/ : play videos matched by a regex (/i)
q, quit, exit : exit application
STDIN_HELP
redo;
}
elsif ($line =~ /^(?:q|quit|exit)\z/) {
exit 0;
}
elsif ($line eq 'all') {
play_picked_videos(1 .. @results);
}
elsif ($line =~ m{^/(.*?)/\h*$}) {
my $match = eval { qr/$1/i };
if ($@) {
warn "\nError in regex: $@\n";
redo;
}
play_picked_videos(grep { $results[$_ - 1]->{'title'} =~ /$match/ } 1 .. @results) || do {
warn "\n(X_X) No video matched by the regex: /$match/\n";
redo;
};
}
elsif ($line =~ /\d/ and not $line =~ /(?>\s|^)[^\d-]/) {
$line =~ s/(\d+)(?>-|\.\.)(\d+)/join q{ }, $1 .. $2;/eg; # '2..5' or '2-5' to '2 3 4 5'
play_picked_videos(grep { $_ > 0 and $_ <= @results if /^\d+$/ } split(/[\s[:punct:]]+/, $line));
}
elsif ($line =~ /^(?:r|random)\z/) {
play_random_video();
}
elsif ($line =~ m{^https?://.}) {
play($_);
}
}
redo;
}