twimage 0.0.13 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.md +10 -2
- data/lib/twimage.rb +4 -3
- data/lib/twimage/version.rb +1 -1
- metadata +2 -3
- data/.rvmrc +0 -1
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -8,7 +8,11 @@ Add twimage to your Gemfile:
|
|
8
8
|
|
9
9
|
gem 'twimage'
|
10
10
|
|
11
|
-
|
11
|
+
Of without bundler:
|
12
|
+
|
13
|
+
gem install twimage
|
14
|
+
|
15
|
+
Now simply take the standard URL that is posted to Twitter and tell Twimage to go get it:
|
12
16
|
|
13
17
|
result = Twimage.get('http://instagr.am/p/EHqLG/')
|
14
18
|
|
@@ -18,7 +22,11 @@ Twimage will create a Ruby tempfile with the image. To get the tempfile:
|
|
18
22
|
|
19
23
|
Save the image to your local system, upload to S3, etc. As soon as there are no more references to the
|
20
24
|
tempfile in your code it will be unlinked (deleted). There are a couple additional instance variables
|
21
|
-
you have access to
|
25
|
+
you have access to...try `result.inspect` to take a look.
|
26
|
+
|
27
|
+
Twimage will follow any redirects that eventually get you to any of the included services. So, if you
|
28
|
+
have a Instagram image behind a Bitly shortened URL, just give the Bitly link to Twimage and he'll
|
29
|
+
(she'll?) take care of the rest.
|
22
30
|
|
23
31
|
Enjoy!
|
24
32
|
|
data/lib/twimage.rb
CHANGED
@@ -13,6 +13,7 @@ module Twimage
|
|
13
13
|
class ImageNotFound < StandardError; end # thrown if the service_url doesn't contain an expected image tag
|
14
14
|
class ImageURLInvalid < StandardError; end # thrown if the image_url found in the service_url returns a 404
|
15
15
|
|
16
|
+
USER_AGENT = "Twimage #{Twimage::VERSION} http://github.com/cannikin/twimage"
|
16
17
|
SERVICES = [{ :name => :twitpic,
|
17
18
|
:service_match => /twitpic\.com/,
|
18
19
|
:full_url_modifier => lambda { |url| url + '/full' },
|
@@ -26,7 +27,7 @@ module Twimage
|
|
26
27
|
:image_css_match => '.photo'}]
|
27
28
|
|
28
29
|
def self.get(url)
|
29
|
-
service_url = HTTParty.get(url).request.path.to_s # first point HTTParty at this URL and follow any redirects to get to the final page
|
30
|
+
service_url = HTTParty.get(url, :headers => { 'User-Agent' => USER_AGENT }).request.path.to_s # first point HTTParty at this URL and follow any redirects to get to the final page
|
30
31
|
service = find_service(service_url) # check the resulting service_url for which service we're hitting
|
31
32
|
full_res_service_url = service[:full_url_modifier] ? service[:full_url_modifier].call(service_url) : service_url # get the full res version of the service_url
|
32
33
|
image_url = get_image_url(service, full_res_service_url) # get the URL to the image
|
@@ -50,7 +51,7 @@ module Twimage
|
|
50
51
|
def self.get_image_url(service, url)
|
51
52
|
# get the content of the image page
|
52
53
|
begin
|
53
|
-
image_tag = Nokogiri::HTML(open(url)).css(service[:image_css_match]).first
|
54
|
+
image_tag = Nokogiri::HTML(open(url, 'User-Agent' => USER_AGENT)).css(service[:image_css_match]).first
|
54
55
|
rescue OpenURI::HTTPError
|
55
56
|
raise ServiceURLInvalid, "The service URL #{url} was not found (returned a 404)"
|
56
57
|
end
|
@@ -67,7 +68,7 @@ module Twimage
|
|
67
68
|
# download the actual image and put into a tempfile
|
68
69
|
def self.get_image(url)
|
69
70
|
# get the image itself
|
70
|
-
response = HTTParty.get(url)
|
71
|
+
response = HTTParty.get(url, :headers => { 'User-Agent' => USER_AGENT })
|
71
72
|
if response.code == 200
|
72
73
|
return response.body.force_encoding('utf-8')
|
73
74
|
else
|
data/lib/twimage/version.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: twimage
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0
|
5
|
+
version: 0.1.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Rob Cameron
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-05-
|
13
|
+
date: 2011-05-28 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -46,7 +46,6 @@ extra_rdoc_files: []
|
|
46
46
|
|
47
47
|
files:
|
48
48
|
- .gitignore
|
49
|
-
- .rvmrc
|
50
49
|
- Gemfile
|
51
50
|
- README.md
|
52
51
|
- Rakefile
|
data/.rvmrc
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
rvm use 1.9.2@twimage && rvm wrapper 1.9.2@twimage textmate
|