dustin-r2flickr 0.1.1.6 → 0.1.1.7

Sign up to get free protection for your applications and to get access to all the features.
data/lib/flickr/tags.rb CHANGED
@@ -1,59 +1,59 @@
1
1
  require 'flickr/base'
2
2
 
3
3
  class Flickr::Tags < Flickr::APIBase
4
- def getListPhoto(photo)
5
- photo = photo.id if photo.class == Flickr::Photo
6
- res = @flickr.call_method('flickr.tags.getListPhoto',
7
- 'photo_id'=>photo)
8
- xml = res.root
9
- phid = xml.attributes['id']
10
- photo = (photo.class == Flickr::Photo) ? photo :
11
- (@flickr.photo_cache_lookup(phid) ||
12
- Flickr::Photo.new(@flickr,phid))
13
- if xml.elements['tags']
14
- tags = []
15
- xml.elements['tags'].each_element do |el|
16
- tags << Flickr::Tag.from_xml(el,photo)
17
- end
18
- end
19
- photo.tags = tags
20
- return tags
21
- end
4
+ def getListPhoto(photo)
5
+ photo = photo.id if photo.class == Flickr::Photo
6
+ res = @flickr.call_method('flickr.tags.getListPhoto',
7
+ 'photo_id'=>photo)
8
+ xml = res.root
9
+ phid = xml.attributes['id']
10
+ photo = (photo.class == Flickr::Photo) ? photo :
11
+ (@flickr.photo_cache_lookup(phid) ||
12
+ Flickr::Photo.new(@flickr,phid))
13
+ if xml.elements['tags']
14
+ tags = []
15
+ xml.elements['tags'].each_element do |el|
16
+ tags << Flickr::Tag.from_xml(el,photo)
17
+ end
18
+ end
19
+ photo.tags = tags
20
+ return tags
21
+ end
22
22
 
23
- def getListUserPopular(user,count = nil)
24
- user = user.nsid if user.class == Flickr::Person
25
- args = { 'user_id' => user }
26
- args['count'] = count if count
23
+ def getListUserPopular(user,count = nil)
24
+ user = user.nsid if user.class == Flickr::Person
25
+ args = { 'user_id' => user }
26
+ args['count'] = count if count
27
27
 
28
- res = @flickr.call_method('flickr.tags.getListUserPopular',args)
29
- tags = {}
30
- res.elements['/who/tags'].each_element do |tag|
31
- att = tag.attributes
32
- tags[tag.text]=att['count'].to_i
33
- end
34
- return tags
35
- end
28
+ res = @flickr.call_method('flickr.tags.getListUserPopular',args)
29
+ tags = {}
30
+ res.elements['/who/tags'].each_element do |tag|
31
+ att = tag.attributes
32
+ tags[tag.text]=att['count'].to_i
33
+ end
34
+ return tags
35
+ end
36
36
 
37
- def getListUser(user)
38
- user = user.nsid if user.class == Flickr::Person
39
- args = { 'user_id' => user }
37
+ def getListUser(user)
38
+ user = user.nsid if user.class == Flickr::Person
39
+ args = { 'user_id' => user }
40
40
 
41
- res = @flickr.call_method('flickr.tags.getListUser',args)
42
- tags = []
43
- res.elements['/who/tags'].each_element do |tag|
44
- tags << tag.text
45
- end
46
- return tags
47
- end
41
+ res = @flickr.call_method('flickr.tags.getListUser',args)
42
+ tags = []
43
+ res.elements['/who/tags'].each_element do |tag|
44
+ tags << tag.text
45
+ end
46
+ return tags
47
+ end
48
48
 
49
- def getRelated(tag)
50
- args = { 'tag' => tag }
49
+ def getRelated(tag)
50
+ args = { 'tag' => tag }
51
51
 
52
- res = @flickr.call_method('flickr.tags.getRelated',args)
53
- tags = []
54
- res.elements['/tags'].each_element do |tag|
55
- tags << tag.text
56
- end
57
- return tags
58
- end
52
+ res = @flickr.call_method('flickr.tags.getRelated',args)
53
+ tags = []
54
+ res.elements['/tags'].each_element do |tag|
55
+ tags << tag.text
56
+ end
57
+ return tags
58
+ end
59
59
  end
