Last active
          August 5, 2017 10:00 
        
      - 
      
- 
        Save kenjoe41/d047d764aeeb35b1064bc5cf2b79cc58 to your computer and use it in GitHub Desktop. 
    batch downloads codepen podcasts. Change latest_podcast variable when a new one is out. Quick script for my current needs else a `wget` command will do for a one episode
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #!/usr/bin/ruby | |
| require 'fileutils' | |
| require 'rubygems' | |
| require 'typhoeus' | |
| #function to create a directory | |
| def create_a_directory(dir_name) | |
| if dir_name | |
| # dir_name was specified, ensure it is created and writable. | |
| unless File.exist?(dir_name) | |
| begin | |
| FileUtils.mkdir_p(dir_name) | |
| puts "Just made the following dir #{dir_name}" | |
| rescue Errno::EACCES => e | |
| abort "Failed to create #{dir_name}: #{e.message}" | |
| end | |
| end | |
| end | |
| end | |
| dir_name = "path/to/podcast/download/folder" | |
| #create directory if it doesn't exist | |
| create_a_directory(dir_name) | |
| #latest episode to Download | |
| latest_episode = 138 | |
| #variable for number of podcasts downloaded | |
| podcasts = 0 | |
| #iterate a range of podcasts to download | |
| for i in 1..latest_episode | |
| # ensure i is 3 digits n pad it with 00 is not. | |
| #thank you rindolf in freenode ##programing IRC | |
| i = sprintf('%03d', i) | |
| #check if file exists | |
| if File.exist? "#{dir_name}/#{i}" | |
| next | |
| end | |
| #check for new range that has a different URL | |
| if i.to_i >= 124 | |
| i = "codepen-" << i | |
| end | |
| puts "Downloading episode #{i}" | |
| downloaded_file = File.open "#{dir_name}/#{i}", 'wb' | |
| request = Typhoeus::Request.new("codepen-podcast.s3.amazonaws.com/#{i}.mp3") | |
| request.on_headers do |response| | |
| if response.code != 200 | |
| raise "Request failed" | |
| end | |
| end | |
| request.on_body do |chunk| | |
| downloaded_file.write(chunk) | |
| end | |
| request.on_complete do |response| | |
| downloaded_file.close | |
| # Note that response.body is "" | |
| end | |
| request.run | |
| #increment number of completed podcast downloads | |
| podcasts += 1 | |
| end | |
| puts "Done. Downloaded #{podcasts} podcasts" | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment