doubapi 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
1
  module Doubapi
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
data/lib/doubapi.rb CHANGED
@@ -26,6 +26,7 @@ require 'open-uri'
26
26
  require 'nokogiri'
27
27
  require 'pp'
28
28
 
29
+ require 'json'
29
30
 
30
31
  #useful information:
31
32
  #when accessing the the Nokogiri parsed result, all the name are downcase-d
@@ -41,7 +42,10 @@ module Doubapi
41
42
  #use Douban API
42
43
  Event = Struct.new :title, :when, :where, :what,:link,:poster_mobile,:bar_icon
43
44
  #release_date is in the format of YY-MM-DD
44
- Album = Struct.new :author, :title, :release_date, :link,:cover_thumbnail,:cover_big,:publisher,:mobile_site,:rating
45
+ Album = Struct.new :author, :title, :release_date, :link,:cover_thumbnail,:cover_big,:publisher,:mobile_site,:rating,:tracks
46
+
47
+ Track = Struct.new :title
48
+
45
49
 
46
50
  #input:{key => "all/singer_name", :location => "shanghai", :start_index => 16,:max_result => 15}
47
51
  #return total number of events satisfying the search criterion
@@ -63,7 +67,7 @@ end
63
67
  #return total number of events satisfying the search criterion
64
68
  #return Doubapi::Album[]
65
69
  def self.search_albums_of h ,&block
66
- totalResult, returnedResult = Douban.search_albums_of h
70
+ totalResult, returnedResult = Douban.search_albums_of_v2 h
67
71
  if block_given?
68
72
  returnedResult.each {|album| block.call(album) }
69
73
  return totalResult;
@@ -176,6 +180,49 @@ def formate_release_date release_date
176
180
  end
177
181
 
178
182
 
183
+
184
+ # use doubapi v2 where json result was returned
185
+ def search_albums_of_v2 h
186
+
187
+ artist_chinese = h[:singer]
188
+ max=h[:max_result]||10
189
+ keywords= "%" + artist_chinese.each_byte.map {|c| c.to_s(16)}.join("%")
190
+ url="https://api.douban.com/v2/music/search?q=#{keywords}"
191
+ #uri="http://api.douban.com/music/subjects?tag=#{keywords}&start-index=1&max-results=#{max}"
192
+
193
+ puts "requeset url #{url}"
194
+ #issue http request
195
+ doc = open(url, :proxy => nil, 'User-Agent' => 'ruby')
196
+
197
+ #parse result
198
+ albums = []
199
+ response = JSON.parse(File.read(doc))
200
+ response["musics"].each do |item|
201
+ #select only whose singer eqls artist_chinese
202
+ if item["attrs"]["singer"].include?(artist_chinese)
203
+ m = item["attrs"]
204
+ author = artist_chinese
205
+ title = m["title"].first
206
+ formated_release_day = m["pubdate"].first
207
+ link = mobile_site = item['mobile_link']
208
+ cover_thumnail = cover_big = item['image']
209
+ publisher = m['publisher']
210
+ rating = item['rating']['average']
211
+ tracks=[]
212
+ m['tracks'].first.split('\n').each do |t|
213
+ tracks << Doubapi::Track.new(t)
214
+ end
215
+
216
+ albums << Doubapi::Album.new(author, title, formated_release_day, link,
217
+ cover_thumnail,cover_big ,publisher,mobile_site,rating,
218
+ tracks)
219
+ end
220
+ end
221
+ [albums.size,albums]
222
+ end
223
+
224
+
225
+
179
226
  #
180
227
  #search albums tagged with h[:singer]. It is not quite accurate. I have seen some irrevlant result are returned.
181
228
  #
data/lib/test.rb CHANGED
@@ -33,6 +33,10 @@ def test2
33
33
  puts album.link
34
34
  puts album.mobile_site
35
35
  puts album.rating
36
+
37
+ album.tracks.each do |track|
38
+ puts track.title
39
+ end
36
40
  end
37
41
 
38
42
  #rating decending
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: doubapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-26 00:00:00.000000000 Z
12
+ date: 2013-11-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec