doubapi 0.0.3 → 0.0.4

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.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/doubapi.rb CHANGED
@@ -9,9 +9,9 @@ module Doubapi
9
9
  #use Douban API
10
10
  def self.douban_get_xml url
11
11
  puts url
12
- Nokogiri::HTML(open(url,'User-Agent' => 'ruby'),nil, "utf-8")
12
+ doc = open(url,'User-Agent' => 'ruby')
13
+ Nokogiri::HTML(doc,nil, "utf-8")
13
14
  #Nokogiri::HTML(open(url,:proxy => nil,'User-Agent' => 'ruby'),nil, "utf-8")
14
-
15
15
  #no network access, used to simulator
16
16
  #doc = File.read(File.join(RAILS_ROOT, "app","controllers","event_sample.xml"))
17
17
  #Nokogiri::HTML(doc,nil, "utf-8")
@@ -22,7 +22,7 @@ end
22
22
 
23
23
  #return Atom
24
24
  #Douban search : will return results that does not match
25
- def self.search key_chinese, location = "shanghai"
25
+ def self.search_event key_chinese, location = "shanghai"
26
26
  keywords= "%" + key_chinese.each_byte.map {|c| c.to_s(16)}.join("%")
27
27
  uri="http://api.douban.com/events?q=#{keywords}&location=#{location}&start-index=1&max-results=5"
28
28
  #Let's grab it slowly to avoid being baned...
@@ -30,8 +30,16 @@ def self.search key_chinese, location = "shanghai"
30
30
  douban_get_xml(uri)
31
31
  end
32
32
 
33
+ def self.search_ablum artist_chinese ,max=10
34
+ keywords= "%" + artist_chinese.each_byte.map {|c| c.to_s(16)}.join("%")
35
+ uri="http://api.douban.com/music/subjects?tag=#{keywords}&start-index=1&max-results=#{max}"
36
+ #Let's grab it slowly to avoid being baned...
37
+ sleep(7)
38
+ douban_get_xml(uri)
39
+ end
33
40
 
34
- Douban_Event = Struct.new :title, :when, :where, :what,:link
41
+ Douban_Event = Struct.new :title, :when, :where, :what, :link
42
+ Douban_Album = Struct.new :author, :title, :release_date, :link
35
43
 
36
44
 
37
45
  #TODO
@@ -41,13 +49,48 @@ def self.looks_like_a_live_show? e, artist
41
49
  #2010-08-13F21:30:00+08:00
42
50
  _,_,_,hour = e.when.scan(/\d{1,4}/);
43
51
 
44
- puts "==========================events happend at #{e.when}, #{hour}"
45
52
  return true if hour.to_i > 18 and e.what.include?(artist)
46
53
  return false
47
54
  end
48
55
 
56
+ #return true if a >= b
57
+ #a,b could be one of the following format
58
+ #2010.01
59
+ #2010.1
60
+ #2010#1
61
+ #2010-1
62
+ #2010年1
63
+ def self.compare_date a , b
64
+ ya, ma = a.scan(/\d{1,4}/)
65
+ yb, mb = b.scan(/\d{1,4}/)
66
+ return true if (ya.to_i * 12 + ma.to_i ) >= (yb.to_i*12+mb.to_i)
67
+ end
68
+
69
+ def self.search_album_of artist , after_date = "1900.01"
70
+ doc = search_ablum artist
71
+ albums=[]
72
+
73
+ doc.xpath("//entry").each do |entry|
74
+ title = entry.at_xpath(".//title").text
75
+ author = entry.at_xpath(".//name").text
76
+ release_date = unless entry.at_xpath(".//attribute[@name='pubdate']").nil?
77
+ entry.at_xpath(".//attribute[@name='pubdate']").text
78
+ else
79
+ #means unknow
80
+ "0000-00"
81
+ end
82
+ link = entry.at_xpath(".//link[@rel='alternate']")["href"]
83
+
84
+ #check the release date
85
+ if compare_date release_date, after_date
86
+ albums << Douban_Album.new(author, title, release_date, link)
87
+ end
88
+ end
89
+ albums
90
+ end
91
+
49
92
  def self.search_events_of artist
50
- doc = search artist
93
+ doc = search_event artist
51
94
  events=[]
52
95
  doc.xpath("//entry").each do |entry|
53
96
  #pp entry
data/spec/doubapi_spec.rb CHANGED
@@ -1,12 +1,37 @@
1
1
  require 'lib/doubapi'
2
2
  describe Doubapi do
3
3
 
4
- it "should be able to search " do
5
- Doubapi.search_events_of("eagles").each do |event|
6
- event.what.should include?("eagles")
4
+ it "should be able to search 许巍 " do
5
+ key = "许巍"
6
+ Doubapi.search_events_of(key).each do |event|
7
+ event.what.should be_include(key)
8
+ puts event.title
9
+ puts event.when
10
+ puts event.where
11
+ puts event.link
7
12
  end
8
13
  end
9
14
 
15
+ it "should be able to search albums of 李志 " do
16
+ author = "李志"
17
+ Doubapi.search_album_of(author).each do |album|
18
+ puts album.author
19
+ puts album.release_date
20
+ puts album.title
21
+ puts album.link
22
+ end
23
+ end
24
+
25
+ it "should be able to search albums of 李志 that released after 2010 " do
26
+ author = "李志"
27
+ Doubapi.search_album_of(author, "2010/05").each do |album|
28
+ puts album.author
29
+ puts album.release_date
30
+ puts album.title
31
+ puts album.link
32
+ end
33
+ end
34
+
10
35
 
11
36
 
12
37
  end
