Skip to content

Instantly share code, notes, and snippets.

@shifakhan
Last active December 18, 2015 16:42
Show Gist options
  • Save shifakhan/3f1f9402f16770f367c7 to your computer and use it in GitHub Desktop.
Save shifakhan/3f1f9402f16770f367c7 to your computer and use it in GitHub Desktop.
Ruby Script to automatically download all songs in a list from first youtube search link as mp3 using selenium webdriver and a youtube to mp3 service
Ecouter, Carmen Rizzo
Bruises, Chairlift
Under The Gravel Skies, Charlotte Martin
Stopwatch Hearts (feat. Emily Haines), Delerium
Do You Have a Little Time, Dido
Pony (It's Ok), Erin McCarley
Pitter-Pat, Erin McCarley
I Drive Alone, Esthero
Rafe, Fauxliage
require 'selenium-webdriver'
require "cgi"
profile = Selenium::WebDriver::Firefox::Profile.new
profile["browser.download.folderList"] = 1
profile["browser.helperApps.neverAsk.saveToDisk"] = 'audio/mpeg3,audio/x-mpeg-3,video/mpeg,video/x-mpeg'
driver = Selenium::WebDriver.for :firefox, :profile => profile
driver.manage.timeouts.page_load = 30
#song_list_filepath = Path to file containing names of songs in each line. Foe example:
song_list_filepath = "/home/ubuntu/Desktop/list_of_songs_i_want.txt"
songs = File.read(song_list_filepath).split("\n")
songs.each do |song_name|
begin
query = CGI.escape(song_name)
url = "http://youtube.com/results?search_query="
page = driver.navigate.to (url + query)
ele = driver.find_elements(:xpath, '//a[contains(@href, "watch?")]')[0]
video_url = ele.attribute('href')
driver.navigate.to("http://peggo.co/")
ele = driver.find_element(id: 'search-box')
ele.send_keys(video_url)
# driver.find_element(id: 'swap').click unless driver.find_element(id:"id3-title").attribute('value').empty? # Uncomment to swap ID3 tags
driver.find_element(id: 'record-audio').click
# Uncomment for very slow internet speeds
# sleep 5
# driver.find_element(id: 'record-audio').click if driver.find_element(id: 'record-audio').attribute('class') != 'recording'
rescue
p e.message
p song_name
sleep 60
next
end
end
driver.quit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment