critrole.pl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. # Setup LWP
  19. my $ua = LWP::UserAgent->new();
  20. if (!$ua) {
  21. die('Unable to init UA: ' . $! . "\n");
  22. }
  23. $ua->agent($UA_STR);
  24. $ua->timeout($TIMEOUT);
  25. $ua->env_proxy();
  26. # Read the existing INI file
  27. my %old_ytids = ();
  28. {
  29. my $fh = undef();
  30. open($fh, '<', $INI_PATH)
  31. or die('Unable to read ini file: ' . $INI_PATH . ': ' . $! . "\n");
  32. my $url = undef();
  33. while (<$fh>) {
  34. if (/^\s*#\s*(https?\:\/\/[^\s]+)/i) {
  35. $url = $1;
  36. } elsif (/([\w\-]+)/) {
  37. if (!$url) {
  38. $url = 'Unknown';
  39. }
  40. $old_ytids{$1} = $url;
  41. $url = undef();
  42. if ($DEBUG > 1) {
  43. print STDERR 'Found existing YTID: ' . $1 . ' # ' . $old_ytids{$1} . "\n";
  44. }
  45. }
  46. }
  47. close($fh);
  48. }
  49. # Use the top-level page to find episode-specific pages
  50. my @episodes = ();
  51. {
  52. my $url = $TOP_URL;
  53. NEXT_PAGE: {
  54. if ($DEBUG) {
  55. print STDERR 'Loading top page: ' . $url . "\n";
  56. }
  57. my $response = $ua->get($url);
  58. if (!$response->is_success()) {
  59. die(basename($0) . ": Unable to fetch top-level page\n");
  60. }
  61. foreach my $link (split(/\<a\s+/, $response->decoded_content())) {
  62. if ($link =~ /\s+href=\"([^\"]+)\"/) {
  63. my $href = $1;
  64. if ($href =~ $EP_MATCH) {
  65. push(@episodes, $href);
  66. }
  67. }
  68. }
  69. if ($response->decoded_content() =~ $PG_MATCH) {
  70. $url = $1;
  71. redo NEXT_PAGE;
  72. }
  73. }
  74. }
  75. if ($DEBUG) {
  76. print STDERR 'Episode URLs (' . scalar(@episodes) . "):\n\t" . join("\n\t", @episodes) . "\n";
  77. }
  78. # Find each episode's YTID
  79. my %ytids = ();
  80. {
  81. foreach my $url (@episodes) {
  82. my $response = $ua->get($url);
  83. if (!$response->is_success()) {
  84. die(basename($0) . ': Unable to fetch episode page: ' . $url . "\n");
  85. }
  86. if ($response->decoded_content() =~ /data-ytid=\"([^\"]+)\"/) {
  87. $ytids{$1} = $url;
  88. } else {
  89. print STDERR 'No YTID found on: ' .$url . "\n";
  90. if ($DEBUG > 1) {
  91. print STDERR "\tPage content:\n" . $response->decoded_content() . "\n\n";
  92. }
  93. }
  94. }
  95. }
  96. if ($DEBUG) {
  97. print STDERR 'YouTube IDs (' . scalar(keys(%ytids)) . "):\n\t" . join("\n\t", keys(%ytids)) . "\n";
  98. }
  99. # Drop known episodes
  100. {
  101. foreach my $id (keys(%ytids)) {
  102. if (exists($old_ytids{$id})) {
  103. if ($DEBUG > 1) {
  104. print 'Ignoring known episode: ' . $id . ' # ' . $old_ytids{$id} . "\n";
  105. }
  106. delete($ytids{$id});
  107. }
  108. }
  109. }
  110. # Update the INI file
  111. {
  112. my $fh = undef();
  113. open($fh, '>>', $INI_PATH)
  114. or die('Unable to write ini file: ' . $INI_PATH . ': ' . $! . "\n");
  115. foreach my $id (keys(%ytids)) {
  116. if ($DEBUG) {
  117. print 'Adding episode: ' . $id . ' # ' . $ytids{$id} . "\n";
  118. }
  119. print $fh "\n# " . $ytids{$id} . "\n" . $id . "\n";
  120. }
  121. close($fh);
  122. }
  123. # Cleanup
  124. exit(0);