doubapi 0.0.6 → 0.0.7

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.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
data/lib/doubapi.rb CHANGED
@@ -7,8 +7,33 @@ require 'pp'
7
7
  module Doubapi
8
8
  #return a Nokogiri XML object
9
9
  #use Douban API
10
- def self.douban_get_xml url
10
+
11
+
12
+ Event = Struct.new :title, :when, :where, :what, :link
13
+ #release_date is in the format of YY-MM-DD
14
+ Album = Struct.new :author, :title, :release_date, :link
15
+
16
+
17
+ #return Doubapi::Event[]
18
+ def self.search_events_of artist , after_date = Time.now.strftime("%Y-%m")
19
+ Douban.search_events_of artist, after_date
20
+ end
21
+
22
+
23
+
24
+ #return Doubapi::Album[]
25
+ def self.search_albums_of artist , after_date = "1900.01"
26
+ Douban.search_albums_of artist ,after_date
27
+ end
28
+
29
+ protected
30
+ class Douban
31
+
32
+ #instance method
33
+ class << self
34
+ def douban_get_xml url
11
35
  puts url
36
+ #I have forgot why i need to specify the user agend
12
37
  doc = open(url,'User-Agent' => 'ruby')
13
38
  Nokogiri::HTML(doc,nil, "utf-8")
14
39
  #Nokogiri::HTML(open(url,:proxy => nil,'User-Agent' => 'ruby'),nil, "utf-8")
@@ -22,15 +47,21 @@ end
22
47
 
23
48
  #return Atom
24
49
  #Douban search : will return results that does not match
25
- def self.search_event key_chinese, location = "shanghai"
50
+ def search_event key_chinese, location = "shanghai" ,max=20
51
+
52
+ if (key_chinese.downcase == "all")
53
+ uri="http://api.douban.com/event/location/#{location}?type=music&start-index=1&max-results=#{max}"
54
+ else
26
55
  keywords= "%" + key_chinese.each_byte.map {|c| c.to_s(16)}.join("%")
27
- uri="http://api.douban.com/events?q=#{keywords}&location=#{location}&start-index=1&max-results=5"
56
+ uri="http://api.douban.com/events?q=#{keywords}&location=#{location}&start-index=1&max-results=#{max}"
57
+ end
58
+
28
59
  #Let's grab it slowly to avoid being baned...
29
60
  sleep(7)
30
61
  douban_get_xml(uri)
31
62
  end
32
63
 
33
- def self.search_ablum artist_chinese ,max=10
64
+ def search_ablum artist_chinese ,max=10
34
65
  keywords= "%" + artist_chinese.each_byte.map {|c| c.to_s(16)}.join("%")
35
66
  uri="http://api.douban.com/music/subjects?tag=#{keywords}&start-index=1&max-results=#{max}"
36
67
  #Let's grab it slowly to avoid being baned...
@@ -38,21 +69,20 @@ def self.search_ablum artist_chinese ,max=10
38
69
  douban_get_xml(uri)
39
70
  end
40
71
 
41
- Douban_Event = Struct.new :title, :when, :where, :what, :link
42
-
43
- #
44
- #release_date is in the format of YY-MM-DD
45
- Douban_Album = Struct.new :author, :title, :release_date, :link
46
-
47
72
 
48
73
  #TODO
49
- def self.looks_like_a_live_show? e, artist
74
+ def looks_like_a_live_show? e, artist
50
75
 
51
76
  #check e.when should happen
52
77
  #2010-08-13F21:30:00+08:00
53
78
  _,_,_,hour = e.when.scan(/\d{1,4}/);
54
79
 
55
- return true if hour.to_i > 18 and e.what.include?(artist)
80
+ if artist.downcase == "all"
81
+ return true if hour.to_i > 18
82
+ else
83
+ return true if hour.to_i > 18 and e.what.include?(artist)
84
+ end
85
+
56
86
  return false
57
87
  end
58
88
 
@@ -63,13 +93,13 @@ end
63
93
  #2010#1
64
94
  #2010-1
65
95
  #2010年1
