Debian Codegraph @

script/youtube-playlists

raw
1 #!perl
2
3 # PODNAME: youtube-playlists.pl
4
5 use strict;
6 use warnings;
7
8 use WWW::YouTube::Download;
9 use Getopt::Long qw(GetOptions :config bundling);
10 use Pod::Usage qw(pod2usage);
11
12 my $proxy = undef;
13 GetOptions(
14 'C|no-colors!' => \my $disable_colors,
15 'i|video-id' => \my $only_id,
16 'n|normalize' => \my $normalize,
17 'p|proxy=s' => \$proxy,
18 'user-agent=s' => \my $user_agent,
19 'limit=s' => \my $limit,
20 'u|users' => \my $user_uploads,
21 'v|verbose' => \my $verbose,
22 'h|help' => sub { pod2usage(exitval => 0, -noperldoc => 1, -verbose => 2) },
23 'V|version' => sub { show_version() },
24 ) or pod2usage(2);
25 pod2usage() unless @ARGV;
26
27 my $yt = WWW::YouTube::Download->new( verbose => $verbose );
28
29 if($proxy){
30 $yt->ua->proxy(['http','https'] => $proxy);
31 print "--> Using proxy $proxy\n";
32 }
33
34 if ($user_agent) {
35 $yt->ua->agent($user_agent);
36 print "--> Using user-agent-string: '". $yt->ua->agent()."'\n";
37 }
38 $yt->ua->default_header('Accept-Encoding' => scalar HTTP::Message::decodable());
39
40 main: {
41 for my $id_or_url (@ARGV) {
42 chatty("--> Working on %s\n", $id_or_url);
43 my $id;
44 if ($user_uploads) {
45 $id = $yt->user_id($id_or_url);
46 } else {
47 $id = $yt->playlist_id($id_or_url);
48 }
49 throw('%s is not a supported argument', $id_or_url) unless $id;
50 if ($normalize) {
51 print "$id\n";
52 next;
53 }
54 chatty("--> Recognized playlist id %s\n", $id);
55
56 my $videos = $yt->playlist($id, { limit => $limit });
57
58 my $cnt;
59 for(@$videos){
60 $cnt++;
61 print 'https://www.youtube.com/watch?v=' . $_->{id} ."\n";
62 }
63 }
64 }
65
66 sub throw {
67 my $format = shift;
68 die pcolor(['red'], sprintf($format, @_)), "\n";
69 }
70
71 sub chatty {
72 return unless $verbose;
73 my $format = shift;
74 print STDERR sprintf $format, @_;
75 }
76
77 sub pcolor {
78 my ($color, @msg) = @_;
79
80 if ($^O eq 'MSWin32' || $disable_colors || !-t STDOUT) {
81 return @msg;
82 }
83
84 eval { require Term::ANSIColor };
85 return @msg if $@; # module not available
86 return Term::ANSIColor::colored($color, @msg);
87 }
88
89 sub show_version {
90 print "youtube-playlists (WWW::YouTube::Download) version $WWW::YouTube::Download::VERSION\n";
91 exit;
92 }
93
94 =pod
95
96 =encoding UTF-8
97
98 =head1 NAME
99
100 youtube-playlists.pl - Find YouTube video URLs from playlist(s)
101
102 =head1 VERSION
103
104 version 0.65
105
106 =head1 SYNOPSIS
107
108 # print the list of video URLs
109 $ youtube-playlists https://www.youtube.com/playlist?list=PLB199169FA7413767
110 $ youtube-playlists PLB199169FA7413767
111
112 # pipe result to youtube-download
113 $ youtube-playlists PLB199169FA7413767 | youtube-download
114
115 # with socks proxy
116 $ youtube-playlists -p socks://<some IP>:<some port>/ PLB199169FA7413767
117
118 =head1 DESCRIPTION
119
120 For each given argument B<youtube-playlists> generates a list of YouTube
121 video URLs. Arguments can be URL to playlist or to favorite list, or
122 only IDs of a playlist or a favorite list.
123
124 In the spirit of the whole I<WWW::YouTube::Download> distribution, playlist
125 extraction relies solely on scraping common YouTube pages and requires no API key.
126
127 =head1 OPTIONS
128
129 =over
130
131 =item -C, --no-colors
132
133 Force disable colored output
134
135 =item -i, --video-id
136
137 Print only video IDs, not full URLs
138
139 =item -n, --normalize
140
141 Print only normalized playlist IDs, but do not fetch anything.
142 You can call it also dry run.
143
144 =item -p, --proxy
145
146 Use the given proxy. Note that using a socks proxy requires LWP::protocol::socks to be installed.
147
148 =item -u, --users
149
150 Parses given parameters as YouTube usernames and lists their uploaded videos.
151
152 =item --user-agent
153
154 Supply your own user agent string
155
156 =item --limit
157
158 Limit how many fetches the script is allowed to make while scraping playlist items.
159
160 =item -v, --verbose
161
162 turns on chatty output
163
164 =item -h, --help
165
166 display help
167
168 =item -V, --version
169
170 display version
171
172 =back
173
174 =head1 AUTHOR
175
176 xaicron <xaicron {@} cpan.org>
177
178 =head1 COPYRIGHT AND LICENSE
179
180 This software is copyright (c) 2013 by Yuji Shimada.
181
182 This is free software; you can redistribute it and/or modify it under
183 the same terms as the Perl 5 programming language system itself.
184
185 =cut
186
187 __END__
188
189 # ABSTRACT: Find YouTube video URLs from playlist(s)
190