lrd_twimage 0.1.1
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.
- data/.gitignore +6 -0
- data/Gemfile +4 -0
- data/README.md +50 -0
- data/Rakefile +2 -0
- data/lib/twimage.rb +101 -0
- data/lib/twimage/image.rb +21 -0
- data/lib/twimage/version.rb +3 -0
- data/twimage.gemspec +24 -0
- metadata +103 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# Twimage
|
2
|
+
|
3
|
+
This is LRD's fork of twimage by Rob Cameron. Our fork adds support for lockerz.com image service, and fixes a
|
4
|
+
problem we ran into on some twitpic images.
|
5
|
+
|
6
|
+
Twimage provides an easy way to pull raw images from the various Twitter photo image services (twitpic, yfrog, etc.)
|
7
|
+
|
8
|
+
## Usage
|
9
|
+
|
10
|
+
Add twimage to your Gemfile:
|
11
|
+
|
12
|
+
gem 'lrd_twimage'
|
13
|
+
|
14
|
+
Of without bundler:
|
15
|
+
|
16
|
+
gem install lrd_twimage
|
17
|
+
|
18
|
+
Now simply take the standard URL that is posted to Twitter and tell Twimage to go get it:
|
19
|
+
|
20
|
+
result = Twimage.get('http://instagr.am/p/EHqLG/')
|
21
|
+
|
22
|
+
Twimage will create a Ruby tempfile with the image. To get the tempfile:
|
23
|
+
|
24
|
+
result.tempfile
|
25
|
+
|
26
|
+
Save the image to your local system, upload to S3, etc. As soon as there are no more references to the
|
27
|
+
tempfile in your code it will be unlinked (deleted). There are a couple additional instance variables
|
28
|
+
you have access to...try `result.inspect` to take a look.
|
29
|
+
|
30
|
+
Twimage will follow any redirects that eventually get you to any of the included services. So, if you
|
31
|
+
have a Instagram image behind a Bitly shortened URL, just give the Bitly link to Twimage and he'll
|
32
|
+
(she'll?) take care of the rest.
|
33
|
+
|
34
|
+
Enjoy!
|
35
|
+
|
36
|
+
## Support
|
37
|
+
|
38
|
+
LRDTwimage currently supports the following services:
|
39
|
+
|
40
|
+
* twitpic - http://twitpic.com
|
41
|
+
* yfrog - http://yfrog.com
|
42
|
+
* instagram - http://instagr.am
|
43
|
+
* lockerz - http://lockerz.com
|
44
|
+
|
45
|
+
## Contributing
|
46
|
+
|
47
|
+
To add a parser, fork this repo and then send me a pull request. You should attempt to get the highest resolution
|
48
|
+
version of the image possible, which isn't always available at the link posted to Twitter. Check out the `SERVICES`
|
49
|
+
constant in `twimage.rb` for examples of `lambda`s used to modify the original `service_url` to get to the full res
|
50
|
+
version's URL.
|
data/Rakefile
ADDED
data/lib/twimage.rb
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'httparty'
|
4
|
+
require 'tempfile'
|
5
|
+
|
6
|
+
$:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
7
|
+
require 'twimage/version'
|
8
|
+
require 'twimage/image'
|
9
|
+
|
10
|
+
module Twimage
|
11
|
+
|
12
|
+
class ServiceURLInvalid < StandardError; end # thrown if the service_url returns a 404
|
13
|
+
class ImageNotFound < StandardError; end # thrown if the service_url doesn't contain an expected image tag
|
14
|
+
class ImageURLInvalid < StandardError; end # thrown if the image_url found in the service_url returns a 404
|
15
|
+
|
16
|
+
USER_AGENT = "Twimage #{Twimage::VERSION} http://github.com/cannikin/twimage"
|
17
|
+
SERVICES = [{ :name => :twitpic,
|
18
|
+
:service_match => /twitpic\.com/,
|
19
|
+
:full_url_modifier => lambda { |url| url + '/full' },
|
20
|
+
:image_css_match => 'body > img' },
|
21
|
+
{ :name => :yfrog,
|
22
|
+
:service_match => /yfrog\.com/,
|
23
|
+
:full_url_modifier => lambda { |url| url.gsub(/\.com/, '.com/z') },
|
24
|
+
:image_css_match => '#the-image img' },
|
25
|
+
{ :name => :instagram,
|
26
|
+
:service_match => [/instagr\.am/, /instagram\.com/],
|
27
|
+
:image_css_match => '.photo'},
|
28
|
+
{ :name => :lockerz,
|
29
|
+
:service_match => /lockerz\.com/,
|
30
|
+
:image_css_match => '#photo' }
|
31
|
+
]
|
32
|
+
|
33
|
+
def self.get(url)
|
34
|
+
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
|
35
|
+
service = find_service(service_url) # check the resulting service_url for which service we're hitting
|
36
|
+
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
|
37
|
+
#debugger
|
38
|
+
image_url = get_image_url(service, full_res_service_url) # get the URL to the image
|
39
|
+
#debugger
|
40
|
+
image = get_image(image_url) # get the image itself
|
41
|
+
#debugger
|
42
|
+
p({
|
43
|
+
:url => url,
|
44
|
+
:image_url => image_url,
|
45
|
+
:service_url => service_url
|
46
|
+
})
|
47
|
+
|
48
|
+
|
49
|
+
return Image.new(:service => service[:name], :service_url => service_url, :image_url => image_url, :image => image)
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
# figure out which service this is by matching against regexes
|
54
|
+
def self.find_service(url)
|
55
|
+
return SERVICES.find do |service|
|
56
|
+
[service[:service_match]].flatten.find do |regex|
|
57
|
+
url.match(regex)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
# tear apart the HTML on the returned service page and find the source of the image
|
64
|
+
def self.get_image_url(service, url)
|
65
|
+
# get the content of the image page
|
66
|
+
begin
|
67
|
+
image_tag = Nokogiri::HTML(open(url, 'User-Agent' => USER_AGENT)).css(service[:image_css_match]).first
|
68
|
+
rescue OpenURI::HTTPError
|
69
|
+
raise ServiceURLInvalid, "The service URL #{url} was not found (returned a 404)"
|
70
|
+
end
|
71
|
+
|
72
|
+
# get the URL to the actual image file
|
73
|
+
if image_tag
|
74
|
+
return enforce_protocol(image_tag['src'])
|
75
|
+
else
|
76
|
+
raise ImageNotFound, "The service URL #{url} did not contain an identifiable image"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def self.enforce_protocol(url)
|
81
|
+
if url =~ /^http/
|
82
|
+
url
|
83
|
+
elsif url =~ /^\/\//
|
84
|
+
"http:" + url
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
# download the actual image and put into a tempfile
|
90
|
+
def self.get_image(url)
|
91
|
+
#debugger
|
92
|
+
# get the image itself
|
93
|
+
response = HTTParty.get(url, :headers => { 'User-Agent' => USER_AGENT })
|
94
|
+
if response.code == 200
|
95
|
+
return response.body.force_encoding('utf-8')
|
96
|
+
else
|
97
|
+
raise ImageURLInvalid, "The image_url #{url} was not found (returned a 404)"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Twimage
|
2
|
+
class Image
|
3
|
+
|
4
|
+
attr_reader :service, :service_url, :image_url, :tempfile
|
5
|
+
|
6
|
+
def initialize(options)
|
7
|
+
@service = options[:service]
|
8
|
+
@service_url = options[:service_url]
|
9
|
+
@image_url = options[:image_url]
|
10
|
+
|
11
|
+
begin
|
12
|
+
extension = @image_url.match(/(\.\w+)(\?|$)/)[1]
|
13
|
+
rescue NoMethodError
|
14
|
+
extension = '.jpg' # Lockerz does not put extensions or mime types on their images.
|
15
|
+
end
|
16
|
+
@tempfile = Tempfile.new(['twimage', extension])
|
17
|
+
@tempfile << options[:image]
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
data/twimage.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "twimage/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "lrd_twimage"
|
7
|
+
s.version = Twimage::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Evan Dorn", "Rob Cameron"]
|
10
|
+
s.email = ["evan@lrdesign.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{A gem for pulling images from various Twitter image services}
|
13
|
+
s.description = %q{This gem will programatically grab images from a bunch of the most used Twitter image services.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "twimage"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_dependency 'nokogiri'
|
23
|
+
s.add_dependency 'httparty'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lrd_twimage
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 25
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Evan Dorn
|
14
|
+
- Rob Cameron
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2011-10-19 00:00:00 -07:00
|
20
|
+
default_executable:
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
23
|
+
name: nokogiri
|
24
|
+
prerelease: false
|
25
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 3
|
31
|
+
segments:
|
32
|
+
- 0
|
33
|
+
version: "0"
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: httparty
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
type: :runtime
|
49
|
+
version_requirements: *id002
|
50
|
+
description: This gem will programatically grab images from a bunch of the most used Twitter image services.
|
51
|
+
email:
|
52
|
+
- evan@lrdesign.com
|
53
|
+
executables: []
|
54
|
+
|
55
|
+
extensions: []
|
56
|
+
|
57
|
+
extra_rdoc_files: []
|
58
|
+
|
59
|
+
files:
|
60
|
+
- .gitignore
|
61
|
+
- Gemfile
|
62
|
+
- README.md
|
63
|
+
- Rakefile
|
64
|
+
- lib/twimage.rb
|
65
|
+
- lib/twimage/image.rb
|
66
|
+
- lib/twimage/version.rb
|
67
|
+
- twimage.gemspec
|
68
|
+
has_rdoc: true
|
69
|
+
homepage: ""
|
70
|
+
licenses: []
|
71
|
+
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
hash: 3
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
version: "0"
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
hash: 3
|
92
|
+
segments:
|
93
|
+
- 0
|
94
|
+
version: "0"
|
95
|
+
requirements: []
|
96
|
+
|
97
|
+
rubyforge_project: twimage
|
98
|
+
rubygems_version: 1.4.2
|
99
|
+
signing_key:
|
100
|
+
specification_version: 3
|
101
|
+
summary: A gem for pulling images from various Twitter image services
|
102
|
+
test_files: []
|
103
|
+
|