-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube-search
executable file
·111 lines (97 loc) · 2.82 KB
/
youtube-search
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
#!/usr/bin/perl
use strict;
use warnings;
#use diagnostics;
use Getopt::Long;
use URI::Escape;
use LWP::Simple;
use HTML::Entities;
binmode(STDOUT, ':utf8');
my $pattern = uri_escape(shift @ARGV);
my $playlist = 0;
my $first = 0;
my $skip = 0;
my $counter = 0;
my $user;
my $page;
my $music = 0;
my $batch = 0;
my $max;
Getopt::Long::Configure("pass_through");
GetOptions ( 'p|playlist' => \$playlist
, '1|first' => \$first
, 's|skip' => \$skip
, 'u|user=s' => \$user
, 'm|music' => \$music
, 'b|batch' => \$batch
, 'max=i' => \$max
)
|| die('Parsing error');
if ($batch and (not $max)) {
$max = 1 << 32;
} else {
$max = 10; # security
}
push @ARGV, "-novideo" if ($music);
PAGES: for my $i (1..50) {
if ($counter == 0 and $i > 1) {
print STDERR ("stopping as there doesn't seem to be any result anyway\n");
exit 1;
}
print STDERR ("fetching new page $i\n");
if ($user) {
$page = "https://youtube.com/user/${user}/videos?query=${pattern}&page=${i}";
} else {
$page = "https://youtube.com/results?search_query=${pattern}&page=${i}";
}
my $html = get($page);
if (!defined $html) {
print "page is empty, seems not connected or error\n";
exit 1;
}
VIDEOS: while ($html =~ m{<span .*?video-time.*?>([0-9:]+)</span>.*?<h3 .*?yt-lockup-title.*?<a .*?href="/watch\?v=([^"]+?)"[^>]*>([^<]+)<}gs) {
my ($title,$watch,$time) = (decode_entities($3),$2,$1);
my $video_url = "https://youtube.com/watch?v=$watch";
$counter += 1;
if ($counter <= $skip) {
print STDERR ("Skipping ($counter) $watch\n");
next VIDEOS;
}
if ($max <= $counter) {
print STDERR ("Stopped at last video\n");
last PAGES;
}
if ($batch) {
print("$counter $title $video_url\n");
}
else {
print("Play video #$counter '$title' ($time) [ $video_url ]? ");
}
unless ($batch || $playlist) {
my $ans = <stdin>;
if ($ans =~ /^(n|no|non)$/i) {
next VIDEOS;
}
elsif ($ans =~ /^(p|pl|playlist)$/i) {
my ($pl) = $video_url =~ m{list=([^&]+)};
print("playing full playlist: $pl\n");
if (!$batch && system("youtube-playlist", $pl, @ARGV)) {
exit 1;
}
next VIDEOS;
}
elsif ($ans =~ /^only$/i) {
$first = 1;
}
elsif ($ans =~ /^(q|quit|exit|stop)$/) {
exit(1);
}
}
if (!$batch && system("youtube", $video_url, @ARGV)) {
exit 1;
}
if ($first == 1) {
last PAGES;
}
}
}