critrole.pl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 $EP_MATCH = qr/^https?\:\/\/[^\/]+\/critical\-role\-episode/i;
  9. my $INI_PATH = `~/bin/video/mediaPath` . '/YouTube/Critical Role (None)/extra_videos.ini';
  10. 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';
  11. my $TIMEOUT = 10;
  12. # Debug
  13. our $DEBUG = 0;
  14. if (defined($ENV{'DEBUG'}) && $ENV{'DEBUG'} =~ /(\d+)/) {
  15. $DEBUG = $1;
  16. }
  17. # Setup LWP
  18. my $ua = LWP::UserAgent->new();
  19. if (!$ua) {
  20. die('Unable to init UA: ' . $! . "\n");
  21. }
  22. $ua->agent($UA_STR);
  23. $ua->timeout($TIMEOUT);
  24. $ua->env_proxy();
  25. # Read the existing INI file
  26. my %old_ytids = ();
  27. {
  28. my $fh = undef();
  29. open($fh, '<', $INI_PATH)
  30. or die('Unable to read ini file: ' . $INI_PATH . ': ' . $! . "\n");
  31. my $url = undef();
  32. while (<$fh>) {
  33. if (/^\s*#\s*(https?\:\/\/[^\s]+)/i) {
  34. $url = $1;
  35. } elsif (/([\w\-]+)/) {
  36. if (!$url) {
  37. $url = 'Unknown';
  38. }
  39. $old_ytids{$1} = $url;
  40. $url = undef();
  41. if ($DEBUG > 1) {
  42. print STDERR 'Found existing YTID: ' . $1 . ' # ' . $old_ytids{$1} . "\n";
  43. }
  44. }
  45. }
  46. close($fh);
  47. }
  48. # Use the top-level page to find episode-specific pages
  49. my @episodes = ();
  50. {
  51. my $response = $ua->get($TOP_URL);
  52. if (!$response->is_success()) {
  53. die(basename($0) . ": Unable to fetch top-level page\n");
  54. }
  55. foreach my $link (split(/\<a\s+/, $response->decoded_content())) {
  56. if ($link =~ /\s+href=\"([^\"]+)\"/) {
  57. my $href = $1;
  58. if ($href =~ $EP_MATCH) {
  59. push(@episodes, $href);
  60. }
  61. }
  62. }
  63. }
  64. if ($DEBUG) {
  65. print STDERR 'Episode URLs (' . scalar(@episodes) . "):\n\t" . join("\n\t", @episodes) . "\n";
  66. }
  67. # Find each episode's YTID
  68. my %ytids = ();
  69. {
  70. foreach my $url (@episodes) {
  71. my $response = $ua->get($url);
  72. if (!$response->is_success()) {
  73. die(basename($0) . ': Unable to fetch episode page: ' . $url . "\n");
  74. }
  75. if ($response->decoded_content() =~ /data-ytid=\"([^\"]+)\"/) {
  76. $ytids{$1} = $url;
  77. }
  78. }
  79. }
  80. if ($DEBUG) {
  81. print STDERR 'YouTube IDs (' . scalar(keys(%ytids)) . "):\n\t" . join("\n\t", keys(%ytids)) . "\n";
  82. }
  83. # Drop known episodes
  84. {
  85. foreach my $id (keys(%ytids)) {
  86. if (exists($old_ytids{$id})) {
  87. if ($DEBUG > 1) {
  88. print 'Ignoring known episode: ' . $id . ' # ' . $old_ytids{$id} . "\n";
  89. }
  90. delete($ytids{$id});
  91. }
  92. }
  93. }
  94. # Update the INI file
  95. {
  96. my $fh = undef();
  97. open($fh, '>>', $INI_PATH)
  98. or die('Unable to write ini file: ' . $INI_PATH . ': ' . $! . "\n");
  99. foreach my $id (keys(%ytids)) {
  100. if ($DEBUG) {
  101. print 'Adding episode: ' . $id . ' # ' . $ytids{$id} . "\n";
  102. }
  103. print $fh "\n# " . $ytids{$id} . "\n" . $id . "\n";
  104. }
  105. close($fh);
  106. }
  107. # Cleanup
  108. exit(0);