rss.pl 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 = '';
  60. if (defined($ep->{'title'})) {
  61. $title = $ep->{'title'};
  62. }
  63. my $title = $hs->parse($title);
  64. $hs->eof();
  65. $title =~ s/^\s+//;
  66. $title =~ s/\s+$//;
  67. # Description
  68. my $desc = '';
  69. if (defined($ep->{'description'})) {
  70. $desc = $ep->{'description'};
  71. }
  72. my $desc = $hs->parse($desc);
  73. $hs->eof();
  74. $desc =~ s/^\s+//;
  75. $desc =~ s/\s+$//;
  76. # Timestamp
  77. my $time = str2time($ep->{'pubDate'});
  78. # Author
  79. my $author = undef();
  80. if (exists($ep->{'author'})) {
  81. $author = $ep->{'author'};
  82. $author =~ s/^\s+//;
  83. $author =~ s/\s+$//;
  84. }
  85. # Enclosure (i.e. attachment/file) data
  86. my ($url, $ext, $size) = undef();
  87. {
  88. my $enc = $ep->{'enclosure'};
  89. if ($enc && ref($enc) eq 'HASH' && exists($enc->{'url'})) {
  90. $url = $enc->{'url'};
  91. if (exists($enc->{'type'})) {
  92. if ($enc->{'type'} =~ /audio\/mp4/i) {
  93. $ext = 'm4a';
  94. } elsif ($enc->{'type'} =~ /audio\/mpeg/i) {
  95. $ext = 'mp3';
  96. } elsif ($enc->{'type'} =~ /video\//i) {
  97. if ($DEBUG > 1) {
  98. print STDERR 'Skipping video file: ' . $time . "\n";
  99. }
  100. } else {
  101. print STDERR 'Unknown MIME type: ' . $enc->{'type'} . "\n";
  102. }
  103. }
  104. if (exists($enc->{'length'})) {
  105. $size = $enc->{'length'};
  106. }
  107. }
  108. }
  109. # iTunes data, if available (and useful)
  110. my $duration = undef();
  111. if (exists($ep->{'itunes:duration'})) {
  112. if ($ep->{'itunes:duration'} =~ /(\d{1,2})\:(\d{1,2})\:(\d{1,2})/) {
  113. $duration = (3600 * int($1)) + (60 * int($2)) + int($3);
  114. } else {
  115. $duration = int($ep->{'itunes:duration'});
  116. }
  117. }
  118. if (!$desc && exists($ep->{'itunes:summary'})) {
  119. $desc = $ep->{'itunes:summary'};
  120. $desc =~ s/^\s+//;
  121. $desc =~ s/\s+$//;
  122. }
  123. # Collect extracted, cleaned data
  124. if ($title && $desc && $time && $url && $ext) {
  125. my %tmp = (
  126. 'title' => $title,
  127. 'description' => $desc,
  128. 'time' => $time,
  129. 'url' => $url,
  130. 'ext' => $ext,
  131. 'size' => $size,
  132. 'duration' => $duration,
  133. 'author' => $author,
  134. );
  135. $episodes{$time} = \%tmp;
  136. } elsif ($DEBUG) {
  137. print STDERR 'Skipping incomplete entry: ' . $title . ': ' . $time . "\n";
  138. }
  139. }
  140. # Check for existing files
  141. opendir(my $fh, $OUT_DIR)
  142. or die('Unable to open output directory (' . $! . '): ' . $OUT_DIR . "\n");
  143. while (my $file = readdir($fh)) {
  144. my $path = $OUT_DIR . '/' . $file;
  145. if (-d $path) {
  146. next;
  147. }
  148. if ($file =~ /^(.*)\s+\((\d{7,})\)\.\w{3}$/) {
  149. my $title = $1;
  150. my $time = $2;
  151. if (exists($episodes{$time})) {
  152. if (exists($episodes{$time}->{'size'}) && $episodes{$time}->{'size'}) {
  153. (undef(), undef(), undef(), undef(), undef(), undef(), undef(), my $size) = stat($path);
  154. my $ratio = ($size / $episodes{$time}->{'size'});
  155. if ($ratio < (1 - $ERR_RATIO) || $ratio > (1 + $ERR_RATIO)) {
  156. print STDERR 'Invalid output file size (' . $size . '/' . $episodes{$time}->{'size'} . '): ' . $file . "\n";
  157. }
  158. }
  159. $have{$time} = 1;
  160. $episodes{$time}->{'title'} = $title;
  161. }
  162. if ($DEBUG) {
  163. print STDERR 'Found file: ' . $file . "\n";
  164. }
  165. } elsif ($DEBUG > 1) {
  166. print STDERR 'Skipping file: ' . $file . "\n";
  167. }
  168. }
  169. closedir($fh);
  170. # Decide what we want to download
  171. foreach my $time (keys(%episodes)) {
  172. if (!exists($have{$time})) {
  173. push(@need, $time);
  174. if ($DEBUG > 1) {
  175. print STDERR 'Will fetch: ' . $time . "\n";
  176. }
  177. }
  178. }
  179. # Apply naming rules
  180. if (-e $RULE_FILE) {
  181. if ($DEBUG > 1) {
  182. print STDERR 'Importing local rules from: ' . $RULE_FILE . "\n";
  183. }
  184. require $RULE_FILE;
  185. localRules(\%episodes);
  186. } else {
  187. foreach my $time (sort(keys(%episodes))) {
  188. $episodes{$time}->{'title'} = time2str('%Y-%m-%d', $time) . ' - ' . $episodes{$time}->{'title'};
  189. }
  190. }
  191. foreach my $time (keys(%episodes)) {
  192. $episodes{$time}->{'title'} =~ s/\s*\:\s*/ - /g;
  193. $episodes{$time}->{'title'} =~ s/\s+/ /g;
  194. $episodes{$time}->{'title'} =~ s/\"/\'/g;
  195. $episodes{$time}->{'title'} =~ s/[^\w\s\,\-\.\!\'\(\)\#\&\@]//g;
  196. }
  197. # Fetch needed files, with basic validation
  198. my $dlCount = 0;
  199. foreach my $time (sort(@need)) {
  200. if ($dlCount >= $DL_LIMIT) {
  201. die('Download limit (' . $DL_LIMIT . ') reached: ' . $SERIES . "\n");
  202. }
  203. # Build a file path
  204. my $ep = $episodes{$time};
  205. my $file = $OUT_DIR . '/' . $ep->{'title'} . ' (' . int($time) . ').' . $ep->{'ext'};
  206. if (-e $file) {
  207. die('Unexpected output file: ' . $file . "\n");
  208. }
  209. # Debug
  210. if ($DEBUG) {
  211. print STDERR 'Fetching (' . $time . '): ' . $ep->{'title'} . ' => ' . $ep->{'url'} . "\n";
  212. }
  213. # Fetch
  214. my $code = getstore($ep->{'url'}, $file);
  215. $dlCount++;
  216. # Validate
  217. my $err = undef();
  218. if ($code != 200) {
  219. $err = 'Invalid HTTP response: ' . $code;
  220. goto OUT;
  221. }
  222. if (!-e $file) {
  223. $err = 'No output file: ' . $file;
  224. goto OUT;
  225. }
  226. if ($ep->{'size'}) {
  227. (undef(), undef(), undef(), undef(), undef(), undef(), undef(), my $size) = stat($file);
  228. my $ratio = $size / $ep->{'size'};
  229. if ($ratio < (1 - $ERR_RATIO) || $ratio > (1 + $ERR_RATIO)) {
  230. $err = 'Invalid download size (' . $size . '/' . $ep->{'size'} . '): ' . $file;
  231. goto OUT;
  232. }
  233. }
  234. # Set MP3/M4A tags
  235. if ($ep->{'ext'} eq 'mp3') {
  236. # Parse the MP3
  237. my $mp3 = MP3::Tag->new($file);
  238. if (!$mp3) {
  239. $err = 'Unable to parse MP3 tags in: ' . $file . "\n";
  240. goto OUT;
  241. }
  242. # Delete any ID3v1 tags
  243. if (exists($mp3->{'ID3v1'})) {
  244. print STDERR "Removing ID3v1 tag\n";
  245. $mp3->{'ID3v1'}->remove_tag();
  246. print STDERR "Removed?\n";
  247. }
  248. # Find or create the ID3v2 tag
  249. my $tags = undef();
  250. if (exists($mp3->{'ID3v2'})) {
  251. $tags = $mp3->{'ID3v2'};
  252. } else {
  253. $tags = $mp3->new_tag('ID3v2');
  254. }
  255. # Update tags
  256. $tags->title($ep->{'title'});
  257. $tags->year(time2str('%Y', $ep->{'time'}));
  258. $tags->comment($ep->{'description'});
  259. $tags->album($SERIES);
  260. $tags->artist($SERIES);
  261. $tags->genre($GENRE);
  262. # Save
  263. if (!$tags->write_tag()) {
  264. $err = 'Unable to update MP3 tags in: ' . $file . "\n";
  265. goto OUT;
  266. }
  267. $mp3->close();
  268. } else {
  269. print STDERR 'Tag munging not available for ' . $ep->{'ext'} . ' files: ' . $file . "\n";
  270. }
  271. OUT:
  272. # Cleanup
  273. if (defined($err)) {
  274. print STDERR 'Unable to download ' . $ep->{'url'} . ': ' . $err . "\n";
  275. if (-e $file) {
  276. unlink($file);
  277. }
  278. }
  279. }