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_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
156 157 158 159 160 |
# File 'lib/doubapi.rb', line 156 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
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/doubapi.rb', line 80 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
163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/doubapi.rb', line 163 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
134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/doubapi.rb', line 134 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
235 236 237 238 |
# File 'lib/doubapi.rb', line 235 def parse_date date year, month , day = date.scan(/\d{1,4}/) Time.local(year,month,day) end |
.search_ablum(h) ⇒ Object
122 123 124 125 126 127 128 129 130 |
# File 'lib/doubapi.rb', line 122 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.
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 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 |
# File 'lib/doubapi.rb', line 183 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 #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_event(h) ⇒ Object
return Atom Douban search : will return results that does not match
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/doubapi.rb', line 102 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
240 241 242 243 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 |
# File 'lib/doubapi.rb', line 240 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 |