Skip to content

Instantly share code, notes, and snippets.

@arathunku
Last active December 15, 2015 12:19
Show Gist options
  • Save arathunku/5259059 to your computer and use it in GitHub Desktop.
Save arathunku/5259059 to your computer and use it in GitHub Desktop.
Code to parse Polskie Radio Lista Przebojów site and output spotify urls to make a playlist. Unfortunately, creating playlist in spotify has to be done by hand now.
require 'nokogiri'
require 'open-uri'
require 'json'
require './handleData.rb'
def lista_przebojow_trojki(url=nil)
site = Nokogiri::HTML(open(url))
playlist = []
site.css('#bigList tr').each do |song|
song.css('.aT a').each_slice(2) do |s, t|
playlist << {artist: s.content, title: t.content}
end
end
site.css('#bigListRest tr').each do |song|
song.css('.aT a').each_slice(2) do |s, t|
playlist << {artist: s.content, title: t.content}
end
end
site.css('#bigListRest_Unhappy tr').each do |song|
song.css('.aT a').each_slice(2) do |s, t|
playlist << {artist: s.content, title: t.content}
end
end
playlist
end
def proper(s)
s.gsub!('&', 'and') if s.include?('&')
s.split(' ').join('+')
end
#Methods has to receive playlist as array containing hasheh with keys:
#artist and title
def spotify_search(playlist=[], handler)
playlist.each_with_index do |p, i|
artist = proper(p[:artist])
title = proper(p[:title])
handler.output(nil, "#{i} #{p[:artist]} - #{p[:title]}")
search = {'info'=>{}}
count = 1
begin
until search['info'].include?'num_results' do
url = "http://ws.spotify.com/search/1/track.json?q=#{artist}+#{title}"
uri = open(URI.parse(URI.encode(url.strip))).read
search = JSON.parse(uri)
sleep(0.2)
puts count
count += 1
end
rescue OpenURI::HTTPError => ex
handler.output(-9999, "#{url}")
end
if search["info"].include?('num_results') && search["info"]["num_results"] != 0
handler.output(nil, "#{search}")
handler.output(1, "#{search["tracks"][0]["href"]}")
handler.output(nil, "y")
else
handler.output(-1, "#{p[:artist]} - #{p[:title]}")
end
end
end
handler = HandleData.new()
playlist = lista_przebojow_trojki("http://lp3.polskieradio.pl/notowania/?rok=2013&numer=1628")
spotify_search(playlist, handler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment