#!/usr/bin/ruby # # This is a ruby adaptation of bashpodder.shell By Linc 10/1/2004 # Adaptation by sgbirch@imsmail.org licenced under the GPL. # # This program expects to be run in $HOME/rubypodder. If you need to # run it slesewhere please change the definition of RUNDIR below. # # If you want to see what the program is doing, run it like this: # # ruby -d rubypodder.rb # # Find the latest bashpodder script at: # # http://linc.homeunix.org:8080/scripts/bashpodder # # Last bashpodder revision 07/01/2005 - Many Contributers! # If you use this and have made improvements or have comments # drop me an email at linc dot fessenden at gmail dot com # I'd appreciate it! require 'fileutils' # # Constants # RUNDIR = ENV['HOME'] + '/rubypodder' LOGFILE = 'rubypodder.log' CONFFILE = 'bp.conf' OUTDIR = 'podder' puts "RUNDIR: #{RUNDIR}" if $DEBUG puts "LOGFILE: #{LOGFILE}" if $DEBUG puts "CONFFILE: #{CONFFILE}" if $DEBUG puts "OUTDIR: #{OUTDIR}" if $DEBUG # # Switch to home dir so crontab friendly # puts "Changing working directory to #{RUNDIR}" if $DEBUG FileUtils.cd RUNDIR # # Read the list of files already retrieved from the # file pointed to by LOGFILE (usually rubypodder.log). # The list is cleaned up (lf removed) and placed in # the variable 'retrieved'. # retrieved = [] if File.exist? LOGFILE retrieved = open(LOGFILE) do |io| retrieved = io.readlines retrieved.each {|x| x.chomp!} end end # # Create a new sequence number used as a prefix for each sound file. # seq = retrieved.length puts "Next sequence number: #{seq}" if $DEBUG # # The next section performs the black magic taken from bashpodder. # Each feed listed in bp.conf is visited and a list of published # ogg/mp3 files is created in the 'files' variable. # files = [] begin open(CONFFILE) do |f| f.each_line do |podcast| podcast.chomp! puts "Checking site: #{podcast}" if $DEBUG `wget -q #{podcast} -O - >temp.xml` `xsltproc parse_enclosure.xsl temp.xml >temp.fil 2>/dev/null` if $? != 0 system %q!tr '\r' '\n' temp.fil! end open("temp.fil") do |io| io.readlines.each { |f| files << f.chomp } end end end ensure FileUtils.rm_f 'temp.fil' FileUtils.rm_f 'temp.xml' end files = files.sort.uniq print "Candidates: #{files.length}\n" if $DEBUG # # Remove from 'files' all files previously downloaded and # create the list of new sound files to download (fetchfiles). # fetchfiles = files.reject { |url| retrieved.member?(url) } puts "Files to fetch: #{fetchfiles.length}" if $DEBUG # # Make sure the output directory exists OUTDIR (podder) # if not File.exists?(OUTDIR) puts "Creating #{OUTDIR}" if $DEBUG FileUtils.mkdir(OUTDIR) end # # Retrieve the sound files. Update the logfile after each wget. # fetchfiles.each do |url| url.chomp! outfilename = 'podder/' + seq.to_s + '-' + url.split("/")[-1] begin cmd = "wget -q -O #{outfilename} \"#{url}\"" puts "#{cmd}" if $DEBUG print "outfilename: #{outfilename}\n" `#{cmd}` if $? == 0 retrieved << url seq += 1 # Update of logfile. This is done via # a temp file so we don't end up losing data # if the program is aborted in the middle. FileUtils.rm_f LOGFILE + '.tmp' open(LOGFILE + '.tmp', 'w') do |io| io.write retrieved.join("\n") io.write("\n") end outfilename = '' FileUtils.mv(LOGFILE + '.tmp', LOGFILE) end ensure # Make sure the temp file is removed. FileUtils.rm_f outfilename end end