Last active
August 29, 2015 14:19
-
-
Save kpq/f675a4474fb9c7c402e7 to your computer and use it in GitHub Desktop.
a starter scrape file for jasmine
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
require 'open-uri' | |
require 'json' | |
def get_song_info(artist, song) | |
# this fixes some things with spaces for the url. | |
# there's probably a better way | |
query = (artist + ' ' + song).gsub! ' ', '%20' | |
# query = query.gsub! '’', '' | |
# construct a url | |
url = 'https://itunes.apple.com/search?media=music&entity=song&limit=15&term=' + query | |
# get the result from the api and parse it | |
result = JSON.parse(open(url).read) | |
results = result['results'] | |
# we;ll add to this for each result | |
all_the_data = [] | |
# fetch some info for each result on this reponse and save it | |
results.each_with_index do |element,index| | |
release_date = results[index]['releaseDate'] | |
track_name = results[index]['trackName'] | |
artist_name = results[index]['artistName'] | |
genre = results[index]['primaryGenreName'] | |
artist_id = results[index]['artistId'] | |
all_the_data[index] = { | |
"release_date" => release_date, | |
"track_name" => track_name, | |
"artist_name" => artist_name, | |
"genre" => genre, | |
"artist_id" => artist_id | |
} | |
end | |
# return all the data for this reponse | |
return all_the_data | |
end | |
# my data from mr data converter | |
songs = | |
[{"artist"=>"Rihanna","songName"=>"We Found Love"}, | |
{"artist"=>"Flo Rida","songName"=>"Whistle"}]; | |
# the object representing all the data I will ever need | |
all_songs = [] | |
# for all songs in my data, run my function | |
songs.each_with_index do |row, i| | |
this_song = row["songName"] | |
this_artist = row["artist"] | |
this_song_info = get_song_info(this_artist, this_song) | |
all_songs.push(this_song_info) | |
sleep 1 | |
end | |
# output this to terminal | |
puts all_songs | |
# this goes into the terminal | |
# ruby scrape_itunes.rb > whatever2.txt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment