Class: SL::ITunesSearch

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

Class Method Summary collapse

Class Method Details

.search(search_type, search_terms, link_text) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/searchlink/searches/itunes.rb', line 24

def search(search_type, search_terms, link_text)
  case search_type
  when /^ialb$/ # iTunes Album Search
    url, title = search_itunes("album", search_terms, false)
  when /^iart$/ # iTunes Artist Search
    url, title = search_itunes("musicArtist", search_terms, false)
  when /^imov?$/ # iTunes movie search
    dev = false
    url, title = search_itunes("movie", search_terms, dev, SL.config["itunes_affiliate"])
  when /^ipod$/
    url, title = search_itunes("podcast", search_terms, false)
  when /^isong$/ # iTunes Song Search
    url, title = search_itunes("song", search_terms, false)
  when /^itud?$/ # iTunes app search
    dev = search_type =~ /d$/
    url, title = search_itunes("iPadSoftware", search_terms, dev, SL.config["itunes_affiliate"])
  when /^masd?$/ # Mac App Store search (mas = itunes link, masd = developer link)
    dev = search_type =~ /d$/
    url, title = search_itunes("macSoftware", search_terms, dev, SL.config["itunes_affiliate"])
  end

  [url, title, link_text]
end

.search_itunes(entity, terms, dev, aff = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/searchlink/searches/itunes.rb', line 48

def search_itunes(entity, terms, dev, aff = nil)
  aff ||= SL.config["itunes_affiliate"]

  url = "https://itunes.apple.com/search?term=#{terms.url_encode}&country=#{SL.config['country_code']}&entity=#{entity}&limit=1"
  puts url
  begin
    page = Curl::Json.new(url, compressed: true)
    json = page.json
  rescue StandardError => e
    SL.add_error("Invalid response", "Search for #{terms}: (#{e})")
    return false
  end
  return false unless json

  return false unless json["resultCount"]&.positive?

  result = json["results"][0]
  case entity
  when /movie/
    # dev parameter probably not necessary in this case
    output_url = result["trackViewUrl"]
    output_title = result["trackName"]
  when /(mac|iPad)Software/
    output_url = dev && result["sellerUrl"] ? result["sellerUrl"] : result["trackViewUrl"]
    output_title = result["trackName"]
  when /(musicArtist|song|album)/
    case result["wrapperType"]
    when "track"
      output_url = result["trackViewUrl"]
      output_title = "#{result['trackName']} by #{result['artistName']}"
    when "collection"
      output_url = result["collectionViewUrl"]
      output_title = "#{result['collectionName']} by #{result['artistName']}"
    when "artist"
      output_url = result["artistLinkUrl"]
      output_title = result["artistName"]
    end
  when /podcast/
    output_url = result["collectionViewUrl"]
    output_title = result["collectionName"]
  end
  return false unless output_url && output_title

  return [output_url, output_title] if dev

  [output_url + aff, output_title]
end

.settingsObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/searchlink/searches/itunes.rb', line 8

def settings
  {
    trigger: "(i(pod|art|alb|song|tud?)|masd?)",
    searches: [
      ["ipod", "iTunes podcast"],
      ["iart", "iTunes artist"],
      ["ialb", "iTunes album"],
      ["isong", "iTunes song"],
      ["itu", "iOS App Store Search"],
      ["itud", "iOS App Store Developer Link"],
      ["mas", "Mac App Store Search"],
      ["masd", "Mac App Store Developer Link"]
    ]
  }
end