flickr 1.0.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4ac455cf4ac4d354d2eb5c169b563c92150c167364bb43b11ce74f631f3a9ff6
4
+ data.tar.gz: 42b38c39f413a14db0422fee1a921eccbb771112abbbda6b5e1178988651f10e
5
+ SHA512:
6
+ metadata.gz: bef5a2e5c09c225a12623dd99018df767ca8b6a77f9fb46b88841293fcbef729be2a381530e117144ad2d613edd4829f7eae9bc204a8409922b72f622e28ea13
7
+ data.tar.gz: 982840a709e68ef01876bbe75f0132681cf80b54fd8510ff9fbd3461b70961f6bc31c53196dfce610817f27d0aed6ff1d837fe5ce6f49e5ecddbe62ced79d110
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2006 Mael Clerambault <maelclerambault@yahoo.fr>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,185 @@
1
+ = Flickr
2
+
3
+ Flickr is a library to access the Flickr[https://flickr.com] API in a simple way.
4
+ It maps exactly the methods described in {the official API documentation}[https://www.flickr.com/services/api/].
5
+ It also tries to present the data returned in a simple and intuitive way.
6
+ The methods are fetched from Flickr when loading the library by using introspection capabilities. So it is always up-to-date with regards to new methods added by Flickr.
7
+
8
+ The github repository: https://github.com/hanklords/flickraw
9
+
10
+ = Installation
11
+ Type this in a console (you might need to be superuser)
12
+
13
+ gem install flickr
14
+
15
+ This will recreate the documentation by fetching the method descriptions from Flickr and then virtually plugging them in standard rdoc documentation.
16
+ $ cd flickr
17
+ $ rake rdoc
18
+
19
+ = Features
20
+
21
+ * Minimal dependencies
22
+ * Complete support of Flickr API. This doesn't require an update of the library
23
+ * Ruby syntax similar to the Flickr API
24
+ * Flickr authentication
25
+ * HTTPS Support
26
+ * Photo upload
27
+ * Proxy support
28
+ * Flickr URLs helpers
29
+
30
+ = Usage
31
+
32
+ == Getting started
33
+
34
+ To use the Flickr API, you must first obtain an API key and shared secret from Flickr. You can do this by logging in to Flickr and creating an application here[https://www.flickr.com/services/apps/create/apply]. API keys are usually granted automatically and instantly.
35
+
36
+ require 'flickr'
37
+
38
+ # The credentials can be provided as parameters:
39
+
40
+ flickr = Flickr.new "YOUR API KEY", "YOUR SHARED SECRET"
41
+
42
+ # Alternatively, if the API key and Shared Secret are not provided, Flickr will attempt to read them
43
+ # from environment variables:
44
+ # ENV['FLICKR_API_KEY']
45
+ # ENV['FLICKR_SHARED_SECRET']
46
+
47
+ flickr = Flickr.new
48
+
49
+ # Flickr will raise an error if either parameter is not explicitly provided, or available via environment variables.
50
+
51
+
52
+ == Simple
53
+
54
+ list = flickr.photos.getRecent
55
+
56
+ id = list[0].id
57
+ secret = list[0].secret
58
+ info = flickr.photos.getInfo :photo_id => id, :secret => secret
59
+
60
+ puts info.title # => "PICT986"
61
+ puts info.dates.taken # => "2006-07-06 15:16:18"
62
+
63
+
64
+ sizes = flickr.photos.getSizes :photo_id => id
65
+
66
+ original = sizes.find { |s| s.label == 'Original' }
67
+ puts original.width # => "800" -- may fail if they have no original marked image
68
+
69
+ == Authentication
70
+
71
+ token = flickr.get_request_token
72
+ auth_url = flickr.get_authorize_url(token['oauth_token'], :perms => 'delete')
73
+
74
+ puts "Open this url in your browser to complete the authentication process: #{auth_url}"
75
+ puts "Copy here the number given when you complete the process."
76
+ verify = gets.strip
77
+
78
+ begin
79
+ flickr.get_access_token(token['oauth_token'], token['oauth_token_secret'], verify)
80
+ login = flickr.test.login
81
+ puts "You are now authenticated as #{login.username} with token #{flickr.access_token} and secret #{flickr.access_secret}"
82
+ rescue Flickr::FailedResponse => e
83
+ puts "Authentication failed : #{e.msg}"
84
+ end
85
+
86
+ If the user has already been authenticated, you can reuse the access token and access secret:
87
+
88
+ flickr.access_token = "... Your access token ..."
89
+ flickr.access_secret = "... Your access secret ..."
90
+
91
+ # From here you are logged:
92
+ login = flickr.test.login
93
+ puts "You are now authenticated as #{login.username}"
94
+
95
+ If you need to have several users authenticated at the same time in your application (ex: a public web application) you need to create separate Flickr objects since it keeps the authentication data internally.
96
+
97
+ flickr = Flickr.new
98
+
99
+ == Upload
100
+
101
+ PHOTO_PATH = 'photo.jpg'
102
+
103
+ # You need to be authenticated to do that, see the previous examples.
104
+ flickr.upload_photo PHOTO_PATH, :title => "Title", :description => "This is the description"
105
+
106
+ == Caching
107
+
108
+ The first time the Flickr object is instantiated, it will download the current Flickr API definition and dynamically create all the required classes and objects. This is how the gem remains up-to-date without requiring updates.
109
+
110
+ Unfortunately this adds a significant delay to startup, but the Flickr gem can be configured to cache the API definition to a local file. Just set a file location before the Flickr class is instantiated:
111
+
112
+ Flickr.cache = '/tmp/flickr-api.yml'
113
+ flickr = Flickr.new
114
+
115
+ == Proxy
116
+
117
+ require 'flickr'
118
+ Flickr.proxy = "https://user:pass@proxy.example.com:3129/"
119
+
120
+ === Server Certificate Verification
121
+
122
+ Server certificate verification is enabled by default. If you don't want to check the server certificate:
123
+
124
+ require 'flickr'
125
+ Flickr.check_certificate = false
126
+
127
+ === CA Certificate File Path
128
+
129
+ OpenSSL::X509::DEFAULT_CERT_FILE is used as a CA certificate file. If you want to change the path:
130
+
131
+ require 'flickr'
132
+ Flickr.ca_file = '/path/to/cacert.pem'
133
+
134
+ You can also specify a path to a directory with a number of certifications:
135
+
136
+ Flickr.ca_path = '/path/to/certificates'
137
+
138
+ == Flickr URL Helpers
139
+
140
+ There are some helpers to build Flickr urls:
141
+
142
+ === url, url_m, url_s, url_t, url_b, url_z, url_q, url_n, url_c, url_o
143
+
144
+ # url_s : Square
145
+ # url_q : Large Square
146
+ # url_t : Thumbnail
147
+ # url_m : Small
148
+ # url_n : Small 320
149
+ # url : Medium
150
+ # url_z : Medium 640
151
+ # url_c : Medium 800
152
+ # url_b : Large
153
+ # url_o : Original
154
+
155
+ info = flickr.photos.getInfo(:photo_id => "3839885270")
156
+ Flickr.url_b(info) # => "https://farm3.static.flickr.com/2485/3839885270_6fb8b54e06_b.jpg"
157
+
158
+ === url_profile
159
+
160
+ info = flickr.photos.getInfo(:photo_id => "3839885270")
161
+ Flickr.url_profile(info) # => "https://www.flickr.com/people/41650587@N02/"
162
+
163
+ === url_photopage
164
+
165
+ info = flickr.photos.getInfo(:photo_id => "3839885270")
166
+ Flickr.url_photopage(info) # => "https://www.flickr.com/photos/41650587@N02/3839885270"
167
+
168
+ === url_photoset, url_photosets
169
+
170
+ info = flickr.photos.getInfo(:photo_id => "3839885270")
171
+ Flickr.url_photosets(info) # => "https://www.flickr.com/photos/41650587@N02/sets/"
172
+
173
+ === url_short, url_short_m, url_short_s, url_short_t
174
+
175
+ info = flickr.photos.getInfo(:photo_id => "3839885270")
176
+ Flickr.url_short(info) # => "https://flic.kr/p/6Rjq7s"
177
+
178
+ === url_photostream
179
+
180
+ info = flickr.photos.getInfo(:photo_id => "3839885270")
181
+ Flickr.url_photostream(info) # => "https://www.flickr.com/photos/41650587@N02/"
182
+
183
+ == Examples
184
+
185
+ See the _examples_ directory to find more examples.
data/examples/auth.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'flickr'
2
+
3
+ # This is how to authenticate on flickr website.
4
+ # You need an API key for that, see https://www.flickr.com/services/api/keys/
5
+ API_KEY = ''
6
+ SHARED_SECRET = ''
7
+
8
+ flickr = Flickr.new API_KEY, SHARED_SECRET
9
+ token = flickr.get_request_token
10
+ auth_url = flickr.get_authorize_url(token['oauth_token'], :perms => 'delete')
11
+
12
+ puts "Open this url in your browser to complete the authentication process: #{auth_url}"
13
+ puts "Copy here the number given when you complete the process."
14
+ verify = gets.strip
15
+
16
+ begin
17
+ flickr.get_access_token(token['oauth_token'], token['oauth_token_secret'], verify)
18
+ login = flickr.test.login
19
+ puts "You are now authenticated as #{login.username} with token #{flickr.access_token} and secret #{flickr.access_secret}"
20
+ rescue Flickr::FailedResponse => e
21
+ puts "Authentication failed: #{e.msg}"
22
+ end
@@ -0,0 +1,7 @@
1
+ require 'flickr'
2
+
3
+ # Get the list of the 20 most recent 'interesting photos'
4
+
5
+ flickr = Flickr.new
6
+ list = flickr.interestingness.getList :per_page => 20
7
+ list.each { |photo| puts "'#{photo.title}' id=#{photo.id} secret=#{photo.secret}" }
@@ -0,0 +1,20 @@
1
+ require 'flickr'
2
+
3
+ # search for pictures taken within 60 miles of new brunswick, between 1890-1920
4
+
5
+ flickr = Flickr.new
6
+ new_b = flickr.places.find :query => "new brunswick"
7
+ latitude = new_b[0]['latitude'].to_f
8
+ longitude = new_b[0]['longitude'].to_f
9
+
10
+ # within 60 miles of new brunswick, let's use a bbox
11
+ radius = 1
12
+ args = {}
13
+ args[:bbox] = "#{longitude - radius},#{latitude - radius},#{longitude + radius},#{latitude + radius}"
14
+
15
+ # requires a limiting factor, so let's give it one
16
+ args[:min_taken_date] = '1890-01-01 00:00:00'
17
+ args[:max_taken_date] = '1920-01-01 00:00:00'
18
+ args[:accuracy] = 1 # the default is street only granularity [16], which most images aren't...
19
+ discovered_pictures = flickr.photos.search args
20
+ discovered_pictures.each { |p| url = Flickr.url p; puts url }
@@ -0,0 +1,31 @@
1
+ require 'flickr'
2
+ require 'sinatra'
3
+
4
+ API_KEY = ''
5
+ SHARED_SECRET = ''
6
+ use Rack::Session::Pool
7
+
8
+ get '/authenticate' do
9
+ flickr = Flickr.new API_KEY, SHARED_SECRET
10
+ token = flickr.get_request_token(:oauth_callback => to('check'))
11
+ session[:token] = token
12
+ redirect flickr.get_authorize_url(token['oauth_token'], :perms => 'delete')
13
+ end
14
+
15
+ get '/check' do
16
+ token = session.delete :token
17
+ session[:auth_flickr] = @auth_flickr = Flickr.new
18
+ @auth_flickr.get_access_token(token['oauth_token'], token['oauth_token_secret'], params['oauth_verifier'])
19
+
20
+ redirect to('/authenticated')
21
+ end
22
+
23
+ get '/authenticated' do
24
+ @auth_flickr = session[:auth_flickr]
25
+
26
+ login = @auth_flickr.test.login
27
+ %{
28
+ You are now authenticated as <em>#{login.username}</em>
29
+ with token <strong>#{@auth_flickr.access_token}</strong> and secret <strong>#{@auth_flickr.access_secret}</strong>.
30
+ }
31
+ end
@@ -0,0 +1,16 @@
1
+ require 'flickr'
2
+
3
+ # This is how to upload photos on flickr.
4
+ # You need to be authenticated to do that.
5
+
6
+ API_KEY = ''
7
+ SHARED_SECRET = ''
8
+ ACCESS_TOKEN = ''
9
+ ACCESS_SECRET = ''
10
+ PHOTO_PATH = 'photo.jpg'
11
+
12
+ flickr = Flickr.new API_KEY, SHARED_SECRET
13
+ flickr.access_token = ACCESS_TOKEN
14
+ flickr.access_secret = ACCESS_SECRET
15
+
16
+ flickr.upload_photo PHOTO_PATH, :title => 'Title', :description => 'This is the description'
@@ -0,0 +1,47 @@
1
+ require 'flickr'
2
+
3
+ # A short howto for web flow authentication on the flickr website.
4
+ # This would live inside a rails controller or alternative framework equivalent
5
+ # Error handling left out for clarity :-)
6
+ # Added by Darren Greaves (https://github.com/boncey/)
7
+ #
8
+ # You need an API key first, see https://www.flickr.com/services/api/keys/
9
+ API_KEY = ''
10
+ SHARED_SECRET = ''
11
+
12
+ # This is the URL Flickr will redirect your users to once they agree to access
13
+ @callback_url = 'http://localhost:3000/auth_controller/callback'
14
+
15
+ # Users should hit this method to get the link which sends them to flickr
16
+ def auth
17
+ flickr = Flickr.new API_KEY, SHARED_SECRET
18
+ token = flickr.get_request_token(:oauth_callback => URI.escape(@callback_url))
19
+ # You'll need to store the token somewhere for when the user is returned to the callback method
20
+ # I stick mine in memcache with their session key as the cache key
21
+
22
+ @auth_url = flickr.get_authorize_url(token['oauth_token'], :perms => 'delete')
23
+ # Stick @auth_url in your template for users to click
24
+ end
25
+
26
+ # Your users browser will be redirected here from Flickr (see @callback_url above)
27
+ def callback
28
+ flickr = Flickr.new
29
+
30
+ request_token = # Retrieve from cache or session etc - see above
31
+ oauth_token = params[:oauth_token]
32
+ oauth_verifier = params[:oauth_verifier]
33
+
34
+ raw_token = flickr.get_access_token(request_token['oauth_token'], request_token['oauth_token_secret'], oauth_verifier)
35
+ # raw_token is a hash like this {"user_nsid"=>"92023420%40N00", "oauth_token_secret"=>"XXXXXX", "username"=>"boncey", "fullname"=>"Darren%20Greaves", "oauth_token"=>"XXXXXX"}
36
+ # Use URI.unescape on the nsid and name parameters
37
+
38
+ oauth_token = raw_token["oauth_token"]
39
+ oauth_token_secret = raw_token["oauth_token_secret"]
40
+
41
+ # Store the oauth_token and oauth_token_secret in session or database
42
+ # and attach to a Flickr instance before calling any methods requiring authentication
43
+
44
+ # Attach the tokens to your flickr instance - you can now make authenticated calls with the flickr object
45
+ flickr.access_token = oauth_token
46
+ flickr.access_secret = oauth_token_secret
47
+ end
data/flickr_rdoc.rb ADDED
@@ -0,0 +1,128 @@
1
+ require "rdoc"
2
+ require "rdoc/parser/ruby"
3
+ require "nokogiri"
4
+
5
+ FLICKR_API_URL = 'https://www.flickr.com/services/api'
6
+
7
+ FakedToken = Struct.new :text, :kind
8
+
9
+ module RDoc
10
+ class FlickrParser < Parser::Ruby
11
+ parse_files_matching(/flickr\.rb$/)
12
+
13
+ def scan
14
+ super
15
+
16
+ fr = @top_level.find_module_named 'Flickr'
17
+ k = fr.add_class NormalClass, 'Flickr', 'Flickr::Request'
18
+ k.record_location @top_level
19
+ @stats.add_class 'Flickr'
20
+
21
+ add_flickr_methods(Flickr, k)
22
+ @top_level
23
+ end
24
+
25
+ private
26
+
27
+ def add_flickr_methods(obj, doc)
28
+ flickr = Flickr.new
29
+ obj.constants.each { |const_name|
30
+ const = obj.const_get const_name
31
+ if const.is_a?(Class) && const < Flickr::Request
32
+ name = const.name.sub(/.*::/, '')
33
+ k = doc.add_class NormalClass, name, 'Flickr::Request'
34
+ k.record_location @top_level
35
+ @stats.add_class name
36
+
37
+ m = AnyMethod.new nil, name.downcase
38
+ m.comment = "Returns a #{name} object."
39
+ m.params = ''
40
+ m.singleton = false
41
+ doc.add_method m
42
+ @stats.add_method m
43
+
44
+ add_flickr_methods(const, k)
45
+ end
46
+ }
47
+
48
+ obj.flickr_methods.each { |name|
49
+ flickr_method = obj.request_name + '.' + name
50
+ info = flickr.reflection.getMethodInfo :method_name => flickr_method
51
+
52
+ m = AnyMethod.new nil, name
53
+ m.comment = flickr_method_comment(info)
54
+ m.params = flickr_method_args(info)
55
+ m.singleton = false
56
+
57
+ m.start_collecting_tokens
58
+ m.add_token FakedToken.new( %{
59
+ # Generated automatically from flickr api
60
+ def #{name}(*args)
61
+ @flickr.call '#{flickr_method}', *args
62
+ end
63
+ } )
64
+ doc.add_method m
65
+ @stats.add_method m
66
+ }
67
+ end
68
+
69
+ def flickr_method_comment(info)
70
+ description = Nokogiri::HTML(info.method.description.to_s).text
71
+
72
+ if info.respond_to? :arguments
73
+ args = info.arguments.select { |arg| arg.name != 'api_key' }
74
+
75
+ arguments = "<b>Arguments</b>\n"
76
+ if args.size > 0
77
+ args.each { |arg|
78
+ arguments << "[#{arg.name} "
79
+ arguments << "<em>(required)</em> " if arg.optional == '0'
80
+ arguments << "] "
81
+ arguments << "#{Nokogiri::HTML(arg.to_s).text}\n"
82
+ }
83
+ end
84
+ end
85
+
86
+ if info.respond_to? :errors
87
+ errors = "<b>Error codes</b>\n"
88
+ info.errors.each { |e|
89
+ errors << "* #{e.code}: <em>#{e.message}</em>\n\n"
90
+ errors << " #{Nokogiri::HTML(e.to_s).text}\n"
91
+ }
92
+ end
93
+
94
+ if info.method.respond_to? :response
95
+ response = "<b>Returns</b>\n"
96
+ raw = Nokogiri::HTML(info.method.response.to_s).text
97
+ response << raw.lines.collect { |line| line.insert(0, ' ') }.join
98
+ else
99
+ response = ''
100
+ end
101
+
102
+ str = "{#{info.method.name}}[#{FLICKR_API_URL}/#{info.method.name}.html] request.\n\n"
103
+ str << description << "\n\n"
104
+ str << arguments << "\n\n"
105
+ str << errors << "\n\n"
106
+ str << response << "\n\n"
107
+ end
108
+
109
+ def flickr_method_args(info)
110
+ str = ''
111
+ if info.respond_to? :arguments
112
+ args = info.arguments.select { |arg| arg.name != 'api_key' }
113
+
114
+ if args.size > 0
115
+ str << '('
116
+ args.each { |arg|
117
+ str << ":#{arg.name} => '#{arg.name}'"
118
+ str << ','
119
+ }
120
+ str.chomp! ','
121
+ str << ')'
122
+ end
123
+ end
124
+ str
125
+ end
126
+
127
+ end
128
+ end