require 'fileutils' require 'date/format' require 'date' require 'time' require 'net/http' def print_last_x_weekdays(filepath, copypath, x) d = Date.today() counter = 1 badcounter = 0 filename = 'index.rss' f = File.new(filepath + filename, "w") f.puts '' f.puts '' f.puts ' ' f.puts ' Unofficial Don and Mike Show RSS Feed' f.puts ' http://www.donandmikewebsite.com' f.puts ' Unofficial Don and Mike Show rss feed containing daily recap and links to audio recap' f.puts ' en-us' f.puts " #{d.strftime('%a, %d %b %Y 23:00:00 CST')}" f.puts " #{d.strftime('%a, %d %b %Y 23:00:00 CST')}" #Go through until we get a correct number of #days or the number of bad hits exceeds limit while(counter <= x and badcounter < 5) #If the date is a weekday if(d.wday > 0 and d.wday < 6) address = "http://www.donandmikewebsite.com/archive/DAILY_LOG-archive-#{d.mday().to_s}-#{d.mon().to_s}-#{d.strftime('%Y')}.shtml" #address = "http://www.donandmikewebsite.com/Product/#{d.strftime('%m-%d-%y-%m%d%y')}.html" audio = "http://easylink.playstream.com/donandmikewebsite/daily-segment/#{d.strftime('%m-%d-%y')}.wax"; #Hit the URL and see if anything is there url = URI.parse(address) req = Net::HTTP::Get.new(url.path) res = Net::HTTP.start(url.host, url.port) {|http| http.request(req) } #200 = HTTP OK puts res.code + " - " + address if(res.code=="200") counter +=1 body = res.body reg = Regexp.new('(.+)', Regexp::MULTILINE) body = body[reg] #body = body[/(
.+<\/div>)/] body = body.gsub(//, ' ') body = body.gsub(//, '') #Replace EM with apostrophe (encoding issue I presume) body = body.gsub(/\x19/, "'") #get rid of any pesky non-ascii characters body = body.gsub(/[^\x20-\x7F]/, ' ') #Need to see if we have a file that corresponds to this day, #if we do, we need to compare it to the content we retrieved #to see if anything has changed. If so, we need to update the #link to be different so that feed readers will notice the change does_file_exist = File.exist?(filepath + d.strftime('%m-%d-%y')) if(does_file_exist) #Fetch our existing file contents existing_file = File.new(filepath + d.strftime('%m-%d-%y')) existing_contents = "" while (existing_file.gets != nil) existing_contents += $_ end existing_contents = existing_contents.chomp #Compare to what we fetched from the web #If different update our file on disk #along with the address variable (this is what the #feedreaders will key off of to recognize a change if(!body.eql?(existing_contents)) puts "File #{filepath + d.strftime('%m-%d-%y')} does not match content from web, overwriting" existing_file = File.new(filepath + d.strftime('%m-%d-%y'), 'w') existing_file.puts body address = address + '#' + Time.now.strftime('%H%M%S') end existing_file.close() else existing_file = File.new(filepath + d.strftime('%m-%d-%y'), 'w') existing_file.puts body end f.puts ' ' f.puts " Show Recap for #{d.strftime('%A, %B %d, %Y')}" f.puts " #{address}" f.puts ' ' #uses regex on body variable to remove extraneous HTML content f.puts "

' #adding in some show related links (Listen Live, Buy Swag, #Eat at Hank's Look-Around Cafe) to the end of each item f.puts ' Listen Live 3-7 EST  |  ' f.puts ' Don and Mike Podcasts on iTunes  |  ' f.puts ' Buy Don and Mike Swag  |  ' f.puts ' Eat at O\'Meara\'s' f.puts ' ]]>' f.puts '
' f.puts ' Don and Mike' f.puts " #{d.strftime('%a, %d %b %Y 23:00:00 CST')}" f.puts " #{address}" #See if we have an audio feed for this day audiourl = URI.parse(audio) audiores = Net::HTTP.start(audiourl.host, audiourl.port) {|http| http.request_head(audio) } #Add RSS enclosure if we found an audio feed #if(audiores.code=="200") # f.puts " " #end f.puts '
' elsif badcounter+=1 end end #move back one day d = d-1 end f.puts ' ' f.puts '' f.close() #Copy our completed file to its final location FileUtils.copy(filepath + filename, copypath + filename) end #call our method with command line arguments #ARGV[0]: file path to use for file construction #ARGV[1]: file path to copy finished file to #ARGV[2]: number of days back to fetch into file print_last_x_weekdays(ARGV[0], ARGV[1], ARGV[2].to_i())