Class: SL::LastFMSearch

Inherits:
Object
  • Object
show all
Defined in:
lib/searchlink/searches/lastfm.rb

Class Method Summary collapse

Class Method Details

.search(search_type, search_terms, link_text) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/searchlink/searches/lastfm.rb', line 16

def search(search_type, search_terms, link_text)
  type = search_type =~ /art$/ ? 'artist' : 'track'

  url = "http://ws.audioscrobbler.com/2.0/?method=#{type}.search&#{type}=#{search_terms.url_encode}&api_key=2f3407ec29601f97ca8a18ff580477de&format=json"
  json = Curl::Json.new(url).json
  return false unless json['results']

  begin
    case type
    when 'track'
      result = json['results']['trackmatches']['track'][0]
      url = result['url']
      title = "#{result['name']} by #{result['artist']}"
    when 'artist'
      result = json['results']['artistmatches']['artist'][0]
      url = result['url']
      title = result['name']
    end
    [url, title, link_text]
  rescue StandardError
    false
  end
end

.settingsObject



6
7
8
9
10
11
12
13
14
# File 'lib/searchlink/searches/lastfm.rb', line 6

def settings
  {
    trigger: 'l(art|song)',
    searches: [
      ['lart', 'Last.fm Artist Search'],
      ['lsong', 'Last.fm Song Search']
    ]
  }
end