-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspot.rb
172 lines (146 loc) · 3.92 KB
/
spot.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# -*- encoding : utf-8 -*-
require "spot/song"
require "spot/artist"
require "spot/album"
require "spot/prime"
require "spot/clean"
require "json"
require "rest-client"
require "charlock_holmes/string"
require "yaml"
require "uri"
module Spot
class Search
def initialize
@methods = {
:artists => {
:selector => :artists,
:class => Spot::Artist,
:url => generate_url("artist")
},
:songs => {
:selector => :tracks,
:class => Spot::Song,
:url => generate_url("track")
},
:albums => {
:selector => :albums,
:class => Spot::Album,
:url => generate_url("album")
}
}
@cache = {}
@exclude = YAML.load(File.read("#{File.dirname(__FILE__)}/spot/ignore.yml"))
@config = {
:exclude => 2,
:popularity => 7,
:limit => 0.7,
:offset => 10
}
@options = {}
end
def self.method_missing(method, *args, &blk)
Spot::Search.new.send(method, *args, &blk)
end
def method_missing(method, *args, &blk)
if method.to_s =~ /^find(_all)?_([a-z]+)$/i
find($2, !!$1, args.first)
elsif scrape and content["info"].keys.include?(method.to_s)
content["info"][method.to_s]
else
super(method, *args, &blk)
end
end
def page(value)
tap { @page = value }
end
def prime
tap { @prime = true }
end
def prefix(value)
tap { @prefix = value }
end
def find(type, all, s)
tap {
@search = s
@type = all ? type.to_sym : "#{type}s".to_sym
raise NoMethodError.new(@type) unless @methods.keys.include?(@type)
}
end
def results
unless @prime
@_results ||= scrape.select(&:valid?)
else
@_results ||= scrape
end
end
def strip
tap { @strip = true }
end
def territory(value)
tap { @options.merge!(:territory => value) }
end
def result
if @prime
Spot::Prime.new(results.sort_by(&:popularity).reverse[0..6], search).
results.
select(&:valid?).
first
else
results.select(&:valid?).first
end
end
#
# @value String To be cleaned
# @return String A cleaned string
#
def clean!(value)
Spot::Clean.new(value).process
end
private
def url
@url ||= @methods[@type][:url].
gsub(/<SEARCH>/, URI.escape(search)).
gsub(/<PAGE>/, (@page || 1).to_s)
end
def search(force = false)
return @_search if @_search
@_search = ""
@_search = ((@strip or force) ? clean!(@prefix) + " " : @prefix + " ") if @prefix
@_search += ((@strip or force) ? clean!(@search) : @search)
end
def scrape
return @cache[@type] if @cache[@type]
@cache[@type] = []; content[@methods[@type][:selector].to_s].each do |item|
item = @methods[@type][:class].new(item.merge(@options))
@cache[@type] << item if item.valid? or @prime
end
@cache[@type]
end
def content
data = download
if encoding = data.detect_encoding[:encoding]
data = download.force_encoding(encoding)
else
data = download.strip
end
JSON.parse(data)
rescue ArgumentError
JSON.parse(download)
end
def download
@download ||= RestClient.get(url, :timeout => 10)
end
def errors(error)
case error.to_s
when "403 Forbidden"
raise Spot::RequestLimitError.new(url)
else
raise error
end
end
def generate_url(type)
"http://ws.spotify.com/search/1/#{type}.json?q=<SEARCH>&page=<PAGE>"
end
end
end