Created
April 30, 2013 13:33
-
-
Save mamantoha/5488722 to your computer and use it in GitHub Desktop.
Download Last.fm user's loved tracks from VK.com
This file contains 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
# encoding: utf-8 | |
require 'pp' | |
require 'open-uri' | |
require 'mechanize' | |
require 'lastfm' | |
require 'vkontakte' | |
### Last.fm credentials | |
# | |
lastfm_username = 'xxx' | |
lastfm_password = 'xxx' | |
lastfm_api_key = 'xxxxxxxxxxx' | |
lastfm_api_secret = 'xxxxxxxx' | |
### Vk.com credentials | |
# | |
vk_email = 'username@example.com' | |
vk_password = 'xxx' | |
vk_client_id = 'xxxxx' | |
downloaded_folder = 'audio' | |
### Log in to Last.fm | |
# | |
mech = Mechanize.new do |a| | |
a.user_agent_alias = 'Linux Firefox' | |
a.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
a.follow_meta_refresh = true | |
end | |
login_page = mech.get('https://last.fm/login') | |
login_form = login_page.form_with(action: '/login') | |
login_form.username = lastfm_username | |
login_form.password = lastfm_password | |
page = login_form.submit | |
if page.uri.to_s == 'http://www.last.fm/home' | |
puts "Login successful!" | |
elsif page.uri.to_s == 'https://www.last.fm/login' | |
puts "That doesn't seem right." | |
exit | |
else | |
puts page.uri.to_s | |
exit | |
end | |
### | |
# | |
lastfm = Lastfm.new(lastfm_api_key, lastfm_api_secret) | |
lastfm_token = lastfm.auth.get_token | |
# open 'http://www.last.fm/api/auth/?api_key=xxxxxxxxxxx&token=xxxxxxxx' and grant the application | |
# | |
grantaccess_url = "http://www.last.fm/api/auth/?api_key=#{lastfm_api_key}&token=#{lastfm_token}" | |
grantaccess_page = mech.get(grantaccess_url) | |
grantaccess_form = grantaccess_page.form_with(action: '/api/grantaccess') | |
grantaccess_form.submit | |
lastfm.session = lastfm.auth.get_session(:token => lastfm_token)['key'] | |
tracks = lastfm.user.get_loved_tracks(user: lastfm_username, limit: 10) | |
# Log in in to Vkontakte | |
# | |
vk = Vkontakte::Client.new(vk_client_id) | |
vk.login!(vk_email, vk_password, scope = 'audio') | |
audios = [] | |
tracks.each do |track| | |
str = "#{track['artist']['name']} - #{track['name']}" | |
# Повертає найпопулярніший результат пошуку строки str(серед аудіозаписів) | |
audio = vk.api.audio_search(q: str, sort: 2)[1] | |
audios << audio | |
end | |
### Save files to disk | |
# | |
audios.each do |audio| | |
path = "#{downloaded_folder}/" + "#{audio['artist']} - #{audio['title']}.mp3".tr("/\000", "") | |
unless File.exists?(File.absolute_path(path)) | |
print "Download #{audio['artist']} - #{audio['title']}" | |
File.open(path, 'wb') do |saved_file| | |
open(audio['url'], 'rb') do |read_file| | |
saved_file.write(read_file.read) | |
end | |
end | |
puts " - Done." | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment