def _FlickrAPI__send_multipart(self, url, body, progress_callback=None): assert not progress_callback, \ "twisted upload/replace does not support progress callbacks yet" # would be like # http://twistedmatrix.com/pipermail/twisted-web/2007-January/003253.html LOG.debug("Uploading to %s" % url) reply = getPage(url, method="POST", postdata=str(body), headers=dict([body.header()])) return reply
def need_new(token): if token: return token, None # good token, no new frob LOG.debug("Getting frob for new token") rsp = self.auth_getFrob(auth_token=None, format='xmlnode') @rsp.addCallback def valid_frob(rsp): frob = rsp.frob[0].text self.validate_frob(frob, perms) return token, frob return rsp # automatic deferred chaining
def _FlickrAPI__wrap_in_parser(self, wrapped_method, parse_format, *args, **kwargs): if parse_format in rest_parsers and 'format' in kwargs: kwargs['format'] = 'rest' LOG.debug('Wrapping call %s(self, %s, %s)' % (wrapped_method, args, kwargs)) data = wrapped_method(*args, **kwargs) if parse_format not in rest_parsers: return data parser = rest_parsers[parse_format] return data.addCallback(lambda resp: parser(self, resp))
def _FlickrAPI__flickr_call(self, **kwargs): LOG.debug("Calling %s" % kwargs) post_data = self.encode_and_sign(kwargs) if self.cache and self.cache.get(post_data): return defer.succeed(self.cache.get(post_data)) url = "http://" + FlickrAPI.flickr_host + FlickrAPI.flickr_rest_form reply = getPage(url, method="POST", postdata=post_data, headers={ "Content-Type": "application/x-www-form-urlencoded"}) if self.cache is not None: reply.addCallback(self._add_to_cache, post_data) return reply
def getSetPhotos(self, sets, x): """ Gets sets of a given user and all photos belonging to it @param sets: an iterable of set ids @param x: an executor to execute calls in parallel @return: {set:[photo]} """ spmap = {} #[s.get(NID) for s in self.photosets_getList(user_id=nsid).getchildren()[0].getchildren()] for r in x.run_to_results_any(partial(self.photosets_getPhotos, photoset_id=sid) for sid in sets): pset = r.getchildren()[0] sid = pset.get(NID) spmap[sid] = [p.get(NID) for p in pset.getchildren()] LOG.debug("set: got %s photos (%s)" % (len(pset), sid), 6) return spmap
def check(token): if not token: return None # need new one LOG.debug("Trying cached token '%s'" % token) rsp = self.auth_checkToken(auth_token=token, format='xmlnode') @rsp.addCallback def check_get(rsp): tokenPerms = rsp.auth[0].perms[0].text if tokenPerms == "read" and perms != "read": return None # need new elif tokenPerms == "write" and perms == "delete": return None # need new return token # is good @rsp.addErrback def check_err(err): err.trap(FlickrError) LOG.debug("Cached token invalid") self.token_cache.forget() return None # need new return rsp # automatic deferred chaining
def __flickr_call(self, **kwargs): # Use persistent HTTP connections through a thread-local socket from flickrapi import LOG LOG.debug("Calling %s" % kwargs) post_data = self.encode_and_sign(kwargs) # Return value from cache if available if self.cache and self.cache.get(post_data): return self.cache.get(post_data) # Thread-local persistent connection try: if "conn" not in self.thr.__dict__: self.thr.conn = HTTPConnection(FlickrAPI.flickr_host) LOG.debug("connection opened: %s" % FlickrAPI.flickr_host) self.thr.conn.request("POST", FlickrAPI.flickr_rest_form, post_data, {"Content-Type": "application/x-www-form-urlencoded"}) reply = self.thr.conn.getresponse().read() except (ImproperConnectionState, socket.error), e: LOG.debug("connection error: %s" % repr(e)) self.thr.conn.close() del self.thr.conn raise
def check_err(err): err.trap(FlickrError) LOG.debug("Cached token invalid") self.token_cache.forget() return None # need new
def extract_token(rsp): token = rsp.auth[0].token[0].text LOG.debug("get_token: new token '%s'" % token) # store the auth info for next time self.token_cache.token = token return token