flickrip 0.0.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,48 @@
1
+ require 'nokogiri'
2
+ require 'open-uri'
3
+
4
+ #require './url_parser'
5
+
6
+ module Flickrip
7
+ class FlickrImage
8
+ def initialize(url)
9
+ @url = url
10
+ end
11
+
12
+ def image_for_size(size="l",allow_redirect=false)
13
+ #TODO: if size is a symbol (.sym?), then convert it to the character representation
14
+ toks = Flickrip::UrlParser.parse @url
15
+ if toks.has_key?(:user) && toks.has_key?(:imageid)
16
+ pageurl = compute_page_url_for_user_imageid_size toks[:user], toks[:imageid], size
17
+ begin
18
+ page = Nokogiri::HTML( open( pageurl, :redirect => allow_redirect ) )
19
+ src = page.css("div#allsizes-photo > img").first.attributes["src"].value
20
+ return open( src )
21
+ rescue Exception => ex
22
+ if ex.class == OpenURI::HTTPRedirect
23
+ # Original image size not found
24
+ end
25
+ # TODO: Raise NonFlickrImageUrl exception
26
+ # TODO: if there is a 404 or 302 error, or a Flickr unauthorized request, then
27
+ # the file size requested does not exist. Raise InvalidFlickrImageSize exception
28
+ end
29
+ else
30
+ # TODO: Raise NonFlickrImageUrl exception
31
+ end
32
+
33
+ end
34
+
35
+ def original_image
36
+ image_for_size "o"
37
+ end
38
+
39
+ def largest_image
40
+ # Let flickr try to redirect us to the largest size image
41
+ image_for_size "o", true
42
+ end
43
+
44
+ def compute_page_url_for_user_imageid_size(user, imageid, size)
45
+ "http://www.flickr.com/photos/#{user}/#{imageid}/sizes/#{size}/"
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,6 @@
1
+
2
+ module Flickrip
3
+ SIZES = %w[o l z m n s t q sq]
4
+ SIZE_KEYS = %w[original large medium2 medium small2 small thumbnail square150 square75]
5
+ end
6
+
@@ -0,0 +1,47 @@
1
+
2
+ module Flickrip
3
+ class UrlParser
4
+ SET_REGEX = /^http.*flickr.*\/(.*)\/sets\/(\d+)/
5
+ IN_SET_REGEX = /^http.*flickr.*\/(.*)\/(\d+)\/in\/set-(\d+)/
6
+ IMAGE_REGEX = /^http.*flickr.*\/(.*)\/(\d+)/
7
+
8
+ def self.parse(url)
9
+ return {} if url.nil?
10
+ # test for "/in-set/"
11
+ g = url.scan IN_SET_REGEX
12
+ if g.count == 1
13
+ return Hash[ [:user,:imageid,:setid].zip g[0] ]
14
+ end
15
+
16
+ # test for "/sets/"
17
+ g = url.scan SET_REGEX
18
+ if g.count == 1
19
+ return Hash[ [:user,:setid].zip g[0] ]
20
+ end
21
+
22
+ # fall back to single image
23
+ g = url.scan IMAGE_REGEX
24
+ if g.count == 1
25
+ return Hash[ [:user,:imageid].zip g[0] ]
26
+ end
27
+
28
+ # if we get here, we didnt have a valid flickr url
29
+ {}
30
+ end
31
+
32
+ def self.is_set?(url)
33
+ h = self.parse(url)
34
+ h.has_key?(:user) && h.has_key?(:setid)
35
+ end
36
+
37
+ def self.is_in_set?(url)
38
+ h = self.parse(url)
39
+ h.has_key?(:user) && h.has_key?(:setid) && h.has_key?(:imageid)
40
+ end
41
+
42
+ def self.is_image?(url)
43
+ h = self.parse(url)
44
+ h.has_key?(:user) && h.has_key?(:imageid)
45
+ end
46
+ end
47
+ end
@@ -1,3 +1,3 @@
1
1
  module Flickrip
