/youtube-get
Perl | 107 lines | 84 code | 8 blank | 15 comment | 22 complexity | 57abcc7f817e4933eca64c6a8c15c3ee MD5 | raw file
- #!/usr/bin/perl
- # Author: WinsonSmith1913 <WinstonSmith1913 gmail com>
- #
- # List all youtube videos in a users upload channel.
- # For now this should be used with youtube-dl which downloads the actual video.
- # In the future this script will contain a perl based version of youtube-dl
- #
- # Suggested usage with icky python(slow and for pre-alpha testing only) based youtube-dl:
- # youtube-get -n username | youtube-dl -a- -c -o "%(stitle)s-%(id)s.%(ext)s"
- # or if you want to keep a list of what you downloaded which is usefull when youtube-dl takes a dump which will happen:
- # youtube-get -n username -o vidlst ; youtube-dl -a vidlst -c -o "%(stitle)s-%(id)s.%(ext)s"
- #
- # I wrote this script in response to: http://bitbucket.org/rg3/youtube-dl/issue/44/channel-user-download-support
- # When it came time to upload it I found that there was already a fork of youtube-dl that included channel support.
- # Oh well. Atleast this is perl based instead of python based.
- use LWP;
- use Switch;
- sub GetUserUploadFeed {
- $VideoIndex=1; # start with the first video
- $IndexIncrement=25; # request 25 videos per page
- $NumberOfVideos=999999; # initialized to a high number to satisfy if statement
- while ($VideoIndex < $NumberOfVideos) { # loop until all videos are downloaded
- while (1) { #loop until the feed is downloaded, for poor connections
- $response = $ua->get("http://gdata.youtube.com/feeds/api/users/" .
- $UserName . "/uploads?orderby=published&start-index=" .
- $VideoIndex . "&max-results=25");
- if ($response->is_success) {
- $feed=$response->content;
- last;
- } else { # sending all messages to stderr so it isnt mixed in
- print STDERR "Timout, trying again...\n" if !$Silent; # with the data pipe
- }
- }
- if ($NumberOfVideos == 999999) { # is this your first time?
- $NumberOfVideos = int(($feed =~ m/(\d+)<.openSearch:totalResults>/gsm)[0]); # how many videos should I expect
- if ($NumberOfVideos < 1) { # is there anything to get?
- print STDERR "User '$UserName' does not have any uploaded videos or user does not exist.\n" if !$Silent;
- exit;
- } else {
- print STDERR "Found $NumberOfVideos uploaded videos.\n" if !$Silent;
- }
- }
- print STDERR "Looking up videos $VideoIndex-" . ($VideoIndex + $IndexIncrement) . "...\n" if !$Silent;
- push(@videos, ($feed =~ m/http:\/\/www\.youtube\.com\/watch\?v=[\w-]+/g)); #extract video links and append to list
- $VideoIndex += $IndexIncrement;
- }
- @videos = sort keys %{{ map { $_ => 1 } @videos }}; # sort and remove duplicate entries
- print STDERR "$UserName has $#videos out of $NumberOfVideos videos available to be downloaded.\n" if !$Silent;
- return @videos;
- }
- $Silent=0;
- $Usage= " Usage: $0 -n username [-o file] [-q]\n";
- if ($#ARGV == -1) { # requires atleast a username
- print STDERR $Usage;
- exit;
- } else {
- while ($#ARGV != -1) { # process commandline parameters
- switch($ARGV[0]) {
- case "-n" { # get username
- shift;
- if ($#ARGV == -1) {
- print STDERR $Usage;
- exit;
- } else {
- $UserName=@ARGV[0];
- }
- }
- case "-o" { # get optional output file
- shift;
- if ($#ARGV == -1) {
- print STDERR $Usage;
- exit;
- } else {
- $SaveListTo=@ARGV[0];
- }
- }
- case "-q" { # shut up! - silent mode
- $Silent=1;
- shift;
- }
- else { # found unkwown switch
- print STDERR $Usage;
- exit;
- }
- }
- shift;
- }
- }
- $ua = LWP::UserAgent->new;
- $ua->timeout(5); # set timeout, could be increased
- $ua->agent('Mozilla/5.0'); # hide useragent
- @videos=GetUserUploadFeed();
- if (defined($SaveListTo)) {
- open(OUTPUT, ">$SaveListTo");
- print OUTPUT join("\n", @videos); # save links to file
- close(OUTPUT);
- } else {
- print join("\n", @videos); # display links
- }