doubapi 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/doubapi.rb +15 -11
- data/lib/doubapi/version.rb +1 -1
- data/lib/test.rb +14 -5
- metadata +1 -1
data/lib/doubapi.rb
CHANGED
@@ -58,7 +58,8 @@ Event = Struct.new :title, :when, :where, :what,:link,:poster_mobile,:bar_icon
|
|
58
58
|
#release_date is in the format of YY-MM-DD
|
59
59
|
Album = Struct.new :author, :title, :release_date, :link,:cover_thumbnail,:cover_big,:publisher,:mobile_site,:rating,:tracks
|
60
60
|
|
61
|
-
Track = Struct.new :title,:
|
61
|
+
Track = Struct.new :title,:play_url
|
62
|
+
|
62
63
|
|
63
64
|
|
64
65
|
#input:{key => "all/singer_name", :location => "shanghai", :start_index => 16,:max_result => 15}
|
@@ -199,34 +200,37 @@ end
|
|
199
200
|
def search_albums_of_v2 h
|
200
201
|
|
201
202
|
artist_chinese = h[:singer]
|
202
|
-
|
203
|
+
max=h[:max_result]||10
|
203
204
|
keywords= "%" + artist_chinese.each_byte.map {|c| c.to_s(16)}.join("%")
|
204
205
|
url="https://api.douban.com/v2/music/search?q=#{keywords}"
|
205
206
|
#uri="http://api.douban.com/music/subjects?tag=#{keywords}&start-index=1&max-results=#{max}"
|
206
207
|
|
207
|
-
puts "requeset url #{url}"
|
208
|
+
puts "requeset url #{url} #{artist_chinese}"
|
208
209
|
#issue http request
|
209
210
|
doc = open(url, :proxy => nil, 'User-Agent' => 'ruby')
|
210
211
|
|
211
212
|
#parse result
|
212
213
|
albums = []
|
213
|
-
response = JSON.parse(
|
214
|
+
response = JSON.parse(doc.read)
|
215
|
+
|
214
216
|
response["musics"].each do |item|
|
215
217
|
#select only whose singer eqls artist_chinese
|
216
|
-
|
218
|
+
has_singer_attr = !item["attrs"].nil? && !item["attrs"]["singer"].nil?
|
219
|
+
if has_singer_attr && item["attrs"]["singer"].include?(artist_chinese)
|
217
220
|
m = item["attrs"]
|
218
221
|
author = artist_chinese
|
219
|
-
title = m["title"].first
|
220
|
-
formated_release_day = m["pubdate"].first
|
222
|
+
title = if m['title'].nil? then "unknown" else m["title"].first end
|
223
|
+
formated_release_day = if m['pubdate'].nil? then "unknown" else m["pubdate"].first end
|
221
224
|
link = mobile_site = item['mobile_link']
|
222
225
|
cover_thumnail = cover_big = item['image']
|
223
|
-
publisher = m['publisher'].first
|
226
|
+
publisher = if m['publisher'].nil? then "unknown" else m['publisher'].first end
|
224
227
|
rating = item['rating']['average']
|
225
228
|
tracks=[]
|
226
|
-
m['tracks'].
|
227
|
-
tracks
|
229
|
+
if not m['tracks'].nil?
|
230
|
+
m['tracks'].first.split("\n").each_with_index do |t,index|
|
231
|
+
tracks << Doubapi::Track.new(t,nil)
|
232
|
+
end
|
228
233
|
end
|
229
|
-
|
230
234
|
albums << Doubapi::Album.new(author, title, formated_release_day, link,
|
231
235
|
cover_thumnail,cover_big ,publisher,mobile_site,rating,
|
232
236
|
tracks)
|
data/lib/doubapi/version.rb
CHANGED
data/lib/test.rb
CHANGED
@@ -19,8 +19,14 @@ end
|
|
19
19
|
|
20
20
|
def test2
|
21
21
|
#author = "李志"
|
22
|
-
author = "窦唯"
|
23
|
-
|
22
|
+
#author = "窦唯"
|
23
|
+
|
24
|
+
#authors = File.read("artists.txt").split("\n")
|
25
|
+
|
26
|
+
authors = ['汪峰']
|
27
|
+
authors.each do |author|
|
28
|
+
|
29
|
+
total, albums = Doubapi.search_albums_of(:singer=>author,:since=>"1900-05",:max_result => 20)
|
24
30
|
|
25
31
|
#release date decending
|
26
32
|
albums.sort {|a , b| b.release_date <=> a.release_date }.each do |album|
|
@@ -38,11 +44,13 @@ def test2
|
|
38
44
|
puts track.title
|
39
45
|
end
|
40
46
|
|
41
|
-
puts album.json
|
47
|
+
#puts album.json
|
42
48
|
end
|
43
|
-
|
49
|
+
|
50
|
+
end# each author
|
44
51
|
#rating decending
|
45
|
-
|
52
|
+
|
53
|
+
=begin
|
46
54
|
#release date decending
|
47
55
|
albums.sort {|a , b| b.rating <=> a.rating }.each do |album|
|
48
56
|
puts "-------------------------------"
|
@@ -56,6 +64,7 @@ def test2
|
|
56
64
|
puts album.rating
|
57
65
|
end
|
58
66
|
puts "total #{total}"
|
67
|
+
=end
|
59
68
|
end
|
60
69
|
|
61
70
|
|