data/lib/flickr/test.rb CHANGED
@@ -1,19 +1,20 @@
1
1
  require 'flickr/base'
2
2
 
3
3
  class Flickr::Test < Flickr::APIBase
4
- # This has to be a Hash
5
- def echo(args)
6
- return @flickr.call_method('flickr.test.echo',args)
7
- end
4
+
5
+ # This has to be a Hash
6
+ def echo(args)
7
+ return @flickr.call_method('flickr.test.echo',args)
8
+ end
8
9
 
9
- def login
10
- res = @flickr.call_method('flickr.test.login')
11
- nsid = res.elements['/user'].attributes['id']
12
- name = res.elements['/user/username'].text
13
- p = @flickr.person_cache_lookup(nsid) ||
14
- Flickr::Person.new(@flickr,nsid,name)
15
- p.name = name
16
- @flickr.person_cache_store(p)
17
- return p
18
- end
10
+ def login
11
+ res = @flickr.call_method('flickr.test.login')
12
+ nsid = res.elements['/user'].attributes['id']
13
+ name = res.elements['/user/username'].text
14
+ p = @flickr.person_cache_lookup(nsid) ||
15
+ Flickr::Person.new(@flickr,nsid,name)
16
+ p.name = name
17
+ @flickr.person_cache_store(p)
18
+ return p
19
+ end
19
20
  end
@@ -2,32 +2,32 @@ require 'flickr/base'
2
2
 
3
3
  class Flickr::BaseTokenCache < Flickr::APIBase
4
4
 
5
- def load_token
6
- nil
7
- end
5
+ def load_token
6
+ nil
7
+ end
8
8
 
9
- def cache_token(token)
10
- nil
11
- end
9
+ def cache_token(token)
10
+ nil
11
+ end
12
12
 
13
13
  end
14
14
 
15
15
  class Flickr::FileTokenCache < Flickr::BaseTokenCache
16
16
 
17
- def initialize(filename)
18
- @cache_file = filename
19
- end
17
+ def initialize(filename)
18
+ @cache_file = filename
19
+ end
20
20
 
21
- def load_token
22
- token = nil
23
- File.open(@cache_file,'r'){ |f| token = f.read }
24
- @token = Flickr::Token.from_xml(REXML::Document.new(token))
25
- rescue Errno::ENOENT
26
- nil
27
- end
21
+ def load_token
22
+ token = nil
23
+ File.open(@cache_file,'r'){ |f| token = f.read }
24
+ @token = Flickr::Token.from_xml(REXML::Document.new(token))
25
+ rescue Errno::ENOENT
26
+ nil
27
+ end
28
28
 
29
- def cache_token(token)
30
- File.open(@cache_file,'w'){ |f| f.write token.to_xml } if token
31
- end
29
+ def cache_token(token)
30
+ File.open(@cache_file,'w'){ |f| f.write token.to_xml } if token
31
+ end
32
32
 
33
- end
33
+ end
@@ -1,9 +1,11 @@
1
1
  require 'flickr/base'
2
2
 
3
3
  class Flickr::Transform < Flickr::APIBase
4
- def rotate(photo,degrees)
5
- photo = photo.id if photo.class == Flickr::Photo
6
- @flickr.call_method('flickr.photos.transform.rotate',
7
- 'photo_id' => photo, 'degrees' => degrees)
8
- end
4
+
5
+ def rotate(photo,degrees)
6
+ photo = photo.id if photo.class == Flickr::Photo
7
+ @flickr.call_method('flickr.photos.transform.rotate',
8
+ 'photo_id' => photo, 'degrees' => degrees)
9
+ end
10
+
9
11
  end
data/lib/flickr/upload.rb CHANGED
@@ -3,58 +3,59 @@ require 'mime/types'
3
3
  require 'net/http'
4
4
 
5
5
  class Flickr::Ticket
