PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/youtube-get

https://bitbucket.org/winstonsmith1913/youtube-get
Perl | 107 lines | 84 code | 8 blank | 15 comment | 22 complexity | 57abcc7f817e4933eca64c6a8c15c3ee MD5 | raw file
  1. #!/usr/bin/perl
  2. # Author: WinsonSmith1913 <WinstonSmith1913 gmail com>
  3. #
  4. # List all youtube videos in a users upload channel.
  5. # For now this should be used with youtube-dl which downloads the actual video.
  6. # In the future this script will contain a perl based version of youtube-dl
  7. #
  8. # Suggested usage with icky python(slow and for pre-alpha testing only) based youtube-dl:
  9. # youtube-get -n username | youtube-dl -a- -c -o "%(stitle)s-%(id)s.%(ext)s"
  10. # or if you want to keep a list of what you downloaded which is usefull when youtube-dl takes a dump which will happen:
  11. # youtube-get -n username -o vidlst ; youtube-dl -a vidlst -c -o "%(stitle)s-%(id)s.%(ext)s"
  12. #
  13. # I wrote this script in response to: http://bitbucket.org/rg3/youtube-dl/issue/44/channel-user-download-support
  14. # When it came time to upload it I found that there was already a fork of youtube-dl that included channel support.
  15. # Oh well. Atleast this is perl based instead of python based.
  16. use LWP;
  17. use Switch;
  18. sub GetUserUploadFeed {
  19. $VideoIndex=1; # start with the first video
  20. $IndexIncrement=25; # request 25 videos per page
  21. $NumberOfVideos=999999; # initialized to a high number to satisfy if statement
  22. while ($VideoIndex < $NumberOfVideos) { # loop until all videos are downloaded
  23. while (1) { #loop until the feed is downloaded, for poor connections
  24. $response = $ua->get("http://gdata.youtube.com/feeds/api/users/" .
  25. $UserName . "/uploads?orderby=published&start-index=" .
  26. $VideoIndex . "&max-results=25");
  27. if ($response->is_success) {
  28. $feed=$response->content;
  29. last;
  30. } else { # sending all messages to stderr so it isnt mixed in
  31. print STDERR "Timout, trying again...\n" if !$Silent; # with the data pipe
  32. }
  33. }
  34. if ($NumberOfVideos == 999999) { # is this your first time?
  35. $NumberOfVideos = int(($feed =~ m/(\d+)<.openSearch:totalResults>/gsm)[0]); # how many videos should I expect
  36. if ($NumberOfVideos < 1) { # is there anything to get?
  37. print STDERR "User '$UserName' does not have any uploaded videos or user does not exist.\n" if !$Silent;
  38. exit;
  39. } else {
  40. print STDERR "Found $NumberOfVideos uploaded videos.\n" if !$Silent;
  41. }
  42. }
  43. print STDERR "Looking up videos $VideoIndex-" . ($VideoIndex + $IndexIncrement) . "...\n" if !$Silent;
  44. push(@videos, ($feed =~ m/http:\/\/www\.youtube\.com\/watch\?v=[\w-]+/g)); #extract video links and append to list
  45. $VideoIndex += $IndexIncrement;
  46. }
  47. @videos = sort keys %{{ map { $_ => 1 } @videos }}; # sort and remove duplicate entries
  48. print STDERR "$UserName has $#videos out of $NumberOfVideos videos available to be downloaded.\n" if !$Silent;
  49. return @videos;
  50. }
  51. $Silent=0;
  52. $Usage= " Usage: $0 -n username [-o file] [-q]\n";
  53. if ($#ARGV == -1) { # requires atleast a username
  54. print STDERR $Usage;
  55. exit;
  56. } else {
  57. while ($#ARGV != -1) { # process commandline parameters
  58. switch($ARGV[0]) {
  59. case "-n" { # get username
  60. shift;
  61. if ($#ARGV == -1) {
  62. print STDERR $Usage;
  63. exit;
  64. } else {
  65. $UserName=@ARGV[0];
  66. }
  67. }
  68. case "-o" { # get optional output file
  69. shift;
  70. if ($#ARGV == -1) {
  71. print STDERR $Usage;
  72. exit;
  73. } else {
  74. $SaveListTo=@ARGV[0];
  75. }
  76. }
  77. case "-q" { # shut up! - silent mode
  78. $Silent=1;
  79. shift;
  80. }
  81. else { # found unkwown switch
  82. print STDERR $Usage;
  83. exit;
  84. }
  85. }
  86. shift;
  87. }
  88. }
  89. $ua = LWP::UserAgent->new;
  90. $ua->timeout(5); # set timeout, could be increased
  91. $ua->agent('Mozilla/5.0'); # hide useragent
  92. @videos=GetUserUploadFeed();
  93. if (defined($SaveListTo)) {
  94. open(OUTPUT, ">$SaveListTo");
  95. print OUTPUT join("\n", @videos); # save links to file
  96. close(OUTPUT);
  97. } else {
  98. print join("\n", @videos); # display links
  99. }