Skip to content

Instantly share code, notes, and snippets.

@turizoft
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save turizoft/52e2218a3fb612986079 to your computer and use it in GitHub Desktop.
Save turizoft/52e2218a3fb612986079 to your computer and use it in GitHub Desktop.
Download csv song collection using tinysong
# This script reads a csv file with song name and
# artist name in order to download them.
# This csv can be generated using for example http://groovebackup.com/
# if your collection is hosted on Grooveshark.
# Before running ensure watir-webdriver gems is installed.
# For windows you will also need Chromedriver.exe to be in your PATH.
# Finally the extension GroovesharkDownloader is required.
# Note that this technique is intrusive!
# You can't use your pc while its working since
# it opens and closes a lot of windows.
require 'csv'
require 'watir-webdriver'
# Option 1: (Uncomment) Provide a csv [song_name, song_author] (from any source)
# csv_path = 'songs.csv'
# csv_text = File.read(csv_path)
# csv = CSV.parse(csv_text, headers: true)
# Option 2: (Default)
# 1: Go to groovebackup.com and authorize the app
# 2: Choose either full collection, favorites or a playlist
# 3: As soon as you click on any of the options it will start to download automatically
browser = Watir::Browser.new(:chrome)
browser.goto('http://groovebackup.com/')
browser.driver.manage.timeouts.implicit_wait = 60 * 60
browser.pre().wait_until_present
csv = CSV.parse(browser.pre().text, headers: true)
browser.close
csv.each do |row|
# Format query and url
query = "#{row[0]} #{row[1]}"
url = "http://www.tinysong.com/#/result/#{query}/"
# Start a browser
browser = Watir::Browser.new(:chrome, switches: %w[--load-extension=Extensions/ooblpjoncpjmbncgocjlnannofkjjhnp])
# Set timeout to 2 min (for slower connections try setting a higher value)
browser.driver.manage.timeouts.implicit_wait = 2 * 60
# Download source from search page
browser.goto(url)
begin
# Wait until results are shown
browser.div(id: 'result_wrapper').wait_until_present
# Click first button of results (by default) and wait until song downloads
browser.div(css: '#result_wrapper > ul > li > div.sharesong').wait_until_present
browser.div(css: '#result_wrapper > ul > li > div.sharesong').click
browser.div(css: '#result_wrapper > ul > li > div.sharesong.completed').wait_until_present
rescue
puts "Error while downloading #{row[0]}"
end
sleep(3)
browser.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment