tumblr_api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README +12 -0
  2. data/tumblr_api.rb +96 -0
  3. metadata +66 -0
data/README ADDED
@@ -0,0 +1,12 @@
1
+ require 'tumblr-api'
2
+
3
+ url = 'http://thoughtbot.tumblr.com'
4
+ tumblr = Tumblr.new(url)
5
+ articles = tumblr.articles
6
+ total = tumblr.total
7
+
8
+ puts tumblr.id
9
+ puts tumblr.title
10
+
11
+ puts articles[total].url
12
+ puts articles[total].content
@@ -0,0 +1,96 @@
1
+ require 'rubygems'
2
+ require 'nokogiri'
3
+ require 'open-uri'
4
+
5
+ class Tumblr
6
+ attr_accessor :articles
7
+
8
+ def initialize(url)
9
+ get_full_list(url)
10
+ @tumblelog = @xml.xpath("//tumblelog")
11
+ @posts = @body.xpath("//post")
12
+ @articles = []
13
+ @total.times do |num|
14
+ @articles.push Article.new(@posts[num])
15
+ end
16
+ @articles.sort_by{|a| [a.date_gmt]}
17
+ end
18
+
19
+ class Article
20
+ attr_accessor :id, :url, :type, :date_gmt, :date, :unix_timestamp, :format, :reglog_key, :slug
21
+ def initialize(post)
22
+ @article = Nokogiri::XML(post.to_s, nil, 'UTF-8').xpath("//post")[0]
23
+ @id = @article['id']
24
+ @url = @article['url']
25
+ @type = @article['type']
26
+ @date_gmt = @article['date-gmt']
27
+ @date = @article['date']
28
+ @unix_timestamp = @article['unix_timestamp']
29
+ @format = @article['format']
30
+ @reblog_key = @article['reblog-key']
31
+ @slug = @article['slug']
32
+ end
33
+
34
+ def title
35
+ title = ""
36
+ unless @article.xpath("//photo-caption")[0].nil?
37
+ title = @article.xpath("//photo-caption")[0].content
38
+ @article.xpath("//photo-caption")[0].remove
39
+ end
40
+ unless @article.xpath("//regular-title")[0].nil?
41
+ title = @article.xpath("//regular-title")[0].content
42
+ @article.xpath("//regular-title")[0].remove
43
+ end
44
+ title
45
+ end
46
+
47
+ def content
48
+ @article.css("photo-url").each do |elem|
49
+ "<img src = \"" + elem.content + "\" />"
50
+ elem.remove
51
+ end
52
+ @article.css("tag").each do |elem|
53
+ elem.remove
54
+ end
55
+ @article
56
+ end
57
+ end
58
+
59
+ def id
60
+ @tumblelog[0]['name']
61
+ end
62
+
63
+ def title
64
+ @tumblelog[0]['title']
65
+ end
66
+
67
+ def timezone
68
+ @tumblelog[0]['timezone']
69
+ end
70
+
71
+ def to_s
72
+ "http://" + @tumblelog[0]['name'] + ".tumblr.com"
73
+ end
74
+
75
+ def get_full_list(url)
76
+ uri = url + "/api/read?num=50"
77
+ @xml = Nokogiri::HTML(open(uri), nil, 'UTF-8')
78
+ posts = @xml.xpath("//posts").to_s
79
+ @total = @xml.xpath("//posts")[0]['total'].to_i
80
+ pages = @total.to_i/50
81
+ num = 50
82
+ pages.times do
83
+ uri = url + "/api/read?num=50&start=" + num.to_s
84
+ temp = Nokogiri::HTML(open(uri), nil, 'UTF-8').xpath("//posts").to_s
85
+ posts = posts + temp
86
+ num += 50
87
+ end
88
+ @body = Nokogiri::HTML(posts, nil, 'UTF-8')
89
+ end
90
+ end
91
+
92
+ =begin
93
+ url = "http://thejoysofbeingjoy.tumblr.com"
94
+ articles = Tumblr.new(url).articles
95
+ puts articles[0].title
96
+ =end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tumblr_api
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Kyu Park
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-11-24 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: It loads a tumblr blog data.
22
+ email: park@kyu.co
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - tumblr_api.rb
31
+ - README
32
+ homepage: https://github.com/parkq/tumblr_api
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ hash: 3
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.11
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Tumblr Read API
65
+ test_files: []
66
+