Class: Doubapi::Douban

Inherits:
Object
  • Object
show all
Defined in:
lib/doubapi.rb

Class Method Summary collapse

Class Method Details

.compare_date(a, b) ⇒ Object

return true if a >= b a,b could be one of the following format 2010.01 2010.1 2010#1 2010-1 2010年1



174
175
176
177
178
# File 'lib/doubapi.rb', line 174

def compare_date a , b
  ya, ma = a.scan(/\d{1,4}/)
  yb, mb = b.scan(/\d{1,4}/)
  return true if (ya.to_i * 12 + ma.to_i ) >= (yb.to_i*12+mb.to_i)
end

.douban_get_xml(url) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/doubapi.rb', line 98

def douban_get_xml url
  puts url
  #I have forgot why i need to specify the user agend
  doc = open(url, :proxy => nil, 'User-Agent' => 'ruby')
  if doc.nil?
    puts "error:failed to open #{url}"
    return nil;
  end
  
  Nokogiri::HTML(doc,nil, "utf-8")
  
  #Nokogiri::HTML(open(url,:proxy => nil,'User-Agent' => 'ruby'),nil, "utf-8")
  #no network access, used to simulator 
  #doc = File.read(File.join(RAILS_ROOT, "app","controllers","event_sample.xml"))
  #Nokogiri::HTML(doc,nil, "utf-8")
  #Nokogiri::HTML(open(url,:proxy => nil,'User-Agent' => 'ruby'),nil, "utf-8")
end

.formate_release_date(release_date) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/doubapi.rb', line 181

def formate_release_date release_date
    #make release data YY-MM-DD style
    r = release_date.scan(/\d{1,4}/)
    #if DD was not specified
    r << "01"         if r.size == 2
    r << "01" << "01" if r.size == 1

    y , m , d = r

    m = "01" unless (1..12).include?(m.to_i) 
    d = "01" unless (1..30).include?(d.to_i)
    
    "#{y}-#{m}-#{d}" 
end

.looks_like_a_live_show?(e, artist) ⇒ Boolean

TODO

Returns:

  • (Boolean)


152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/doubapi.rb', line 152

def looks_like_a_live_show? e, artist

  #check e.when should happen
  #2010-08-13F21:30:00+08:00
  _,_,_,hour = e.when.scan(/\d{1,4}/);

  if artist.downcase == "all"
    return true if hour.to_i > 18
  else
    return true if hour.to_i > 18 and e.what.include?(artist) 
  end

  return false
end

.parse_date(date) ⇒ Object

return Time object date format is “时间:2010年8月13日 周五 21:30 - 23:55” or 2010-08-13F21:30:00+08:00



299
300
301
302
# File 'lib/doubapi.rb', line 299

def parse_date date
  year, month , day = date.scan(/\d{1,4}/)
  Time.local(year,month,day)
end

.search_ablum(h) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/doubapi.rb', line 140

def search_ablum h
  artist_chinese = h[:singer]
  max=h[:max_result]||10
  keywords= "%" + artist_chinese.each_byte.map {|c| c.to_s(16)}.join("%")
  uri="http://api.douban.com/music/subjects?tag=#{keywords}&start-index=1&max-results=#{max}"
  #Let's grab it slowly to avoid being baned... 
  sleep(7)  
  douban_get_xml(uri)
end

.search_albums_of(h) ⇒ Object

search albums tagged with h. It is not quite accurate. I have seen some irrevlant result are returned.



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/doubapi.rb', line 244

def search_albums_of h 
  artist = h[:singer]
  after_date = h[:since]||"1900.01"
  doc = search_ablum h
 
  if(doc.nil?) 
    return [0,[]];
  end
  
  #the totalResult here trying
  #totalResults = doc.at_xpath(".//totalresults").text.to_i
   
  albums=[]
  doc.xpath("//entry").each do |entry|
    title = entry.at_xpath(".//title").text
    #author
    authorItem = entry.at_xpath(".//name")
    author = if authorItem.nil? then artist else authorItem.text end
    #link - pc web
    link =  entry.at_xpath(".//link[@rel='alternate']")["href"]
    cover_thumnail = entry.at_xpath(".//link[@rel='image']")["href"]
    
    #cover big 
    #example:
    #thumbnail http://img1.douban.com/spic/s1461123.jpg
    #big       http://img1.douban.com/lpic/s1461123.jpg
    cover_big = cover_thumnail.gsub("spic","lpic");
    
    #publisher
    pubItem = entry.at_xpath(".//attribute[@name='publisher']")
    publisher = if pubItem.nil? then "unknow" else pubItem.text end
      
    #rating
    rating = entry.at_xpath(".//rating")["average"]
    #link - mobile_site
    mobile_site = entry.at_xpath(".//link[@rel='mobile']")["href"]
    #release_date
    pubDateItem = entry.at_xpath(".//attribute[@name='pubdate']")
    release_date = if pubDateItem.nil? then "0000-00" else pubDateItem.text end
    formated_release_day = formate_release_date(release_date)
    #check the release date
    if compare_date release_date, after_date     
      albums << Doubapi::Album.new(author, title, formated_release_day, link, cover_thumnail,cover_big ,publisher,mobile_site,rating)
    end
  end
  #improve ME
  [albums.size,albums]
