twitter-photos 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/Rakefile +1 -2
  2. data/VERSION +1 -1
  3. data/lib/twitter-photos.rb +99 -46
  4. metadata +7 -19
data/Rakefile CHANGED
@@ -6,11 +6,10 @@ begin
6
6
  Jeweler::Tasks.new do |gem|
7
7
  gem.name = "twitter-photos"
8
8
  gem.summary = %Q{Put in your username, get a bunch of photos}
9
- gem.description = %Q{This gem allows you to use any twitter username to return an array of photo urls, with corresponding tweets, dates and thumbnails}
9
+ gem.description = %Q{Allows you to use any twitter username to return a collection of objects of photo urls, with corresponding tweets, dates and thumbnails.}
10
10
  gem.email = "vegan.bookis@gmail.com"
11
11
  gem.homepage = "http://github.com/bookis/twitter-photos"
12
12
  gem.authors = ["bookis"]
13
- gem.add_development_dependency "thoughtbot-shoulda"
14
13
  gem.add_dependency 'nokogiri'
15
14
  gem.add_dependency 'typhoeus'
16
15
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -1,25 +1,55 @@
1
- module TwitterPhotos
2
- require 'typhoeus'
3
- require 'nokogiri'
4
- def self.get_photos_by(username)
1
+ require 'typhoeus'
2
+ require 'nokogiri'
3
+
4
+ class TwitterPhoto
5
+ attr_accessor :username, :thumb, :date, :tweet, :url
6
+
7
+ TWITPIC_URL = 'http://twitpic.com'
8
+ TWEETPHOTO_URL = 'http://tweetphotoapi.com/api/tpapi.svc/json/usernames/'
9
+ YFROG_URL = 'http://yfrog.com/froggy.php'
10
+ TWITGOO_URL = 'http://twitgoo.com/api/username/timeline/'
11
+
12
+
13
+ def initialize(thumb=nil, tweet='No tweet for this photo', date=nil, url=nil, username=nil)
14
+ date = date.strip
15
+ dmatch = date.match(/\sago\b/i)
16
+ @username = username
17
+ @thumb = thumb
18
+ @tweet = tweet.strip
19
+ @date = dmatch.nil? ? DateTime.parse(date).to_s : DateTime.time_ago_to_date(date).to_s rescue @date = date
20
+ @url = url
21
+ @tweet = "No tweet for this photo." if @tweet.blank?
22
+ end
23
+
24
+ def self.find_by_username(username)
25
+ found = []
26
+ ObjectSpace.each_object(TwitterPhoto) { |o|
27
+ found << o if o.username == username
28
+ }
29
+ found.sort_by {|x| x.date}
30
+ end
31
+
32
+ def self.get_photos_by(username, options={})
5
33
  @pix = []
6
34
  hydra = Typhoeus::Hydra.new
7
35
 
8
- third_request = Typhoeus::Request.new("http://twitpic.com/photos/#{username}", :timeout => 5000)
9
- third_request.on_complete do |response|
36
+ twitpic_request = Typhoeus::Request.new("#{TWITPIC_URL}/photos/#{username}", :timeout => 5000)
37
+ twitpic_request.on_complete do |response|
10
38
  twitpics = Nokogiri.parse(response.body)
11
39
  twitpics.xpath("//div[@class='profile-photo-img']/a").each do |row|
12
40
  url = row[:href]
13
- twitpic_request = Typhoeus::Request.new("http://twitpic.com#{url}", :timeout => 5000)
41
+ twitpic_request = Typhoeus::Request.new("#{TWITPIC_URL}#{url}", :timeout => 5000)
14
42
  twitpic_request.on_complete do |twitpic|
15
43
  begin
16
44
  twitpic = Nokogiri.parse(twitpic.body)
17
- pic = []
18
- pic << "http://twitpic.com/show/thumb#{url}.jpg"
19
- pic << twitpic.xpath("//div/div[@id='view-photo-caption']").text
20
- pic << twitpic.xpath("//div/div[@id='photo-info']").text
21
- pic << twitpic.at_xpath("//img[@class='photo']")[:src]
22
- @pix << pic
45
+ # pic = []
46
+ TwitterPhoto.new(
47
+ "#{TWITPIC_URL}/show/thumb#{url}.jpg",
48
+ twitpic.xpath("//div/div[@id='view-photo-caption']").text,
49
+ twitpic.xpath("//div/div[@id='photo-info']").text,
50
+ twitpic.at_xpath("//img[@class='photo']")[:src],
51
+ username
52
+ )
23
53
  rescue
24
54
  end
25
55
  end
@@ -27,8 +57,8 @@ module TwitterPhotos
27
57
  end
28
58
  end
29
59
 
30
- first_request = Typhoeus::Request.new("http://yfrog.com/froggy.php", :params => {:username => username}, :timeout => 5000)
31
- first_request.on_complete do |response|
60
+ yfrog_request = Typhoeus::Request.new(YFROG_URL, :params => {:username => username}, :timeout => 5000)
61
+ yfrog_request.on_complete do |response|
32
62
  yfrogs = Nokogiri.parse(response.body)
33
63
  yfrogs.xpath("//div[@class='timeline_entry']").each do |row|
34
64
  url = row.at_xpath("div/div[@class='thumbtweet' ]/a")[:href]
@@ -36,12 +66,13 @@ module TwitterPhotos
36
66
  yfrog_request.on_complete do |yfrog|
37
67
  begin
38
68
  yfrog = Nokogiri.parse(yfrog.body)
39
- pic = []
40
- pic << yfrog.at_xpath("//meta[@property='og:title']").attributes['content'].to_s + '.th.jpg'
41
- pic << yfrog.at_xpath("///div[@class='twittertweet']/div/div/div").text rescue pic << ''
42
- pic << yfrog.at_xpath("///div[@class='twittertweet']/div/div/div/div").text rescue pic << ''
43
- pic << yfrog.at_xpath("//img[@id='main_image']")[:src]
44
- @pix << pic
69
+ TwitterPhoto.new(
70
+ yfrog.at_xpath("//meta[@property='og:title']").attributes['content'].to_s + '.th.jpg',
71
+ yfrog.at_xpath("///div[@class='twittertweet']/div/div/div").text,
72
+ yfrog.at_xpath("///div[@class='twittertweet']/div/div/div/div").text,
73
+ yfrog.at_xpath("//img[@id='main_image']")[:src],
74
+ username
75
+ )
45
76
  rescue
46
77
  end
47
78
  end
@@ -49,47 +80,69 @@ module TwitterPhotos
49
80
  end
50
81
  end
51
82
 
52
- second_request = Typhoeus::Request.new("http://tweetphotoapi.com/api/tpapi.svc/json/users/#{username}", :timeout => 5000)
53
- second_request.on_complete do |response|
83
+ tweetphoto_request = Typhoeus::Request.new("#{TWEETPHOTO_URL}#{username}", :timeout => 5000)
84
+ tweetphoto_request.on_complete do |response|
54
85
  if response.code == 200
55
86
  tweetphoto_id = JSON.parse(response.body)["Id"]
56
- tweetphoto_user_request = Typhoeus::Request.new("http://tweetphotoapi.com/api/tpapi.svc/json/users/#{tweetphoto_id}/photos", :timeout => 5000)
57
- tweetphoto_user_request.on_complete do |tweetphoto_user|
58
- tweetphotodata = JSON.parse(tweetphoto_user.body)
87
+ tweetphoto_username_request = Typhoeus::Request.new("#{TWEETPHOTO_URL}#{tweetphoto_id}/photos", :timeout => 5000)
88
+ tweetphoto_username_request.on_complete do |tweetphoto_username|
89
+ tweetphotodata = JSON.parse(tweetphoto_username.body)
59
90
  tweetphotodata['List'].each do |tweetphoto|
60
- pic = []
61
- pic << tweetphoto['ThumbnailUrl']
62
- pic << tweetphoto['Message']
63
- pic << tweetphoto['UploadDateString']
64
- pic << tweetphoto['MediumImageUrl']
65
- @pix << pic
91
+ TwitterPhoto.new(
92
+ tweetphoto['ThumbnailUrl'],
93
+ tweetphoto['Message'],
94
+ tweetphoto['UploadDateString'],
95
+ tweetphoto['MediumImageUrl'],
96
+ username
97
+ )
66
98
  end
