Class: Doubapi::Douban
- Inherits:
-
Object
- Object
- Doubapi::Douban
- Defined in:
- lib/doubapi.rb
Class Method Summary collapse
-
.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.
- .douban_get_xml(url) ⇒ Object
- .formate_release_date(release_date) ⇒ Object
-
.looks_like_a_live_show?(e, artist) ⇒ Boolean
TODO.
-
.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.
- .search_ablum(h) ⇒ Object
-
.search_albums_of(h) ⇒ Object
search albums tagged with h.
-
.search_albums_of_v2(h) ⇒ Object
use doubapi v2 where json result was returned.
-
.search_event(h) ⇒ Object
return Atom Douban search : will return results that does not match.
- .search_events_of(h = {}) ⇒ Object
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
175 176 177 178 179 |
# File 'lib/doubapi.rb', line 175 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
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/doubapi.rb', line 99 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
182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/doubapi.rb', line 182 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
153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/doubapi.rb', line 153 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
303 304 305 306 |
# File 'lib/doubapi.rb', line 303 def parse_date date year, month , day = date.scan(/\d{1,4}/) Time.local(year,month,day) end |
.search_ablum(h) ⇒ Object
141 142 143 144 145 146 147 148 149 |
# File 'lib/doubapi.rb', line 141 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.
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 292 293 294 295 |
# File 'lib/doubapi.rb', line 248 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 = entry.at_xpath(".//name") = if .nil? then artist else .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 = 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(, title, formated_release_day, link, cover_thumnail,cover_big ,publisher,mobile_site,) end end #improve ME [albums.size,albums] end |
.search_albums_of_v2(h) ⇒ Object
use doubapi v2 where json result was returned
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 237 238 239 240 |
# File 'lib/doubapi.rb', line 200 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} #{artist_chinese}" #issue http request doc = open(url, :proxy => nil, 'User-Agent' => 'ruby') #parse result albums = [] response = JSON.parse(doc.read) response["musics"].each do |item| #select only whose singer eqls artist_chinese has_singer_attr = !item["attrs"].nil? && !item["attrs"]["singer"].nil? if has_singer_attr && item["attrs"]["singer"].include?(artist_chinese) m = item["attrs"] = artist_chinese title = if m['title'].nil? then "unknown" else m["title"].first end formated_release_day = if m['pubdate'].nil? then "unknown" else m["pubdate"].first end link = mobile_site = item['mobile_link'] cover_thumnail = cover_big = item['image'] publisher = if m['publisher'].nil? then "unknown" else m['publisher'].first end = item['rating']['average'] tracks=[] if not m['tracks'].nil? m['tracks'].first.split("\n").each_with_index do |t,index| tracks << Doubapi::Track.new(t,nil) end end albums << Doubapi::Album.new(, title, formated_release_day, link, cover_thumnail,cover_big ,publisher,mobile_site,, tracks) end end [albums.size,albums] end |
.search_event(h) ⇒ Object
return Atom Douban search : will return results that does not match
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/doubapi.rb', line 121 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
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 338 339 340 341 |
# File 'lib/doubapi.rb', line 308 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 = 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, ) end end #filtering of the results events.select!{|e| looks_like_a_live_show?(e,artist)} [totalResults, events] end |