doubapi 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/doubapi.gemspec CHANGED
@@ -14,6 +14,8 @@ Gem::Specification.new do |s|
14
14
 
15
15
  s.rubyforge_project = "doubapi"
16
16
  s.add_development_dependency "rspec", "~> 2.0.0.beta.22"
17
+ s.add_dependency "nokogiri"
18
+
17
19
  s.files = `git ls-files`.split("\n")
18
20
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
21
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
@@ -1,3 +1,3 @@
1
1
  module Doubapi
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/doubapi.rb CHANGED
@@ -1,3 +1,86 @@
1
+ require 'rubygems' #a hack to require failure for nokogiri
2
+ require 'open-uri'
3
+ require 'nokogiri'
4
+ require 'pp'
5
+
6
+
1
7
  module Doubapi
2
- # Your code goes here...
8
+ #return a Nokogiri XML object
9
+ #use Douban API
10
+ def self.douban_get_xml url
11
+ puts url
12
+ Nokogiri::HTML(open(url,'User-Agent' => 'ruby'),nil, "utf-8")
13
+ #Nokogiri::HTML(open(url,:proxy => nil,'User-Agent' => 'ruby'),nil, "utf-8")
14
+
15
+ #no network access, used to simulator
16
+ #doc = File.read(File.join(RAILS_ROOT, "app","controllers","event_sample.xml"))
17
+ #Nokogiri::HTML(doc,nil, "utf-8")
18
+ #Nokogiri::HTML(open(url,:proxy => nil,'User-Agent' => 'ruby'),nil, "utf-8")
3
19
  end
20
+
21
+
22
+
23
+ #return Atom
24
+ #Douban search : will return results that does not match
25
+ def self.search key_chinese, location = "shanghai"
26
+ 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"
28
+ #Let's grab it slowly to avoid being baned...
29
+ sleep(7)
30
+ douban_get_xml(uri)
31
+ end
32
+
33
+
34
+ Douban_Event = Struct.new :title, :when, :where, :what,:link
35
+
36
+
37
+ #TODO
38
+ def self.looks_like_a_live_show? e, artist
39
+
40
+ #check e.when should happen
41
+ #2010-08-13F21:30:00+08:00
42
+ _,_,_,hour = e.when.scan(/\d{1,4}/);
43
+
44
+ puts "==========================events happend at #{e.when}, #{hour}"
45
+ return true if hour.to_i > 18 and e.what.include?(artist)
46
+ return false
47
+ end
48
+
49
+ def self.search_events_of artist
50
+ doc = search artist
51
+ events=[]
52
+ doc.xpath("//entry").each do |entry|
53
+ #pp entry
54
+ title = entry.at_xpath(".//title").text
55
+ #attribute is starttime NOT startTime as specified in the xml
56
+ start_time = entry.at_xpath('.//when')["starttime"]
57
+ # city = entry.at_xpath('.//location').text
58
+ where = entry.at_xpath('.//where')["valuestring"]
59
+ link = entry.at_xpath(".//link[@rel='alternate']")["href"]
60
+ what = entry.at_xpath(".//content").text
61
+ events << Douban_Event.new(title, start_time, where, what, link)
62
+ end
63
+
64
+ #filtering of the results
65
+ events.select{|e| looks_like_a_live_show?(e,artist)}
66
+ end
67
+
68
+
69
+
70
+ if __FILE__== $0
71
+ #should use Artist Model
72
+ #File.read("./app/controllers/artists.txt").split("\n").each {|artist| Artist.new(:name=>artist,:intro=>"no").save}
73
+ #Artist.all.each {|a| puts a.name}
74
+ File.read("artists.txt").split("\n").each do |artist|
75
+ puts artist
76
+ e = search_events_of artist
77
+ e.each do |event|
78
+ puts event.title
79
+ puts event.when
80
+ puts event.where
81
+ puts event.what
82
+ end
83
+ end
84
+ end
85
+
86
+ end #Module
@@ -0,0 +1,12 @@
1
+ require 'lib/doubapi'
2
+ describe Doubapi do
3
+
4
+ it "should be able to search " do
5
+ Doubapi.search_events_of("eagles").each do |event|
6
+ event.what.should include?("eagles")
7
+ end
8
+ end
9
+
10
+
11
+
12
+ 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: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - pierr chen
@@ -36,6 +36,20 @@ dependencies:
36
36
  version: 2.0.0.beta.22
37
37
  type: :development
38
38
  version_requirements: *id001
39
+ - !ruby/object:Gem::Dependency
40
+ name: nokogiri
41
+ prerelease: false
42
+ requirement: &id002 !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ hash: 3
48
+ segments:
49
+ - 0
50
+ version: "0"
51
+ type: :runtime
52
+ version_requirements: *id002
39
53
  description: douban API gems
40
54
  email:
41
55
  - pierr.chen@gmail.com
@@ -53,6 +67,7 @@ files:
53
67
  - doubapi.gemspec
54
68
  - lib/doubapi.rb
55
69
  - lib/doubapi/version.rb
70
+ - spec/doubapi_spec.rb
56
71
  has_rdoc: true
57
72
  homepage: ""
58
73
  licenses: []