data/spec/sample_album ADDED
@@ -0,0 +1,87 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <feed xmlns="http://www.w3.org/2005/Atom" xmlns:db="http://www.douban.com/xmlns/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:opensearch="http://a9.com/-/spec/opensearchrss/1.0/">
3
+ <title>带有标签 李志 的条目</title>
4
+ <opensearch:startIndex>1</opensearch:startIndex>
5
+ <opensearch:totalResults>27</opensearch:totalResults>
6
+ <entry>
7
+ <id>http://api.douban.com/music/subject/1958731</id>
8
+ <title>梵高先生</title>
9
+ <category scheme="http://www.douban.com/2007#kind" term="http://www.douban.com/2007#music"/>
10
+ <author>
11
+ <name>李志</name>
12
+ </author>
13
+ <link href="http://api.douban.com/music/subject/1958731" rel="self"/>
14
+ <link href="http://music.douban.com/subject/1958731/" rel="alternate"/>
15
+ <link href="http://img3.douban.com/spic/s3743777.jpg" rel="image"/>
16
+ <db:attribute name="isrc">CNA070601780</db:attribute>
17
+ <db:attribute name="ean">CNA070601780</db:attribute>
18
+ <db:attribute name="pubdate">2007年1月11日</db:attribute>
19
+ <db:attribute name="singer">李志</db:attribute>
20
+ <db:attribute name="publisher">口袋唱片</db:attribute>
21
+ <gd:rating average="8.8" max="10" min="0" numRaters="15224"/>
22
+ </entry>
23
+ <entry>
24
+ <id>http://api.douban.com/music/subject/1933017</id>
25
+ <title>被禁忌的游戏</title>
26
+ <category scheme="http://www.douban.com/2007#kind" term="http://www.douban.com/2007#music"/>
27
+ <author>
28
+ <name>李志</name>
29
+ </author>
30
+ <link href="http://api.douban.com/music/subject/1933017" rel="self"/>
31
+ <link href="http://music.douban.com/subject/1933017/" rel="alternate"/>
32
+ <link href="http://img3.douban.com/spic/s2846631.jpg" rel="image"/>
33
+ <db:attribute name="isrc">CNA070604100</db:attribute>
34
+ <db:attribute name="ean">CNA070604100</db:attribute>
35
+ <db:attribute name="pubdate">2005-12-01</db:attribute>
36
+ <db:attribute name="singer">李志</db:attribute>
37
+ <db:attribute name="publisher">口袋唱片</db:attribute>
38
+ <gd:rating average="8.7" max="10" min="0" numRaters="9602"/>
39
+ </entry>
40
+ <entry>
41
+ <id>http://api.douban.com/music/subject/4060882</id>
42
+ <title>我爱南京</title>
43
+ <category scheme="http://www.douban.com/2007#kind" term="http://www.douban.com/2007#music"/>
44
+ <author>
45
+ <name>李志</name>
46
+ </author>
47
+ <link href="http://api.douban.com/music/subject/4060882" rel="self"/>
48
+ <link href="http://music.douban.com/subject/4060882/" rel="alternate"/>
49
+ <link href="http://img3.douban.com/spic/s4008894.jpg" rel="image"/>
50
+ <db:attribute name="pubdate">2009-10-16</db:attribute>
51
+ <db:attribute name="singer">李志</db:attribute>
52
+ <gd:rating average="8.5" max="10" min="0" numRaters="11408"/>
53
+ </entry>
54
+ <entry>
55
+ <id>http://api.douban.com/music/subject/3435496</id>
56
+ <title>工体东路没有人</title>
57
+ <category scheme="http://www.douban.com/2007#kind" term="http://www.douban.com/2007#music"/>
58
+ <author>
59
+ <name>李志</name>
60
+ </author>
61
+ <link href="http://api.douban.com/music/subject/3435496" rel="self"/>
62
+ <link href="http://music.douban.com/subject/3435496/" rel="alternate"/>
63
+ <link href="http://img3.douban.com/spic/s3561838.jpg" rel="image"/>
64
+ <db:attribute name="pubdate">2009-01-22</db:attribute>
65
+ <db:attribute name="singer">李志</db:attribute>
66
+ <db:attribute name="publisher">MicroMu</db:attribute>
67
+ <gd:rating average="8.9" max="10" min="0" numRaters="8735"/>
68
+ </entry>
69
+ <entry>
70
+ <id>http://api.douban.com/music/subject/1918315</id>
71
+ <title>B&amp;B</title>
72
+ <category scheme="http://www.douban.com/2007#kind" term="http://www.douban.com/2007#music"/>
73
+ <author>
74
+ <name>李志</name>
75
+ </author>
76
+ <link href="http://api.douban.com/music/subject/1918315" rel="self"/>
77
+ <link href="http://music.douban.com/subject/1918315/" rel="alternate"/>
78
+ <link href="http://img3.douban.com/spic/s2329976.jpg" rel="image"/>
79
+ <db:attribute name="isrc">CNS040444800</db:attribute>
80
+ <db:attribute name="pubdate">2004-12</db:attribute>
81
+ <db:attribute name="singer">李志</db:attribute>
82
+ <db:attribute name="publisher">VAYA</db:attribute>
83
+ <gd:rating average="9.0" max="10" min="0" numRaters="6273"/>
84
+ </entry>
85
+ <opensearch:itemsPerPage>5</opensearch:itemsPerPage>
86
+ </feed>
87
+
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: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
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: 2010-12-24 00:00:00 +08:00
18
+ date: 2011-01-06 00:00:00 +08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -68,6 +68,7 @@ files:
68
68
  - lib/doubapi.rb
69
69
  - lib/doubapi/version.rb
70
70
  - spec/doubapi_spec.rb
71
+ - spec/sample_album
71
72
  has_rdoc: true
72
73
  homepage: ""
73
74
  licenses: []