end

.search_albums_of_v2(h) ⇒ Object

use doubapi v2 where json result was returned



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/doubapi.rb', line 199

def search_albums_of_v2 h

  artist_chinese = h[:singer]
  max=h[:max_result]||10
  keywords= "%" + artist_chinese.each_byte.map {|c| c.to_s(16)}.join("%")
  url="https://api.douban.com/v2/music/search?q=#{keywords}"
  #uri="http://api.douban.com/music/subjects?tag=#{keywords}&start-index=1&max-results=#{max}"

  puts "requeset url #{url}"
  #issue http request 
  doc = open(url, :proxy => nil, 'User-Agent' => 'ruby')

  #parse result
  albums = []
  response = JSON.parse(File.read(doc))
  response["musics"].each do |item|
    #select only whose singer eqls artist_chinese
    if item["attrs"]["singer"].include?(artist_chinese)
      m = item["attrs"]
      author = artist_chinese
      title = m["title"].first
      formated_release_day = m["pubdate"].first
      link = mobile_site = item['mobile_link']
      cover_thumnail = cover_big = item['image']
      publisher = m['publisher'].first
      rating = item['rating']['average']
      tracks=[]
      m['tracks'].first.split('\n').each_with_index do |t,index|
        tracks << Doubapi::Track.new(t,nil)
      end

      albums << Doubapi::Album.new(author, title, formated_release_day, link, 
                                   cover_thumnail,cover_big ,publisher,mobile_site,rating,
                                  tracks)
    end 
  end
  [albums.size,albums]
end

.search_event(h) ⇒ Object

return Atom Douban search : will return results that does not match



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/doubapi.rb', line 120

def search_event h
  puts h.inspect

  key_chinese = h[:key]
  location    = h[:location] || "shanghai"
  start_index = h[:start_index] || 1
  max         = h[:max_result]|| 20

  if (key_chinese.downcase == "all")
  uri="http://api.douban.com/event/location/#{location}?type=music&start-index=#{start_index}&max-results=#{max}"
  else
  keywords= "%" + key_chinese.each_byte.map {|c| c.to_s(16)}.join("%")
  uri="http://api.douban.com/events?q=#{keywords}&location=#{location}&start-index=#{start_index}&max-results=#{max}"
  end

  #Let's grab it slowly to avoid being baned... 
  sleep(7)  
  douban_get_xml(uri)
end

.search_events_of(h = {}) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/doubapi.rb', line 304

def search_events_of(h={})
  artist = h[:key]
  after_date =  h[:after_date]||Time.now.strftime("%Y-%m")
  doc = search_event h
  events=[]
  
  #NOTES:all the key will be downcase-d
  totalResults = doc.at_xpath(".//totalresults").text.to_i
  doc.xpath("//entry").each do |entry|
    #pp entry
    title = entry.at_xpath(".//title").text
    #attribute is starttime NOT startTime as specified in the xml
    start_time = entry.at_xpath('.//when')["starttime"]
    #  city = entry.at_xpath('.//location').text
    where = entry.at_xpath('.//where')["valuestring"]
    link =  entry.at_xpath(".//link[@rel='alternate']")["href"]
    what = entry.at_xpath(".//content").text
    posterItem = entry.at_xpath(".//link[@rel='image-lmobile']")
    poster_mobile = if posterItem.nil? then "empty" else posterItem["href"] end
    authItem = entry.at_xpath(".//author")
    iconLink = if authItem.nil? then nil else authItem.at_xpath(".//link[@rel='icon']") end
    bar_icon = if (iconLink.nil?) then nil else iconLink["href"] end

  #check the date
  if parse_date(start_time) > parse_date(after_date)
      events << Doubapi::Event.new(title, start_time, where, what, link, poster_mobile, bar_icon)
    end
  end
  
  #filtering of the results
  events.select!{|e| looks_like_a_live_show?(e,artist)}
  
  [totalResults, events]
end