rss.pl 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Cwd qw(abs_path);
  5. use MP3::Tag;
  6. use XML::Feed;
  7. use LWP::Simple;
  8. use HTML::Strip;
  9. use Date::Parse;
  10. use Date::Format;
  11. use File::Basename;
  12. # Command-line
  13. my $OUT_DIR = abs_path($ARGV[0]);
  14. if (!$OUT_DIR || !-d $OUT_DIR) {
  15. die('Usage: ' . basename($0) . " output_directory\n");
  16. }
  17. # Parameters
  18. my $GENRE = 'Podcast';
  19. my $SERIES = basename($OUT_DIR);
  20. my $URL_FILE = $OUT_DIR . '/url';
  21. my $RULE_FILE = $OUT_DIR . '/rules.pm';
  22. my $ERR_RATIO = 0.05;
  23. my $DL_LIMIT = 50;
  24. # Init
  25. our $DEBUG = 0;
  26. if (defined($ENV{'DEBUG'}) && $ENV{'DEBUG'} =~ /(\d+)/) {
  27. $DEBUG = $1;
  28. }
  29. my %episodes = ();
  30. my %have = ();
  31. my @need = ();
  32. my $hs = HTML::Strip->new();
  33. # Skip if there is a season_done file
  34. if (-e $OUT_DIR . '/season_done') {
  35. if ($DEBUG) {
  36. print STDERR 'Podcast disabled: ' . $SERIES . "\n";
  37. }
  38. exit(0);
  39. }
  40. # Fetch and analyize the feed
  41. my $feed = undef();
  42. {
  43. open(my $fh, '<', $URL_FILE)
  44. or die('Unable to open feed file: ' . $URL_FILE . "\n");
  45. my $url = <$fh>;
  46. close($fh);
  47. if (!$url || length($url) < 5) {
  48. die('Invalid feed URL: ' . $url . "\n");
  49. }
  50. $feed = XML::Feed->parse(URI->new($url));
  51. if (!$feed) {
  52. die('Unable to parse feed (' . XML::Feed->errstr() . '): ' . $url . "\n");
  53. }
  54. }
  55. # Parse each episode from the feed
  56. for my $entry ($feed->entries()) {
  57. my $ep = $entry->unwrap();
  58. # Title
  59. my $title = $hs->parse($ep->{'title'});
  60. $hs->eof();
  61. $title =~ s/^\s+//;
  62. $title =~ s/\s+$//;
  63. # Description
  64. my $desc = $hs->parse($ep->{'description'});
  65. $hs->eof();
  66. $desc =~ s/^\s+//;
  67. $desc =~ s/\s+$//;
  68. # Timestamp
  69. my $time = str2time($ep->{'pubDate'});
  70. # Author
  71. my $author = undef();
  72. if (exists($ep->{'author'})) {
  73. $author = $ep->{'author'};
  74. $author =~ s/^\s+//;
  75. $author =~ s/\s+$//;
  76. }
  77. # Enclosure (i.e. attachment/file) data
  78. my ($url, $ext, $size) = undef();
  79. {
  80. my $enc = $ep->{'enclosure'};
  81. if ($enc && ref($enc) eq 'HASH' && exists($enc->{'url'})) {
  82. $url = $enc->{'url'};
  83. if (exists($enc->{'type'})) {
  84. if ($enc->{'type'} =~ /audio\/mp4/i) {
  85. $ext = 'm4a';
  86. } elsif ($enc->{'type'} =~ /audio\/mpeg/i) {
  87. $ext = 'mp3';
  88. } elsif ($enc->{'type'} =~ /video\//i) {
  89. if ($DEBUG > 1) {
  90. print STDERR 'Skipping video file: ' . $time . "\n";
  91. }
  92. } else {
  93. print STDERR 'Unknown MIME type: ' . $enc->{'type'} . "\n";
  94. }
  95. }
  96. if (exists($enc->{'length'})) {
  97. $size = $enc->{'length'};
  98. }
  99. }
  100. }
  101. # iTunes data, if available (and useful)
  102. my $duration = undef();
  103. if (exists($ep->{'itunes:duration'})) {
  104. if ($ep->{'itunes:duration'} =~ /(\d{1,2})\:(\d{1,2})\:(\d{1,2})/) {
  105. $duration = (3600 * int($1)) + (60 * int($2)) + int($3);
  106. } else {
  107. $duration = int($ep->{'itunes:duration'});
  108. }
  109. }
  110. if (!$desc && exists($ep->{'itunes:summary'})) {
  111. $desc = $ep->{'itunes:summary'};
  112. $desc =~ s/^\s+//;
  113. $desc =~ s/\s+$//;
  114. }
  115. # Collect extracted, cleaned data
  116. if ($title && $desc && $time && $url && $ext) {
  117. my %tmp = (
  118. 'title' => $title,
  119. 'description' => $desc,
  120. 'time' => $time,
  121. 'url' => $url,
  122. 'ext' => $ext,
  123. 'size' => $size,
  124. 'duration' => $duration,
  125. 'author' => $author,
  126. );
  127. $episodes{$time} = \%tmp;
  128. } elsif ($DEBUG) {
  129. print STDERR 'Skipping incomplete entry: ' . $title . ': ' . $time . "\n";
  130. }
  131. }
  132. # Check for existing files
  133. opendir(my $fh, $OUT_DIR)
  134. or die('Unable to open output directory (' . $! . '): ' . $OUT_DIR . "\n");
  135. while (my $file = readdir($fh)) {
  136. my $path = $OUT_DIR . '/' . $file;
  137. if (-d $path) {
  138. next;
  139. }
  140. if ($file =~ /^(.*)\s+\((\d{7,})\)\.\w{3}$/) {
  141. my $title = $1;
  142. my $time = $2;
  143. if (exists($episodes{$time})) {
  144. if (exists($episodes{$time}->{'size'}) && $episodes{$time}->{'size'}) {
  145. (undef(), undef(), undef(), undef(), undef(), undef(), undef(), my $size) = stat($path);
  146. my $ratio = ($size / $episodes{$time}->{'size'});
  147. if ($ratio < (1 - $ERR_RATIO) || $ratio > (1 + $ERR_RATIO)) {
  148. print STDERR 'Invalid output file size (' . $size . '/' . $episodes{$time}->{'size'} . '): ' . $file . "\n";
  149. }
  150. }
  151. $have{$time} = 1;
  152. $episodes{$time}->{'title'} = $title;
  153. }
  154. if ($DEBUG) {
  155. print STDERR 'Found file: ' . $file . "\n";
  156. }
  157. } elsif ($DEBUG > 1) {
  158. print STDERR 'Skipping file: ' . $file . "\n";
  159. }
  160. }
  161. closedir($fh);
  162. # Decide what we want to download
  163. foreach my $time (keys(%episodes)) {
  164. if (!exists($have{$time})) {
  165. push(@need, $time);
  166. if ($DEBUG > 1) {
  167. print STDERR 'Will fetch: ' . $time . "\n";
  168. }
  169. }
  170. }
  171. # Apply naming rules
  172. if (-e $RULE_FILE) {
  173. if ($DEBUG > 1) {
  174. print STDERR 'Importing local rules from: ' . $RULE_FILE . "\n";
  175. }
  176. require $RULE_FILE;
  177. localRules(\%episodes);
  178. } else {
  179. foreach my $time (sort(keys(%episodes))) {
  180. $episodes{$time}->{'title'} = time2str('%Y-%m-%d', $time) . ' - ' . $episodes{$time}->{'title'};
  181. }
  182. }
  183. foreach my $time (keys(%episodes)) {
  184. $episodes{$time}->{'title'} =~ s/\s*\:\s*/ - /g;
  185. $episodes{$time}->{'title'} =~ s/\s+/ /g;
  186. $episodes{$time}->{'title'} =~ s/\"/\'/g;
  187. $episodes{$time}->{'title'} =~ s/[^\w\s\,\-\.\!\'\(\)\#\&\@]//g;
  188. }
  189. # Fetch needed files, with basic validation
  190. my $dlCount = 0;
  191. foreach my $time (sort(@need)) {
  192. if ($dlCount >= $DL_LIMIT) {
  193. die('Download limit (' . $DL_LIMIT . ') reached: ' . $SERIES . "\n");
  194. }
  195. # Build a file path
  196. my $ep = $episodes{$time};
  197. my $file = $OUT_DIR . '/' . $ep->{'title'} . ' (' . int($time) . ').' . $ep->{'ext'};
  198. if (-e $file) {
  199. die('Unexpected output file: ' . $file . "\n");
  200. }
  201. # Debug
  202. if ($DEBUG) {
  203. print STDERR 'Fetching (' . $time . '): ' . $ep->{'title'} . ' => ' . $ep->{'url'} . "\n";
  204. }
  205. # Fetch
  206. my $code = getstore($ep->{'url'}, $file);
  207. $dlCount++;
  208. # Validate
  209. my $err = undef();
  210. if ($code != 200) {
  211. $err = 'Invalid HTTP response: ' . $code;
  212. goto OUT;
  213. }
  214. if (!-e $file) {
  215. $err = 'No output file: ' . $file;
  216. goto OUT;
  217. }
  218. if ($ep->{'size'}) {
  219. (undef(), undef(), undef(), undef(), undef(), undef(), undef(), my $size) = stat($file);
  220. my $ratio = $size / $ep->{'size'};
  221. if ($ratio < (1 - $ERR_RATIO) || $ratio > (1 + $ERR_RATIO)) {
  222. $err = 'Invalid download size (' . $size . '/' . $ep->{'size'} . '): ' . $file;
  223. goto OUT;
  224. }
  225. }
  226. # Set MP3/M4A tags
  227. if ($ep->{'ext'} eq 'mp3') {
  228. # Parse the MP3
  229. my $mp3 = MP3::Tag->new($file);
  230. if (!$mp3) {
  231. $err = 'Unable to parse MP3 tags in: ' . $file . "\n";
  232. goto OUT;
  233. }
  234. # Delete any ID3v1 tags
  235. if (exists($mp3->{'ID3v1'})) {
  236. print STDERR "Removing ID3v1 tag\n";
  237. $mp3->{'ID3v1'}->remove_tag();
  238. print STDERR "Removed?\n";
  239. }
  240. # Find or create the ID3v2 tag
  241. my $tags = undef();
  242. if (exists($mp3->{'ID3v2'})) {
  243. $tags = $mp3->{'ID3v2'};
  244. } else {
  245. $tags = $mp3->new_tag('ID3v2');
  246. }
  247. # Update tags
  248. $tags->title($ep->{'title'});
  249. $tags->year(time2str('%Y', $ep->{'time'}));
  250. $tags->comment($ep->{'description'});
  251. $tags->album($SERIES);
  252. $tags->artist($SERIES);
  253. $tags->genre($GENRE);
  254. # Save
  255. if (!$tags->write_tag()) {
  256. $err = 'Unable to update MP3 tags in: ' . $file . "\n";
  257. goto OUT;
  258. }
  259. $mp3->close();
  260. } else {
  261. print STDERR 'Tag munging not available for ' . $ep->{'ext'} . ' files: ' . $file . "\n";
  262. }
  263. OUT:
  264. # Cleanup
  265. if (defined($err)) {
  266. print STDERR 'Unable to download ' . $ep->{'url'} . ': ' . $err . "\n";
  267. if (-e $file) {
  268. unlink($file);
  269. }
  270. }
  271. }