From 62f666dd4efb78b981785d12e813cc13acee9a3c Mon Sep 17 00:00:00 2001 From: Ward Wouts Date: Sun, 16 Apr 2006 19:26:24 +0000 Subject: [PATCH] maak DATE substitutie in filenames mogelijk (dit omdat pennradio idiote filenames heeft) --- getdistorted/getdistorted.rb | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/getdistorted/getdistorted.rb b/getdistorted/getdistorted.rb index 1328547..1c28630 100755 --- a/getdistorted/getdistorted.rb +++ b/getdistorted/getdistorted.rb @@ -42,6 +42,14 @@ # delete files that aren't in the rss anylonger "delete" => true, }, + "pennradio" => { + "rss" => 'http://www.923freefm.com/pages/podcast/80.rss', + # %%DATE%% is a magic word here, gets replaced by the pubdate in + # YYYYMMDD format. Won't work if there is no pubDate in the rss feed + "rename" => [ /^/, '%%DATE%%-' ], + "savedir" => '/Users/ward/Private/mp3/[books]/PennRadio', + "linkdir" => '/Users/ward/ipod_sync/[books]/PennRadio', + }, } ######################################## @@ -50,6 +58,7 @@ require 'net/http' require 'uri' require 'rexml/document' +require 'date' def fetch(uri_str, limit = 10) # You should choose better exception. @@ -74,13 +83,25 @@ def deleteold(podcast) } end -def getenclosure(podcast, enclosure) - if ! enclosure.attribute("url").nil? +def getenclosure(podcast, item) + enclosure = item.get_elements("enclosure")[0] + pubdate = item.get_elements("pubDate")[0] + date = nil + if ! enclosure.nil? + end + if ! pubdate.nil? + date = Date.parse(pubdate.text).strftime("%Y%m%d") + end + if ! enclosure.nil? && ! enclosure.attribute("url").nil? cast = enclosure.attribute("url").to_s.dup filename = cast.dup filename.sub!(/^.*\//, "") if @podcasts[podcast]["rename"] - filename.sub!(@podcasts[podcast]["rename"][0], @podcasts[podcast]["rename"][1]) + replacement = @podcasts[podcast]["rename"][1].dup + if replacement.match(/%%DATE%%/) + replacement.sub!(/%%DATE%%/, date) + end + filename.sub!(@podcasts[podcast]["rename"][0], replacement) end @filelist[filename] = true if ! File.exists?("#{@podcasts[podcast]["savedir"]}/#{filename}") @@ -110,8 +131,8 @@ for podcast in @podcasts.keys.sort @filelist = {} xmldoc = REXML::Document.new(res.body) - xmldoc.elements.each("rss/channel/*/enclosure") {|enclosure| - files.push getenclosure(podcast, enclosure) + xmldoc.elements.each("rss/channel/item") {|item| + files.push getenclosure(podcast, item) } if @podcasts[podcast]["delete"] deleteold(podcast)