Search works so-so. Done for now. Close #45, close #56

This commit is contained in:
magnolia-fan
2011-06-18 05:09:07 +04:00
parent 24ca64a945
commit c9ea9c6a27
10 changed files with 167 additions and 79 deletions
+9 -10
View File
@@ -1,4 +1,3 @@
require 'uri'
require 'open-uri'
class ArtistController < ApplicationController
@@ -8,15 +7,15 @@ class ArtistController < ApplicationController
artist = Artist.find_by_name(name)
unless artist
results = ArtistController.musicBrainzSearch(name)
if results[0].downcase == name.downcase and results[0] != name
render :json => {correct: results[0]}
return
elsif results[0] == name
if results[0] == name
ImportController.importArtist(name)
render :json => {status: 'loading'}
render :json => {status: 'loaded'}
return
elsif (results[0].downcase == name.downcase or results[0].downcase == 'the '+ name.downcase) and results[0] != name
render :json => {status: 'corrected', page: results[0]}
return
else
render :json => {suggestions: results}
render :json => {status: 'suggestions', values: results}
return
end
end
@@ -71,12 +70,12 @@ class ArtistController < ApplicationController
def self.musicBrainzSearch(query)
begin
response = ActiveSupport::JSON.decode(open('http://search.test.musicbrainz.org/ws/2/artist/?fmt=json&query='+ URI.escape(query).gsub(/\&/, '%26').gsub(/\?/, '%3F') +'~').read)
response = ActiveSupport::JSON.decode(open('http://search.test.musicbrainz.org/ws/2/artist/?fmt=json&query='+ URI.escape(query).gsub(/\&/, '%26').gsub(/\?/, '%3F') +'~&limit=100').read)
artists = []
response['artist-list']['artist'].each do |artist|
artists << artist['name']
artists << artist['name'] unless artist['type'] == 'unknown'
end
return artists
artists.take(10)
rescue
return {}
end
+17 -8
View File
@@ -5,21 +5,28 @@ class ImportController < ApplicationController
# Get artist info
artist_mb_xml = open(
'http://musicbrainz.org/ws/2/artist/?query='+ URI.escape(name).gsub(/\&/, '%26').gsub(/\?/, '%3F') +'&limit=1',
'http://musicbrainz.org/ws/2/artist/?query='+ URI.escape(name).gsub(/\&/, '%26').gsub(/\?/, '%3F') +'&limit=5',
'User-Agent' => @@user_agent
).read
artist_mb_data = artist_mb_xml.scan(/<artist.*?type=\"(.+?)\"\sid=\"([a-f0-9-]+?)\">.*?<name>(.+?)<\/name>/m)
artist_lastfm_xml = open(
'http://ws.audioscrobbler.com/2.0/'+
'?method=artist.getinfo&api_key='+ @@lastfm_api_key +
'&artist='+ URI.escape(name).gsub(/\&/, '%26').gsub(/\?/, '%3F')
).read
begin
artist_lastfm_xml = open(
'http://ws.audioscrobbler.com/2.0/'+
'?method=artist.getinfo&api_key='+ @@lastfm_api_key +
'&artist='+ URI.escape(name).gsub(/\&/, '%26').gsub(/\?/, '%3F')
).read
artist_desc = artist_lastfm_xml.scan(/<summary><\!\[CDATA\[?(.*)\]\]><\/summary>/m)[0][0]
artist_pic = artist_lastfm_xml.scan(/<image\ssize=\"extralarge\">?(.*)<\/image>/)[0][0]
rescue
artist_desc = ''
artist_pic = ''
end
# Save artist
artist = Artist.new
artist.name = artist_mb_data[0][2]
artist.desc = artist_lastfm_xml.scan(/<summary><\!\[CDATA\[?(.*)\]\]><\/summary>/m)[0][0]
artist.pic_url = artist_lastfm_xml.scan(/<image\ssize=\"extralarge\">?(.*)<\/image>/)[0][0]
artist.desc = artist_desc
artist.pic_url = artist_pic
artist.artist_type = artist_mb_data[0][0]
artist.mbid = artist_mb_data[0][1]
artist.save
@@ -42,6 +49,8 @@ class ImportController < ApplicationController
releases_mb_data.each do |item|
if item[2].length == 4
item[2] += '-01-01'
elsif item[2].length == 7
item[2] += '-01'
end
end