6
- attr_reader :id
7
- attr_accessor :complete, :invalid, :photoid
8
-
9
- COMPLETE=[:incomplete,:completed,:failed]
10
-
11
- def initialize(id,upload)
12
- @id = id
13
- @upload = upload
14
- end
15
-
16
- def check
17
- t = @upload.checkTickets(self)[0]
18
- self.complete = t.complete
19
- self.invalid = t.invalid
20
- self.photoid = t.photoid
21
- return t
22
- end
6
+
7
+ attr_reader :id
8
+ attr_accessor :complete, :invalid, :photoid
9
+
10
+ COMPLETE=[:incomplete,:completed,:failed]
11
+
12
+ def initialize(id,upload)
13
+ @id = id
14
+ @upload = upload
15
+ end
16
+
17
+ def check
18
+ t = @upload.checkTickets(self)[0]
19
+ self.complete = t.complete
20
+ self.invalid = t.invalid
21
+ self.photoid = t.photoid
22
+ return t
23
+ end
23
24
  end
24
25
 
25
26
  class Flickr::FormPart
26
- attr_reader :data, :mime_type, :attributes
27
-
28
- def initialize(name,data,mime_type=nil)
29
- @attributes = {}
30
- @attributes['name'] = name
31
- @data = data
32
- @mime_type = mime_type
33
- end
34
-
35
- def to_s
36
- ([ "Content-Disposition: form-data" ] +
37
- attributes.map{|k,v| "#{k}=\"#{v}\""}).
38
- join('; ') + "\r\n"+
39
- (@mime_type ? "Content-Type: #{@mime_type}\r\n" : '')+
40
- "\r\n#{data}"
41
- end
27
+ attr_reader :data, :mime_type, :attributes
28
+
29
+ def initialize(name,data,mime_type=nil)
30
+ @attributes = {}
31
+ @attributes['name'] = name
32
+ @data = data
33
+ @mime_type = mime_type
34
+ end
35
+
36
+ def to_s
37
+ ([ "Content-Disposition: form-data" ] +
38
+ attributes.map{|k,v| "#{k}=\"#{v}\""}).
39
+ join('; ') + "\r\n"+
40
+ (@mime_type ? "Content-Type: #{@mime_type}\r\n" : '')+
41
+ "\r\n#{data}"
42
+ end
42
43
  end
43
44
 
44
45
  class Flickr::MultiPartForm
45
- attr_accessor :boundary, :parts
46
-
47
- def initialize(boundary=nil)
48
- @boundary = boundary ||
49
- "----------------------------Ruby#{rand(1000000000000)}"
50
- @parts = []
51
- end
52
-
53
- def to_s
54
- "--#@boundary\r\n"+
55
- parts.map{|p| p.to_s}.join("\r\n--#@boundary\r\n")+
56
- "\r\n--#@boundary--\r\n"
57
- end
46
+ attr_accessor :boundary, :parts
47
+
48
+ def initialize(boundary=nil)
49
+ @boundary = boundary ||
50
+ "----------------------------Ruby#{rand(1000000000000)}"
51
+ @parts = []
52
+ end
53
+
54
+ def to_s
55
+ "--#@boundary\r\n" + parts.map{|p| p.to_s}.join("\r\n--#@boundary\r\n") +
56
+ "\r\n--#@boundary--\r\n"
57
+ end
58
+
58
59
  end
59
60
 
60
61
  class Flickr::Upload < Flickr::APIBase
@@ -62,144 +63,143 @@ class Flickr::Upload < Flickr::APIBase
62
63
  # TODO: It would probably be better if we wrapped the fault
63
64
  # in something more meaningful. At the very least, a broad
64
65
  # division of errors, such as retryable and fatal.
