critrole.pl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use LWP::UserAgent;
  5. use File::Basename;
  6. # Parameters
  7. my $TOP_URL = 'http://geekandsundry.com/shows/critical-role/';
  8. my $PG_MATCH = qr/\<link rel=\'next\' href=\'([^\']+\/page\/\d+\/)\'/;
  9. my $EP_MATCH = qr/^https?\:\/\/[^\/]+\/critical\-role\-episode/i;
  10. my $INI_PATH = `~/bin/video/mediaPath` . '/YouTube/Critical Role (None)/extra_videos.ini';
  11. my $UA_STR = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A';
  12. my $TIMEOUT = 10;
  13. # Debug
  14. our $DEBUG = 0;
  15. if (defined($ENV{'DEBUG'}) && $ENV{'DEBUG'} =~ /(\d+)/) {
  16. $DEBUG = $1;
  17. }
  18. # ENV Parameters
  19. if (exists($ENV{'TOP_URL'})) {
  20. $TOP_URL = $ENV{'TOP_URL'};
  21. if ($DEBUG) {
  22. print STDERR 'Using ENV TOP_URL: ' . $TOP_URL . "\n";
  23. }
  24. }
  25. if (exists($ENV{'EP_MATCH'})) {
  26. $EP_MATCH = qr/${ENV{'EP_MATCH'}}/i;
  27. if ($DEBUG) {
  28. print STDERR 'Using ENV EP_MATCH: qr/' . $ENV{'EP_MATCH'} . "/i\n";
  29. }
  30. }
  31. if (exists($ENV{'INI_PATH'})) {
  32. $INI_PATH = $ENV{'INI_PATH'};
  33. if ($DEBUG) {
  34. print STDERR 'Using ENV INI_PATH: ' . $INI_PATH . "\n";
  35. }
  36. }
  37. # Setup LWP
  38. my $ua = LWP::UserAgent->new();
  39. if (!$ua) {
  40. die('Unable to init UA: ' . $! . "\n");
  41. }
  42. $ua->agent($UA_STR);
  43. $ua->timeout($TIMEOUT);
  44. $ua->env_proxy();
  45. # Read the existing INI file
  46. my %old_ytids = ();
  47. {
  48. my $fh = undef();
  49. open($fh, '<', $INI_PATH)
  50. or die('Unable to read ini file: ' . $INI_PATH . ': ' . $! . "\n");
  51. my $url = undef();
  52. while (<$fh>) {
  53. if (/^\s*#\s*(https?\:\/\/[^\s]+)/i) {
  54. $url = $1;
  55. } elsif (/([\w\-]+)/) {
  56. if (!$url) {
  57. $url = 'Unknown';
  58. }
  59. $old_ytids{$1} = $url;
  60. $url = undef();
  61. if ($DEBUG > 1) {
  62. print STDERR 'Found existing YTID: ' . $1 . ' # ' . $old_ytids{$1} . "\n";
  63. }
  64. }
  65. }
  66. close($fh);
  67. }
  68. # Use the top-level page to find episode-specific pages
  69. my @episodes = ();
  70. {
  71. my $url = $TOP_URL;
  72. NEXT_PAGE: {
  73. if ($DEBUG) {
  74. print STDERR 'Loading top page: ' . $url . "\n";
  75. }
  76. my $response = $ua->get($url);
  77. if (!$response->is_success()) {
  78. die(basename($0) . ": Unable to fetch top-level page\n");
  79. }
  80. foreach my $link (split(/\<a\s+/, $response->decoded_content())) {
  81. if ($link =~ /\s+href=\"([^\"]+)\"/) {
  82. my $href = $1;
  83. if ($href =~ $EP_MATCH) {
  84. push(@episodes, $href);
  85. }
  86. }
  87. }
  88. if ($response->decoded_content() =~ $PG_MATCH) {
  89. $url = $1;
  90. redo NEXT_PAGE;
  91. }
  92. }
  93. }
  94. if ($DEBUG) {
  95. print STDERR 'Episode URLs (' . scalar(@episodes) . "):\n\t" . join("\n\t", @episodes) . "\n";
  96. }
  97. # Find each episode's YTID
  98. my %ytids = ();
  99. {
  100. foreach my $url (@episodes) {
  101. my $response = $ua->get($url);
  102. if (!$response->is_success()) {
  103. die(basename($0) . ': Unable to fetch episode page: ' . $url . "\n");
  104. }
  105. if ($response->decoded_content() =~ /data-ytid=\"([^\"]+)\"/) {
  106. $ytids{$1} = $url;
  107. } else {
  108. print STDERR 'No YTID found on: ' .$url . "\n";
  109. if ($DEBUG > 1) {
  110. print STDERR "\tPage content:\n" . $response->decoded_content() . "\n\n";
  111. }
  112. }
  113. }
  114. }
  115. if ($DEBUG) {
  116. print STDERR 'YouTube IDs (' . scalar(keys(%ytids)) . "):\n\t" . join("\n\t", keys(%ytids)) . "\n";
  117. }
  118. # Drop known episodes
  119. {
  120. foreach my $id (keys(%ytids)) {
  121. if (exists($old_ytids{$id})) {
  122. if ($DEBUG > 1) {
  123. print 'Ignoring known episode: ' . $id . ' # ' . $old_ytids{$id} . "\n";
  124. }
  125. delete($ytids{$id});
  126. }
  127. }
  128. }
  129. # Update the INI file
  130. {
  131. my $fh = undef();
  132. open($fh, '>>', $INI_PATH)
  133. or die('Unable to write ini file: ' . $INI_PATH . ': ' . $! . "\n");
  134. foreach my $id (keys(%ytids)) {
  135. if ($DEBUG) {
  136. print 'Adding episode: ' . $id . ' # ' . $ytids{$id} . "\n";
  137. }
  138. print $fh "\n# " . $ytids{$id} . "\n" . $id . "\n";
  139. }
  140. close($fh);
  141. }
  142. # Cleanup
  143. exit(0);