ytList.pl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use File::Basename;
  5. use LWP::Simple;
  6. use URI::Escape;
  7. use JSON;
  8. use Date::Parse;
  9. # Paramters
  10. my $BATCH_SIZE = 50;
  11. my $MAX_INDEX = 500;
  12. my $URL_PREFIX = 'http://gdata.youtube.com/feeds/api/users/';
  13. my $URL_SUFFIX = '/uploads';
  14. my %API_PARAMS = (
  15. 'strict' => 1,
  16. 'v' => 2,
  17. 'alt' => 'jsonc',
  18. 'orderby' => 'published',
  19. 'max-results' => $BATCH_SIZE,
  20. );
  21. # Prototypes
  22. sub findVideos($);
  23. sub findFiles($);
  24. # Sanity check
  25. if (scalar(@ARGV) < 1) {
  26. die('Usage: ' . basename($0) . " output_directory\n");
  27. }
  28. # Command-line parameters
  29. my ($dir) = @ARGV;
  30. $dir =~ s/\/+$//;
  31. if (!-d $dir) {
  32. die('Invalid output directory: ' . $dir . "\n");
  33. }
  34. my $user = basename($dir);
  35. if (length($user) < 1 || !($user =~ /^\w+$/)) {
  36. die('Invalid user: ' . $user . "\n");
  37. }
  38. # Environmental parameters
  39. my $DEBUG = 0;
  40. if ($ENV{'DEBUG'}) {
  41. if ($ENV{'DEBUG'} =~ /(\d+)/) {
  42. $DEBUG = $1;
  43. } else {
  44. $DEBUG = 1;
  45. }
  46. }
  47. # Find all the user's videos on YT
  48. my $videos = findVideos($user);
  49. # Find all existing YT files on disk
  50. my $files = findFiles($dir);
  51. # Fill in missing videos and NFOs
  52. foreach my $id (keys(%{$videos})) {
  53. my $basePath;
  54. {
  55. my $safeTitle = $videos->{$id}->{'title'};
  56. $safeTitle =~ s/\:/ - /g;
  57. $safeTitle =~ s/\s+/ /g;
  58. $safeTitle =~ s/[^\w\-\.\?\!\&\,\; ]/_/g;
  59. $basePath = $dir . '/' . sprintf('%02d', $videos->{$id}->{'number'}) . ' - ' . $safeTitle . ' (' . $id . ').';
  60. }
  61. if (!exists($files->{$id})) {
  62. if ($DEBUG) {
  63. print STDERR 'Fetching video: ' . $basePath . "\n";
  64. }
  65. }
  66. if (!exists($files->{$id}) || !-e $files->{$id}->{'nfo'}) {
  67. if ($DEBUG) {
  68. print STDERR 'Creating NFO: ' . $basePath . "nfo\n";
  69. }
  70. }
  71. }
  72. sub findFiles($) {
  73. my ($dir) = @_;
  74. my %files = ();
  75. # Read the output directory
  76. my $fh = undef();
  77. opendir($fh, $dir)
  78. or die('Unable to open output directory: ' . $! . "\n");
  79. while (my $file = readdir($fh)) {
  80. my ($num, $title, $id, $suffix) = $file =~ /(\d+) - (.+) \(([\w\-]+)\)\.(\w\w\w)$/;
  81. if (defined($id) && length($id) > 0) {
  82. if ($suffix eq 'nfo') {
  83. next;
  84. }
  85. my %tmp = (
  86. 'number' => $num,
  87. 'title' => $title,
  88. 'suffix' => $suffix,
  89. 'path' => $dir . '/' . $file,
  90. );
  91. $tmp{'nfo'} = $tmp{'path'};
  92. $tmp{'nfo'} =~ s/\.\w\w\w$/\.nfo/;
  93. $files{$id} = \%tmp;
  94. }
  95. }
  96. close($fh);
  97. if ($DEBUG) {
  98. print STDERR "\n\nFound " . scalar(keys(%files)) . " files:\n\t" . join("\n\t", keys(%files)) . "\n\n";
  99. if ($DEBUG > 1) {
  100. use PrettyPrint;
  101. print STDERR prettyPrint(\%files, "\t") . "\n";
  102. }
  103. }
  104. return \%files;
  105. }
  106. sub findVideos($) {
  107. my ($user) = @_;
  108. my %videos = ();
  109. # Build the base URL
  110. my $baseURL = $URL_PREFIX . $user . $URL_SUFFIX;
  111. # And the static URL parameters
  112. my %params = %API_PARAMS;
  113. # Loop through until we have all the entries
  114. my $index = 1;
  115. my $itemCount = undef();
  116. LOOP:
  117. {
  118. # Build the actual URL
  119. my $url = $baseURL . '?start-index=' . $index;
  120. foreach my $name (keys(%params)) {
  121. $url .= '&' . uri_escape($name) . '=' . uri_escape($params{$name});
  122. }
  123. # Fetch
  124. if ($DEBUG) {
  125. print STDERR 'Fetching list URL: ' . $url . "\n";
  126. }
  127. my $content = get($url);
  128. if (!defined($content) || length($content) < 10) {
  129. die('Invalid content from URL: ' . $url . "\n");
  130. }
  131. # Parse
  132. my $data = decode_json($content);
  133. if (!defined($data) || ref($data) ne 'HASH') {
  134. die('Invalid JSON: ' . $content . "\n");
  135. }
  136. if (!exists($data->{'data'})) {
  137. die('Invalid data set: ' . $content . "\n");
  138. }
  139. $data = $data->{'data'};
  140. # Grab the total count, so we know when to stop
  141. if (!defined($itemCount) && exists($data->{'totalItems'})) {
  142. $itemCount = $data->{'totalItems'};
  143. }
  144. # Process each item
  145. if (!exists($data->{'items'}) || ref($data->{'items'}) ne 'ARRAY') {
  146. die('Invalid item list: ' . $content . "\n");
  147. }
  148. my $items = $data->{'items'};
  149. my $offset = 0;
  150. foreach my $item (@{$items}) {
  151. my %tmp = (
  152. 'number' => ($itemCount - $index - $offset + 1),
  153. 'title' => $item->{'title'},
  154. 'date' => str2time($item->{'uploaded'}),
  155. 'description' => $item->{'description'},
  156. );
  157. $videos{ $item->{'id'} } = \%tmp;
  158. $offset++;
  159. }
  160. # Loop if there are results left to fetch
  161. $index += $BATCH_SIZE;
  162. if (defined($itemCount) && $itemCount > $index) {
  163. # But don't go past the max supported index
  164. if ($index <= $MAX_INDEX) {
  165. redo LOOP;
  166. }
  167. }
  168. }
  169. if ($DEBUG) {
  170. print STDERR "\n\nFound " . scalar(keys(%videos)) . ' videos for YouTube user ' . $user . ":\n\t" . join("\n\t", keys(%videos)) . "\n\n";
  171. if ($DEBUG > 1) {
  172. use PrettyPrint;
  173. print STDERR prettyPrint(\%videos, "\t") . "\n";
  174. }
  175. }
  176. return \%videos;
  177. }