66
- def self.compare_date a , b
96
+ def compare_date a , b
67
97
  ya, ma = a.scan(/\d{1,4}/)
68
98
  yb, mb = b.scan(/\d{1,4}/)
69
99
  return true if (ya.to_i * 12 + ma.to_i ) >= (yb.to_i*12+mb.to_i)
70
100
  end
71
101
 
72
- def self.search_albums_of artist , after_date = "1900.01"
102
+ def search_albums_of artist , after_date = "1900.01"
73
103
  doc = search_ablum artist
74
104
  albums=[]
75
105
 
@@ -102,13 +132,25 @@ def self.search_albums_of artist , after_date = "1900.01"
102
132
  formated_release_day = "#{y}-#{m}-#{d}"
103
133
  #check the release date
104
134
  if compare_date release_date, after_date
105
- albums << Douban_Album.new(author, title, formated_release_day, link)
135
+ albums << Doubapi::Album.new(author, title, formated_release_day, link)
106
136
  end
107
137
  end
108
138
  albums
109
139
  end
110
140
 
111
- def self.search_events_of artist
141
+
142
+ #return Time object
143
+ #date format is
144
+ #"时间:2010年8月13日 周五 21:30 - 23:55"
145
+ #or
146
+ #2010-08-13F21:30:00+08:00
147
+ def parse_date date
148
+ year, month , day = date.scan(/\d{1,4}/)
149
+ Time.local(year,month,day)
150
+ end
151
+
152
+ def search_events_of artist , after_date = Time.now.strftime("%Y-%m")
153
+
112
154
  doc = search_event artist
113
155
  events=[]
114
156
  doc.xpath("//entry").each do |entry|
@@ -120,29 +162,18 @@ def self.search_events_of artist
120
162
  where = entry.at_xpath('.//where')["valuestring"]
121
163
  link = entry.at_xpath(".//link[@rel='alternate']")["href"]
122
164
  what = entry.at_xpath(".//content").text
123
- events << Douban_Event.new(title, start_time, where, what, link)
165
+
166
+ #check the date
167
+ if parse_date(start_time) > parse_date(after_date)
168
+ events << Doubapi::Event.new(title, start_time, where, what, link)
169
+ end
124
170
  end
125
171
 
126
172
  #filtering of the results
127
173
  events.select{|e| looks_like_a_live_show?(e,artist)}
128
174
  end
129
175
 
130
-
131
-
132
- if __FILE__== $0
133
- #should use Artist Model
134
- #File.read("./app/controllers/artists.txt").split("\n").each {|artist| Artist.new(:name=>artist,:intro=>"no").save}
135
- #Artist.all.each {|a| puts a.name}
136
- File.read("artists.txt").split("\n").each do |artist|
137
- puts artist
138
- e = search_events_of artist
139
- e.each do |event|
140
- puts event.title
141
- puts event.when
142
- puts event.where
143
- puts event.what
144
- end
145
- end
146
- end
176
+ end #self,instance method end
177
+ end #Class Douban
147
178
 
148
179
  end #Module
data/spec/doubapi_spec.rb CHANGED
@@ -58,4 +58,27 @@ describe Doubapi do
58
58
 
59
59
  end
60
60
 
61
+
62
+ it "should be able to search all the shows in 育音堂 and the event.when should later than the time when the search happened if after_date has not specified" do
63
+ key = "育音堂"
64
+ Doubapi.search_events_of(key).each do |event|
65
+ event.what.should be_include(key)
66
+ puts event.title
67
+ puts event.when
68
+ puts event.where
69
+ puts event.link
70
+ end
71
+ end
72
+
73
+ it "should search all the music events if no artist is specified" do
74
+
75
+ Doubapi.search_events_of("all").each do |event|
76
+ puts event.title
77
+ puts event.when
78
+ puts event.where
79
+ puts event.link
80
+ end
81
+
82
+ end
83
+
61
84
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: doubapi
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 6
10
- version: 0.0.6
9
+ - 7
10
+ version: 0.0.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - pierr chen
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-06 00:00:00 +08:00
18
+ date: 2011-01-07 00:00:00 +08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency