rss.pl 6.9 KB

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