65
- def error(el)
66
- att = el.attributes
67
- fe = XMLRPC::FaultException.new(att['code'].to_i,
68
- att['msg'])
69
- $stderr.puts "ERR: #{fe.faultString} (#{fe.faultCode})"
70
- raise fe
71
- end
72
-
73
- def prepare_parts(data,mimetype,filename,title=nil,description=nil,
74
- tags=nil, is_public=nil,is_friend=nil,is_family=nil,
75
- sig=nil, async=nil)
76
- parts = []
77
- parts << Flickr::FormPart.new('title',title) if title
78
- parts << Flickr::FormPart.new('description',description) if
79
- description
80
- parts << Flickr::FormPart.new('tags',tags.join(',')) if tags
81
- parts << Flickr::FormPart.new('is_public',
82
- is_public ? '1' : '0') if is_public != nil
83
- parts << Flickr::FormPart.new('is_friend',
84
- is_friend ? '1' : '0') if is_friend != nil
85
- parts << Flickr::FormPart.new('is_family',
86
- is_family ? '1' : '0') if is_family != nil
87
- parts << Flickr::FormPart.new('async',
88
- async ? '1' : '0') if async != nil
89
-
90
- parts << Flickr::FormPart.new('api_key',@flickr.api_key)
91
- parts << Flickr::FormPart.new('auth_token',
92
- @flickr.auth.token.token)
93
- parts << Flickr::FormPart.new('api_sig',sig)
94
-
95
- parts << Flickr::FormPart.new('photo',data,mimetype)
96
- parts.last.attributes['filename'] = filename
97
- return parts
98
- end
99
-
100
- def make_signature(title=nil,description=nil, tags=nil,
101
- is_public=nil,is_friend=nil,is_family=nil,async=nil)
102
- args = {'api_key' => @flickr.api_key,
103
- 'auth_token' => @flickr.auth.token.token}
104
- args['title'] = title if title
105
- args['description'] = description if description
106
- args['tags'] = tags.join(',') if tags
107
- args['is_public'] = (is_public ? '1' : '0') if is_public != nil
108
- args['is_friend'] = (is_friend ? '1' : '0') if is_friend != nil
109
- args['is_family'] = (is_family ? '1' : '0') if is_family != nil
110
- args['async'] = (async ? '1' : '0') if async != nil
111
- args['api_sig'] = @flickr.sign(args)
112
- end
113
-
114
- def send_form(form)
115
- headers = {"Content-Type" =>
116
- "multipart/form-data; boundary=" + form.boundary}
117
-
118
- http = Net::HTTP.new('www.flickr.com', 80)
119
- # http.read_timeout = 900 # 15 minutes max upload time
120
- tries = 3
121
- begin
122
- res=http.post('/services/upload/',form.to_s,headers)
123
- rescue Timeout::Error => err
124
- tries -= 1
125
- $stderr.puts "Timed out, will retry #{tries} more."
126
- retry if tries > 0
127
- raise err
128
- end
129
- return res
130
- end
131
-
132
- def upload_file_async(filename,title=nil,description=nil,tags=nil,
133
- is_public=nil,is_friend=nil,is_family=nil)
134
- mt = MIME::Types.of(filename)
135
- f = File.open(filename,'rb')
136
- data = f.read
137
- f.close
138
- return upload_image_async(data,mt,filename,title,description,
139
- tags, is_public,is_friend,is_family)
140
- end
141
-
142
-
143
- def upload_file(filename,title=nil,description=nil,tags=nil,
144
- is_public=nil,is_friend=nil,is_family=nil)
145
- mt = MIME::Types.of(filename)
146
- f = File.open(filename,'rb')
147
- data = f.read
148
- f.close
149
- return upload_image(data,mt,filename,title,description,tags,
150
- is_public,is_friend,is_family)
151
- end
152
-
153
- def upload_image_async(data,mimetype,filename,title=nil,description=nil,
154
- tags=nil, is_public=nil,is_friend=nil,is_family=nil)
155
- form = Flickr::MultiPartForm.new
156
-
157
- sig = make_signature(title,description, tags, is_public,
158
- is_friend, is_family, true)
159
- form.parts += prepare_parts(data,mimetype,filename,title,
160
- description, tags, is_public, is_friend,
161
- is_family, sig, true)
162
- res = REXML::Document.new(send_form(form).body)
163
- error(res.elements['/rsp/err']) if res.elements['/rsp/err']
164
- t = Flickr::Ticket.new(res.elements['/rsp/ticketid'].text, self)
165
- @flickr.ticket_cache_store(t)
166
- return t
167
- end
168
-
169
- def upload_image(data,mimetype,filename,title=nil,description=nil,
170
- tags=nil, is_public=nil,is_friend=nil,is_family=nil)
171
- form = Flickr::MultiPartForm.new
172
-
173
- sig = make_signature(title,description, tags, is_public,
174
- is_friend, is_family)
175
- form.parts += prepare_parts(data,mimetype,filename,title,
176
- description, tags, is_public, is_friend,
177
- is_family, sig)
178
- res = REXML::Document.new(send_form(form).body)
179
- error(res.elements['/rsp/err']) if res.elements['/rsp/err']
180
- val = res.elements['/rsp/photoid'].text
181
- return val
182
- end
183
-
184
- def checkTickets(tickets)
185
- tickets = [tickets] if tickets.class != Array
186
- targ = tickets.map{|t|
187
- t.id.to_s if t.class == Flickr::Ticket }.join(',')
188
- res = @flickr.call_method('flickr.photos.upload.checkTickets',
189
- 'tickets' => targ)
190
- tickets = []
191
- res.elements['/uploader'].each_element('ticket') do |tick|
192
- att = tick.attributes
193
- tid = att['id']
194
- t = @flickr.ticket_cache_lookup(tid) ||
195
- Flickr::Ticket.new(tid,self)
196
- t.complete = Flickr::Ticket::COMPLETE[att['complete'].to_i]
197
- t.photoid = att['photoid']
198
- t.invalid = true if (att['invalid'] &&
199
- (att['invalid'].to_i == 1))
200
- @flickr.ticket_cache_store(t)
201
- tickets << t
202
- end
203
- return tickets
204
- end
66
+ def error(el)
67
+ att = el.attributes
68
+ fe = XMLRPC::FaultException.new(att['code'].to_i,
69
+ att['msg'])
70
+ $stderr.puts "ERR: #{fe.faultString} (#{fe.faultCode})"
71
+ raise fe
72
+ end
73
+
74
+ def prepare_parts(data,mimetype,filename,title=nil,description=nil,
75
+ tags=nil, is_public=nil,is_friend=nil,is_family=nil,
76
+ sig=nil, async=nil)
77
+ parts = []
78
+ parts << Flickr::FormPart.new('title',title) if title
79
+ parts << Flickr::FormPart.new('description',description) if
80
+ description
81
+ parts << Flickr::FormPart.new('tags',tags.join(',')) if tags
82
+ parts << Flickr::FormPart.new('is_public',
83
+ is_public ? '1' : '0') if is_public != nil
84
+ parts << Flickr::FormPart.new('is_friend',
85
+ is_friend ? '1' : '0') if is_friend != nil
86
+ parts << Flickr::FormPart.new('is_family',
87
+ is_family ? '1' : '0') if is_family != nil
88
+ parts << Flickr::FormPart.new('async',
89
+ async ? '1' : '0') if async != nil
90
+
91
+ parts << Flickr::FormPart.new('api_key',@flickr.api_key)
92
+ parts << Flickr::FormPart.new('auth_token',
93
+ @flickr.auth.token.token)
94
+ parts << Flickr::FormPart.new('api_sig',sig)
95
+
96
+ parts << Flickr::FormPart.new('photo',data,mimetype)
97
+ parts.last.attributes['filename'] = filename
98
+ return parts
99
+ end
100
+
101
+ def make_signature(title=nil,description=nil, tags=nil,
102
+ is_public=nil,is_friend=nil,is_family=nil,async=nil)
103
+ args = {'api_key' => @flickr.api_key,
104
+ 'auth_token' => @flickr.auth.token.token}
105
+ args['title'] = title if title
106
+ args['description'] = description if description
107
+ args['tags'] = tags.join(',') if tags
108
+ args['is_public'] = (is_public ? '1' : '0') if is_public != nil
109
+ args['is_friend'] = (is_friend ? '1' : '0') if is_friend != nil
110
+ args['is_family'] = (is_family ? '1' : '0') if is_family != nil
111
+ args['async'] = (async ? '1' : '0') if async != nil
112
+ args['api_sig'] = @flickr.sign(args)
113
+ end
114
+
115
+ def send_form(form)
116
+ headers = {"Content-Type" => "multipart/form-data; boundary=" + form.boundary}
117
+
118
+ http = Net::HTTP.new('www.flickr.com', 80)
119
+ # http.read_timeout = 900 # 15 minutes max upload time
120
+ tries = 3
121
+ begin
122
+ res=http.post('/services/upload/',form.to_s,headers)
123
+ rescue Timeout::Error => err
124
+ tries -= 1
125
+ $stderr.puts "Timed out, will retry #{tries} more."
126
+ retry if tries > 0
127
+ raise err
128
+ end
129
+ return res
130
+ end
131
+
132
+ def upload_file_async(filename,title=nil,description=nil,tags=nil,
133
+ is_public=nil,is_friend=nil,is_family=nil)
134
+ mt = MIME::Types.of(filename)
135
+ f = File.open(filename,'rb')
136
+ data = f.read
137
+ f.close
138
+ return upload_image_async(data,mt,filename,title,description,
139
+ tags, is_public,is_friend,is_family)
140
+ end
141
+
142
+
143
+ def upload_file(filename,title=nil,description=nil,tags=nil,
144
+ is_public=nil,is_friend=nil,is_family=nil)
145
+ mt = MIME::Types.of(filename)
146
+ f = File.open(filename,'rb')
147
+ data = f.read
148
+ f.close
149
+ return upload_image(data,mt,filename,title,description,tags,
150
+ is_public,is_friend,is_family)
151
+ end
152
+
153
+ def upload_image_async(data,mimetype,filename,title=nil,description=nil,
154
+ tags=nil, is_public=nil,is_friend=nil,is_family=nil)
155
+ form = Flickr::MultiPartForm.new
156
+
157
+ sig = make_signature(title,description, tags, is_public,
158
+ is_friend, is_family, true)
159
+ form.parts += prepare_parts(data,mimetype,filename,title,
160
+ description, tags, is_public, is_friend,
161
+ is_family, sig, true)
162
+ res = REXML::Document.new(send_form(form).body)
163
+ error(res.elements['/rsp/err']) if res.elements['/rsp/err']
164
+ t = Flickr::Ticket.new(res.elements['/rsp/ticketid'].text, self)
165
+ @flickr.ticket_cache_store(t)
166
+ return t
167
+ end
168
+
169
+ def upload_image(data,mimetype,filename,title=nil,description=nil,
170
+ tags=nil, is_public=nil,is_friend=nil,is_family=nil)
171
+ form = Flickr::MultiPartForm.new
172
+
173
+ sig = make_signature(title,description, tags, is_public,
174
+ is_friend, is_family)
175
+ form.parts += prepare_parts(data,mimetype,filename,title,
176
+ description, tags, is_public, is_friend,
177
+ is_family, sig)
178
+ res = REXML::Document.new(send_form(form).body)
179
+ error(res.elements['/rsp/err']) if res.elements['/rsp/err']
180
+ val = res.elements['/rsp/photoid'].text
181
+ return val
182
+ end
183
+
184
+ def checkTickets(tickets)
185
+ tickets = [tickets] if tickets.class != Array
186
+ targ = tickets.map{|t|
187
+ t.id.to_s if t.class == Flickr::Ticket }.join(',')
188
+ res = @flickr.call_method('flickr.photos.upload.checkTickets',
189
+ 'tickets' => targ)
190
+ tickets = []
191
+ res.elements['/uploader'].each_element('ticket') do |tick|
192
+ att = tick.attributes
193
+ tid = att['id']
194
+ t = @flickr.ticket_cache_lookup(tid) ||
195
+ Flickr::Ticket.new(tid,self)
196
+ t.complete = Flickr::Ticket::COMPLETE[att['complete'].to_i]
197
+ t.photoid = att['photoid']
198
+ t.invalid = true if (att['invalid'] &&
199
+ (att['invalid'].to_i == 1))
200
+ @flickr.ticket_cache_store(t)
201
+ tickets << t
202
+ end
203
+ return tickets
204
+ end
205
205
  end