def __upload_to_form(self, form_url, filename, callback, **kwargs): '''Uploads a photo - can be used to either upload a new photo or replace an existing one. form_url must be either ``FlickrAPI.flickr_replace_form`` or ``FlickrAPI.flickr_upload_form``. ''' if not filename: raise IllegalArgumentException("filename must be specified") if not self.token_cache.token: raise IllegalArgumentException("Authentication is required") # Figure out the response format format = self.__extract_upload_response_format(kwargs) # Update the arguments with the ones the user won't have to supply arguments = {'auth_token': self.token_cache.token, 'api_key': self.api_key} arguments.update(kwargs) # Convert to UTF-8 if an argument is an Unicode string kwargs = make_utf8(arguments) if self.secret: kwargs["api_sig"] = self.sign(kwargs) url = "https://%s%s" % (self.flickr_host, form_url) # construct POST data body = Multipart() for arg, value in kwargs.iteritems(): part = Part({'name': arg}, value) body.attach(part) with tempfile.TemporaryFile() as temp_file: with open(filename, "rb") as source_file: filepart = FilePart({'name': 'photo'}, filename, self.file_mm, 'image/jpeg') bodyStr = body.get_data() bodyStr += ['--' + body.boundary] bodyStr += filepart.render() temp_file.write('\r\n'.join(bodyStr)) shutil.copyfileobj(source_file, temp_file) temp_file.write('\r\n--' + body.boundary + "--") temp_file.seek(0) self.file_mm = mmap.mmap(temp_file.fileno(), 0, prot=mmap.PROT_READ) retvalue = self.__wrap_in_parser(self.__send_multipart, format, url, body, callback) self.file_mm.close() temp_file.close() return retvalue
def replace(self, filename, photo_id): """Replace an existing photo. Supported parameters: filename name of a file to upload photo_id the ID of the photo to replace """ if not filename: raise IllegalArgumentException("filename must be specified") if not photo_id: raise IllegalArgumentException("photo_id must be specified") args = {'filename': filename, 'photo_id': photo_id, 'auth_token': self.token_cache.token, 'api_key': self.api_key} args = make_utf8(args) if self.secret: args["api_sig"] = self.sign(args) url = "http://" + FlickrAPI.flickr_host + FlickrAPI.flickr_replace_form # construct POST data body = Multipart() for arg, value in args.iteritems(): # No part for the filename if value == 'filename': continue part = Part({'name': arg}, value) body.attach(part) filepart = FilePart({'name': 'photo'}, filename, 'image/jpeg') body.attach(filepart) return self.__send_multipart(url, body)
def __upload_to_form(self, form_url, filename, callback, **kwargs): '''Uploads a photo - can be used to either upload a new photo or replace an existing one. form_url must be either ``FlickrAPI.flickr_replace_form`` or ``FlickrAPI.flickr_upload_form``. ''' if not filename: raise IllegalArgumentException("filename must be specified") if not self.token_cache.token: raise IllegalArgumentException("Authentication is required") # Figure out the response format format = self.__extract_upload_response_format(kwargs) # Update the arguments with the ones the user won't have to supply arguments = {'auth_token': self.token_cache.token, 'api_key': self.api_key} arguments.update(kwargs) # Convert to UTF-8 if an argument is an Unicode string kwargs = make_utf8(arguments) if self.secret: kwargs["api_sig"] = self.sign(kwargs) url = "http://%s%s" % (self.flickr_host, form_url) # construct POST data body = Multipart() for arg, value in kwargs.iteritems(): part = Part({'name': arg}, value) body.attach(part) filepart = FilePart({'name': 'photo'}, filename, 'image/jpeg') body.attach(filepart) return self.__wrap_in_parser(self.__send_multipart, format, url, body, callback)
def __upload_to_form(self, form_url, filename, callback, **kwargs): '''Uploads a photo - can be used to either upload a new photo or replace an existing one. form_url must be either ``FlickrAPI.flickr_replace_form`` or ``FlickrAPI.flickr_upload_form``. ''' if not filename: raise IllegalArgumentException("filename must be specified") if not self.token_cache.token: raise IllegalArgumentException("Authentication is required") # Figure out the response format format = self.__extract_upload_response_format(kwargs) # Update the arguments with the ones the user won't have to supply arguments = { 'auth_token': self.token_cache.token, 'api_key': self.api_key } arguments.update(kwargs) # Convert to UTF-8 if an argument is an Unicode string kwargs = make_utf8(arguments) if self.secret: kwargs["api_sig"] = self.sign(kwargs) url = "http://%s%s" % (self.flickr_host, form_url) # construct POST data body = Multipart() for arg, value in kwargs.iteritems(): part = Part({'name': arg}, value) body.attach(part) filepart = FilePart({'name': 'photo'}, filename, 'image/jpeg') body.attach(filepart) return self.__wrap_in_parser(self.__send_multipart, format, url, body, callback)
def upload(self, filename, callback=None, **arg): """Upload a file to flickr. Be extra careful you spell the parameters correctly, or you will get a rather cryptic "Invalid Signature" error on the upload! Supported parameters: filename name of a file to upload callback method that gets progress reports title title of the photo description description a.k.a. caption of the photo tags space-delimited list of tags, ``'''tag1 tag2 "long tag"'''`` is_public "1" or "0" for a public resp. private photo is_friend "1" or "0" whether friends can see the photo while it's marked as private is_family "1" or "0" whether family can see the photo while it's marked as private The callback method should take two parameters: def callback(progress, done) Progress is a number between 0 and 100, and done is a boolean that's true only when the upload is done. For now, the callback gets a 'done' twice, once for the HTTP headers, once for the body. """ if not filename: raise IllegalArgumentException("filename must be specified") # verify key names required_params = ('api_key', 'auth_token', 'api_sig') optional_params = ('title', 'description', 'tags', 'is_public', 'is_friend', 'is_family') possible_args = required_params + optional_params for a in arg.keys(): if a not in possible_args: raise IllegalArgumentException("Unknown parameter " "'%s' sent to FlickrAPI.upload" % a) arguments = {'auth_token': self.token_cache.token, 'api_key': self.api_key} arguments.update(arg) # Convert to UTF-8 if an argument is an Unicode string arg = make_utf8(arguments) if self.secret: arg["api_sig"] = self.sign(arg) url = "http://" + FlickrAPI.flickr_host + FlickrAPI.flickr_upload_form # construct POST data body = Multipart() for a in required_params + optional_params: if a not in arg: continue part = Part({'name': a}, arg[a]) body.attach(part) filepart = FilePart({'name': 'photo'}, filename, 'image/jpeg') body.attach(filepart) return self.__send_multipart(url, body, callback)