Class: Flickr::Upload

Inherits:
APIBase show all
Defined in:
lib/flickr/upload.rb

Instance Attribute Summary

Attributes inherited from APIBase

#flickr

Instance Method Summary collapse

Methods inherited from APIBase

#initialize

Constructor Details

This class inherits a constructor from Flickr::APIBase

Instance Method Details

#checkTickets(tickets) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/flickr/upload.rb', line 184

def checkTickets(tickets)
	tickets = [tickets] if tickets.class != Array
	targ = tickets.map{|t|
		t.id.to_s if t.class == Flickr::Ticket }.join(',')
	res = @flickr.call_method('flickr.photos.upload.checkTickets',
		'tickets' => targ)
	tickets = []
	res.elements['/uploader'].each_element('ticket') do |tick|
		att = tick.attributes
		tid = att['id']
		t = @flickr.ticket_cache_lookup(tid) ||
			Flickr::Ticket.new(tid,self)
		t.complete = Flickr::Ticket::COMPLETE[att['complete'].to_i]
		t.photoid = att['photoid']
		t.invalid = true if (att['invalid'] &&
			(att['invalid'].to_i == 1))
		@flickr.ticket_cache_store(t)
		tickets << t
	end
	return tickets
end

#error(el) ⇒ Object

TODO: It would probably be better if we wrapped the fault in something more meaningful. At the very least, a broad division of errors, such as retryable and fatal.



65
66
67
68
69
70
71
# File 'lib/flickr/upload.rb', line 65

def error(el)
	att = el.attributes
	fe = XMLRPC::FaultException.new(att['code'].to_i,
			att['msg'])
	$stderr.puts "ERR: #{fe.faultString} (#{fe.faultCode})"
	raise fe
end

#make_signature(title = nil, description = nil, tags = nil, is_public = nil, is_friend = nil, is_family = nil, async = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/flickr/upload.rb', line 100

def make_signature(title=nil,description=nil, tags=nil,
		is_public=nil,is_friend=nil,is_family=nil,async=nil)
	args = {'api_key' => @flickr.api_key,
		'auth_token' => @flickr.auth.token.token}
	args['title'] = title if title
	args['description'] = description if description
	args['tags'] = tags.join(',') if tags
	args['is_public'] = (is_public ? '1' : '0') if is_public != nil
	args['is_friend'] = (is_friend ? '1' : '0') if is_friend != nil
	args['is_family'] = (is_family ? '1' : '0') if is_family != nil
	args['async'] = (async ? '1' : '0') if async != nil
	args['api_sig'] = @flickr.sign(args)
end

#prepare_parts(data, mimetype, filename, title = nil, description = nil, tags = nil, is_public = nil, is_friend = nil, is_family = nil, sig = nil, async = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/flickr/upload.rb', line 73

def prepare_parts(data,mimetype,filename,title=nil,description=nil,
		tags=nil, is_public=nil,is_friend=nil,is_family=nil,
		sig=nil, async=nil)
	parts = []
	parts << Flickr::FormPart.new('title',title) if title
	parts << Flickr::FormPart.new('description',description) if
		description
	parts << Flickr::FormPart.new('tags',tags.join(',')) if tags
	parts << Flickr::FormPart.new('is_public',
		is_public ?  '1' : '0') if is_public != nil
	parts << Flickr::FormPart.new('is_friend',
		is_friend ?  '1' : '0') if is_friend != nil
	parts << Flickr::FormPart.new('is_family',
		is_family ?  '1' : '0') if is_family != nil
	parts << Flickr::FormPart.new('async',
		async ?  '1' : '0') if async != nil

	parts << Flickr::FormPart.new('api_key',@flickr.api_key)
	parts << Flickr::FormPart.new('auth_token',
			@flickr.auth.token.token)
	parts << Flickr::FormPart.new('api_sig',sig)

	parts << Flickr::FormPart.new('photo',data,mimetype)
	parts.last.attributes['filename'] = filename
	return parts
end

#send_form(form) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/flickr/upload.rb', line 114

def send_form(form)
	headers = {"Content-Type" =>
		"multipart/form-data; boundary=" + form.boundary}

	http = Net::HTTP.new('www.flickr.com', 80)
#		http.read_timeout = 900 # 15 minutes max upload time
	tries = 3
	begin
		res=http.post('/services/upload/',form.to_s,headers)
	rescue Timeout::Error => err
		tries -= 1
		$stderr.puts "Timed out, will retry #{tries} more."
		retry if tries > 0
		raise err
	end
	return res
end

#upload_file(filename, title = nil, description = nil, tags = nil, is_public = nil, is_friend = nil, is_family = nil) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/flickr/upload.rb', line 143

def upload_file(filename,title=nil,description=nil,tags=nil,
		is_public=nil,is_friend=nil,is_family=nil)
	mt = MIME::Types.of(filename)
	f = File.open(filename,'rb')
	data = f.read
	f.close
	return upload_image(data,mt,filename,title,description,tags, 
			is_public,is_friend,is_family)
end

#upload_file_async(filename, title = nil, description = nil, tags = nil, is_public = nil, is_friend = nil, is_family = nil) ⇒ Object



132
133
134
135
136
137
138
139
140
# File 'lib/flickr/upload.rb', line 132

def upload_file_async(filename,title=nil,description=nil,tags=nil,
		is_public=nil,is_friend=nil,is_family=nil)
	mt = MIME::Types.of(filename)
	f = File.open(filename,'rb')
	data = f.read
	f.close
	return upload_image_async(data,mt,filename,title,description,
			tags, is_public,is_friend,is_family)
end

#upload_image(data, mimetype, filename, title = nil, description = nil, tags = nil, is_public = nil, is_friend = nil, is_family = nil) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/flickr/upload.rb', line 169

def upload_image(data,mimetype,filename,title=nil,description=nil,
		tags=nil, is_public=nil,is_friend=nil,is_family=nil)
	form = Flickr::MultiPartForm.new

	sig = make_signature(title,description, tags, is_public,
			is_friend, is_family)
	form.parts += prepare_parts(data,mimetype,filename,title,
			description, tags, is_public, is_friend,
			is_family, sig)
	res = REXML::Document.new(send_form(form).body)
	error(res.elements['/rsp/err']) if res.elements['/rsp/err']
	val = res.elements['/rsp/photoid'].text
	return val
end

#upload_image_async(data, mimetype, filename, title = nil, description = nil, tags = nil, is_public = nil, is_friend = nil, is_family = nil) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/flickr/upload.rb', line 153

def upload_image_async(data,mimetype,filename,title=nil,description=nil,
		tags=nil, is_public=nil,is_friend=nil,is_family=nil)
	form = Flickr::MultiPartForm.new

	sig = make_signature(title,description, tags, is_public,
			is_friend, is_family, true)
	form.parts += prepare_parts(data,mimetype,filename,title,
			description, tags, is_public, is_friend,
			is_family, sig, true)
	res = REXML::Document.new(send_form(form).body)
	error(res.elements['/rsp/err']) if res.elements['/rsp/err']
	t = Flickr::Ticket.new(res.elements['/rsp/ticketid'].text, self)
	@flickr.ticket_cache_store(t)
	return t
end