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



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

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



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

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