67
99
  end
68
- hydra.queue tweetphoto_user_request
100
+ hydra.queue tweetphoto_username_request
69
101
  end
70
102
  end
71
103
 
72
- forth_request = Typhoeus::Request.new("http://twitgoo.com/api/user/timeline/#{username}", :params => {:format => 'json'}, :timeout => 5000)
73
- forth_request.on_complete do |response|
104
+ twitgoo_request = Typhoeus::Request.new("#{TWITGOO_URL}#{username}", :params => {:format => 'json'}, :timeout => 5000)
105
+ twitgoo_request.on_complete do |response|
74
106
  if response.code == 200
75
107
  twitgoo = JSON.parse(response.body)
76
108
  twitgoo['media'].each do |twit|
77
- pic = []
78
- pic << twit['thumburl']
79
- pic << twit['text']
80
- pic << twit['created_at']
81
- pic << twit['imageurl']
82
- @pix << pic
109
+ TwitterPhoto.new(
110
+ twit['thumburl'],
111
+ twit['text'],
112
+ twit['created_at'],
113
+ twit['imageurl'],
114
+ username
115
+ )
83
116
  end
84
117
  end
85
118
  end
86
119
 
87
- hydra.queue first_request
88
- hydra.queue second_request
89
- hydra.queue third_request
90
- hydra.queue forth_request
120
+ hydra.queue yfrog_request unless options[:yfrog] == false
121
+ hydra.queue tweetphoto_request unless options[:tweetphoto] == false
122
+ hydra.queue twitpic_request unless options[:twitpic] == false
123
+ hydra.queue twitgoo_request unless options[:twitgoo] == false
91
124
  hydra.run
125
+
126
+ TwitterPhoto.find_by_username(username)
92
127
 
93
- return @pix
94
128
  end
129
+ end
130
+
131
+ module TimeMod
132
+ def time_ago_to_date(tweet_date)
133
+ date = DateTime.now
134
+ intervals = %w(year month week day hour min minute second)
135
+ intervals.each do |interval|
136
+ number = tweet_date.match(/([\d*]+)\s#{interval}/i)
137
+ if number
138
+ interval = 'minutes' if interval == 'min'
139
+ date = date - number[1].to_i.try(interval)
140
+ end
141
+ end
142
+ date
143
+ end
144
+ end
145
+
146
+ class DateTime
147
+ extend TimeMod
95
148
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - bookis
@@ -14,11 +14,11 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-21 00:00:00 -07:00
17
+ date: 2010-05-23 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: thoughtbot-shoulda
21
+ name: nokogiri
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  requirements:
@@ -27,10 +27,10 @@ dependencies:
27
27
  segments:
28
28
  - 0
29
29
  version: "0"
30
- type: :development
30
+ type: :runtime
31
31
  version_requirements: *id001
32
32
  - !ruby/object:Gem::Dependency
33
- name: nokogiri
33
+ name: typhoeus
34
34
  prerelease: false
35
35
  requirement: &id002 !ruby/object:Gem::Requirement
36
36
  requirements:
@@ -41,19 +41,7 @@ dependencies:
41
41
  version: "0"
42
42
  type: :runtime
43
43
  version_requirements: *id002
44
- - !ruby/object:Gem::Dependency
45
- name: typhoeus
46
- prerelease: false
47
- requirement: &id003 !ruby/object:Gem::Requirement
48
- requirements:
49
- - - ">="
50
- - !ruby/object:Gem::Version
51
- segments:
52
- - 0
53
- version: "0"
54
- type: :runtime
55
- version_requirements: *id003
56
- description: This gem allows you to use any twitter username to return an array of photo urls, with corresponding tweets, dates and thumbnails
44
+ description: Allows you to use any twitter username to return a collection of objects of photo urls, with corresponding tweets, dates and thumbnails.
57
45
  email: vegan.bookis@gmail.com
58
46
  executables: []
59
47