2
- VERSION = '0.0.1'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/flickrip.rb CHANGED
@@ -1,3 +1,10 @@
1
- module Flickrip
1
+ require 'flickrip/version'
2
+ require 'flickrip/sizes'
3
+ require 'flickrip/url_parser'
4
+
2
5
 
6
+ module Flickrip
7
+ def self.version_string
8
+ "Flickrip version #{Flickrip::VERSION}"
9
+ end
3
10
  end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe Flickrip::FlickrImage do
4
+ before :each do
5
+ @no_original_image = "http://www.flickr.com/photos/marshallmickelson/5092430080/sizes/l/in/set-72157630506651062/"
6
+ @allows_original_image = "http://www.flickr.com/photos/cdevers/5066504321/"
7
+ end
8
+
9
+ it 'Can download an original size image' do
10
+ i = Flickrip::FlickrImage.new @allows_original_image
11
+ file = i.original_image
12
+ file.class.should == Tempfile
13
+ end
14
+
15
+ it 'Cant download an original size image if Flickr doesnt allow it' do
16
+ i = Flickrip::FlickrImage.new @no_original_image
17
+ file = i.original_image
18
+ file.should be_nil
19
+ end
20
+
21
+ it 'Can download the largest size available' do
22
+ i = Flickrip::FlickrImage.new @no_original_image
23
+ file = i.largest_image
24
+ file.class.should == Tempfile
25
+ end
26
+
27
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Flickrip do
4
+ it 'should return correct version string' do
5
+ Flickrip.version_string.should == "Flickrip version #{Flickrip::VERSION}"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'rspec'
2
+ require 'flickrip'
3
+ require 'flickrip/url_parser'
4
+ require 'flickrip/flickr_image'
5
+
6
+
7
+ RSpec.configure do |config|
8
+ config.color_enabled = true
9
+ config.formatter = 'documentation'
10
+ end
@@ -0,0 +1,93 @@
1
+ require 'spec_helper'
2
+
3
+ describe Flickrip::UrlParser do
4
+ before :each do
5
+ @isseturl_mobile = "http://m.flickr.com/#/photos/d3sign/7441358166/in/set-72157630283819102/"
6
+ @setsurl_mobile = "http://m.flickr.com/#/photos/d3sign/sets/72157630283819102/"
7
+ @imageurl_mobile = "http://m.flickr.com/#/photos/d3sign/7441358166/"
8
+ @photourl_mobile = "http://m.flickr.com/#/photos/d3sign/7441358166/sizes/l/in/photostream/"
9
+
10
+ @junkfurl_mobile = "http://m.flickr.com/#/junk/nothing/t0/s33/here/"
11
+
12
+ #non-mobile
13
+ @isseturl = "http://www.flickr.com/photos/d3sign/7441358166/in/set-72157630283819102/"
14
+ @setsurl = "http://www.flickr.com/photos/d3sign/sets/72157630283819102/"
15
+ @imageurl = "http://www.flickr.com/photos/d3sign/7441358166/"
16
+ @photourl = "http://www.flickr.com/photos/d3sign/7441358166/sizes/l/in/photostream/"
17
+ @junkfurl = "http://www.flickr.com/junk/nothing/t0/s33/here/"
18
+
19
+ @junkurl = "http://www.someothersite.com/test"
20
+
21
+ @insethash = {:user=>"d3sign",:imageid=>"7441358166",:setid=>"72157630283819102"}
22
+ @sethash = {:user=>"d3sign",:setid=>"72157630283819102"}
23
+ @imagehash = {:user=>"d3sign",:imageid=>"7441358166"}
24
+ end
25
+
26
+ it 'Parser cant parse a nil url' do
27
+ Flickrip::UrlParser.parse( nil ).should == {}
28
+ end
29
+
30
+ it 'Parser cant parse a malformed url' do
31
+ Flickrip::UrlParser.parse( "flickr/d3sign/7441358166/" ).should == {}
32
+ end
33
+
34
+
35
+ it 'Parser finds in-set Url on mobile isseturl' do
36
+ Flickrip::UrlParser.parse( @isseturl_mobile ).should == @insethash
37
+ end
38
+
39
+ it 'Parser finds sets Url on mobile setsurl' do
40
+ Flickrip::UrlParser.parse( @setsurl_mobile ).should == @sethash
41
+ end
42
+
43
+ it 'Parser finds image Url on mobile imageurl' do
44
+ Flickrip::UrlParser.parse( @imageurl_mobile ).should == @imagehash
45
+ end
46
+
47
+ it 'Parser finds image Url on photourl' do
48
+ Flickrip::UrlParser.parse( @photourl_mobile ).should == @imagehash
49
+ end
50
+
51
+
52
+ it 'Parser cant parse a junk mobile flickr url' do
53
+ Flickrip::UrlParser.parse( @junkfurl_mobile ).should == {}
54
+ end
55
+
56
+ it 'Parser finds in-set Url on isseturl' do
57
+ Flickrip::UrlParser.parse( @isseturl ).should == @insethash
58
+ end
59
+
60
+ it 'Parser finds sets Url on setsurl' do
61
+ Flickrip::UrlParser.parse( @setsurl ).should == @sethash
62
+ end
63
+
64
+ it 'Parser finds image Url on imageurl' do
65
+ Flickrip::UrlParser.parse( @imageurl ).should == @imagehash
66
+ end
67
+
68
+ it 'Parser finds image Url on photourl' do
69
+ Flickrip::UrlParser.parse( @photourl ).should == @imagehash
70
+ end
71
+
72
+
73
+ it 'Parser cant parse a junk flickr url' do
74
+ Flickrip::UrlParser.parse( @junkfurl ).should == {}
75
+ end
76
+
77
+
78
+ it 'Parser cant parse a junk url' do
79
+ Flickrip::UrlParser.parse( @junkurl ).should == {}
80
+ end
81
+
82
+ it 'Parser can determine setness of a url' do
83
+ Flickrip::UrlParser.is_set?( @setsurl ).should == true
84
+ end
85
+
86
+ it 'Parser can determine insetness of a url' do
87
+ Flickrip::UrlParser.is_in_set?( @isseturl ).should == true
88
+ end
89
+
90
+ it 'Parser can determine imageness of a url' do
91
+ Flickrip::UrlParser.is_image?( @imageurl ).should == true
92
+ end
93
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flickrip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-09 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2012-07-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.5'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '2.5'
14
30
  description: Inspects flickr http/dom, and allows downloading of images and full sets
15
31
  email:
16
32
  - flickrip@0x07.com
@@ -18,8 +34,15 @@ executables: []
18
34
  extensions: []
19
35
  extra_rdoc_files: []
20
36
  files:
37
+ - lib/flickrip/flickr_image.rb
38
+ - lib/flickrip/sizes.rb
39
+ - lib/flickrip/url_parser.rb
21
40
  - lib/flickrip/version.rb
22
41
  - lib/flickrip.rb
42
+ - spec/flickr_image_spec.rb
43
+ - spec/flickrip_spec.rb
44
+ - spec/spec_helper.rb
45
+ - spec/url_parser_spec.rb
23
46
  homepage: http://github.com/marshallmick007/flickrip
24
47
  licenses: []
25
48
  post_install_message:
@@ -44,4 +67,8 @@ rubygems_version: 1.8.23
44
67
  signing_key:
45
68
  specification_version: 3
46
69
  summary: Flickr HTTP parser and image extractor
47
- test_files: []
70
+ test_files:
71
+ - spec/flickr_image_spec.rb
72
+ - spec/flickrip_spec.rb
73
+ - spec/spec_helper.rb
74
+ - spec/url_parser_spec.rb