def _get(self, operation, post=False, single=False, **args): if not self.user_id or "access_token" not in self.account: logstr = """%s: %s - %s""" % (PROTOCOL_INFO["name"], _("Authentication failed"), "Auth needs updating") log.logger.error("%s", logstr) return [{"error": {"type": "auth", "account": self.account, "message": _("Authentication failed, please re-authorize")}}] args.update({ "access_token": self.account["access_token"] }) sig = "".join("%s=%s" % (k, v) for k, v in sorted(args.items())) args["sig"] = hashlib.md5(sig + self.account["access_token"]).hexdigest() data = network.Download((URL_PREFIX + operation), args, post).get_json() resources.dump(self.account["service"], self.account["id"], data) if isinstance(data, dict) and data.get("error_msg", 0): if "permission" in data["error_msg"]: logstr = """%s: %s - %s""" % (PROTOCOL_INFO["name"], _("Authentication failed"), data["error_msg"]) log.logger.error("%s", logstr) return [{"error": {"type": "auth", "account": self.account, "message": data["error_msg"]}}] elif data["error_code"] == 102: logstr = """%s: %s - %s""" % (PROTOCOL_INFO["name"], _("Session invalid"), data["error_msg"]) log.logger.error("%s", logstr) return [{"error": {"type": "auth", "account": self.account, "message": data["error_msg"]}}] else: logstr = """%s: %s - %s""" % (PROTOCOL_INFO["name"], _("Unknown failure"), data["error_msg"]) log.logger.error("%s", logstr) return [{"error": {"type": "unknown", "account": self.account, "message": data["error_msg"]}}] return data
def _search(self, **args): data = network.Download( "%s/api/search.json" % self.account["url_prefix"], util.compact(args)) data = data.get_json() return [self._result(m) for m in data["results"]]
def _get(self, path, post=False, parse="message", kind="entries", single=False, **args): url = "/".join((URL_PREFIX, path)) data = network.Download(url, util.compact(args), post, self.account["username"], self.account["secret_key"]).get_json() resources.dump(self.account["service"], self.account["id"], data) if isinstance(data, dict) and data.get("errorCode", 0): if "unauthorized" in data["errorCode"]: raise exceptions.GwibberServiceError("auth", self.account["service"], self.account["username"], data["errorCode"]) if single: return [getattr(self, "_%s" % parse)(data)] if parse: return [getattr(self, "_%s" % parse)(m) for m in data["entries"]] else: return [] if parse: data = data[kind][0] if single else data[kind] return [getattr(self, "_%s" % parse)(m) for m in data]
def _get(self, method, parse="message", post=False, **args): args.update({"api_key": API_KEY, "method": method}) data = network.Download(REST_SERVER, args, post).get_json() if parse == "message": return [self._message(m) for m in data["photos"]["photo"]] else: return data
def _get(self, path, parse="message", post=False, single=False, **args): url = "/".join((URL_PREFIX, "api", path)) url += ("&" if "?" in url else "?") + "apikey=%s" % self.account["password"] data = network.Download(url, util.compact(args) or None, post).get_json() resources.dump(self.account["service"], self.account["id"], data) if single: return [getattr(self, "_%s" % parse)(data)] if parse: return [getattr(self, "_%s" % parse)(m) for m in data] else: return []
def send(self, message): args = { "post_method": "microblog", "user_app_key": self.account["secret_key"], "api_key": API_KEY, "body": message } data = network.Download("http://api.ping.fm/v1/user.post", util.compact(args), post=True) return []
def _get(self, path, parse="message", post=False, single=False, **args): if not self.account.has_key("access_token") and not self.account.has_key("secret_token"): log.logger.error("%s unexpected result - %s", PROTOCOL_INFO["name"], _("Account needs to be re-authorized")) return [{"error": {"type": "auth", "account": self.account, "message": _("Account needs to be re-authorized")}}] self.sigmethod = oauth.OAuthSignatureMethod_HMAC_SHA1() self.consumer = oauth.OAuthConsumer("anonymous", "anonymous") self.token = oauth.OAuthToken(self.account["access_token"], self.account["secret_token"]) url = "/".join((self.url_prefix, "api", path)) parameters = util.compact(args) request = oauth.OAuthRequest.from_consumer_and_token(self.consumer, self.token, http_method=post and "POST" or "GET", http_url=url, parameters=parameters) request.sign_request(self.sigmethod, self.consumer, self.token) if post: data = network.Download(request.to_url(), parameters, post).get_json() else: data = network.Download(request.to_url(), None, post).get_json() resources.dump(self.account["service"], self.account["id"], data) if isinstance(data, dict) and data.get("error", 0): log.logger.error("%s failure - %s", PROTOCOL_INFO["name"], data["error"]) if "authenticate" in data["error"]: return [{"error": {"type": "auth", "account": self.account, "message": data["error"]}}] else: return [{"error": {"type": "unknown", "account": self.account, "message": data["error"]}}] elif isinstance(data, str): log.logger.error("%s unexpected result - %s", PROTOCOL_INFO["name"], data) return [{"error": {"type": "unknown", "account": self.account, "message": data}}] if single: return [getattr(self, "_%s" % parse)(data)] if parse: return [getattr(self, "_%s" % parse)(m) for m in data] else: return [] return [self._result(m) for m in data]
def _get(self, path, parse="story", post=False, single=False, **args): url = "/".join((URL_PREFIX, path)) + "?appkey=http://gwibber.com&type=json" data = network.Download(url, util.compact(args) or None, post).get_json() if not data: return [] if "stories" in data: if single: return [getattr(self, "_%s" % parse)(data["stories"])] if parse: return [getattr(self, "_%s" % parse)(m) for m in data["stories"]] else: return [] else: if ("status" in data) and ("message" in data): print "Digg: got message with status %s and message %s" % (data["status"], data["message"]) return []
def _get(self, path, collection="items", parse="message", post=False, single=False, body=None, **args): url = "/".join((URL_PREFIX, path)) args.update({"alt": "json"}) request = oauth.OAuthRequest.from_consumer_and_token(self.consumer, self.token, http_method=post and "POST" or "GET", http_url=url, parameters=util.compact(args)) request.sign_request(self.sigmethod, self.consumer, self.token) data = network.Download(request.to_url(), None, post, header=["Content-Type: application/json"] if body else None, body=body) data = data.get_json() if single: return [getattr(self, "_%s" % parse)(data["data"])] if parse: return [getattr(self, "_%s" % parse)(m) for m in data["data"][collection]] else: return []
def get_avatar_path(url): avatar_cache_dir = realpath(join(CACHE_BASE_DIR, "gwibber", "avatars")) if not isdir(avatar_cache_dir): makedirs(avatar_cache_dir) avatar_cache_image = join(avatar_cache_dir, url.replace("/", "")) if not exists(avatar_cache_image) or len( open(avatar_cache_image, "r").read()) < 1: f = file(avatar_cache_image, "w") data = network.Download(url) f.write(data.get_string()) f.close() img_resize(avatar_cache_image, 48) if len(open(avatar_cache_image, "r").read()) > 0: return avatar_cache_image return None
def _get(self, path, parse="message", post=False, single=False, **args): url = "/".join((URL_PREFIX, path)) data = network.Download(url, util.compact(args) or None, post, self.account["username"], self.account["password"]).get_json() resources.dump(self.account["service"], self.account["id"], data) if not self._check_error(data): return [] checkins = data["checkins"] if single: return [getattr(self, "_%s" % parse)(checkins)] if parse: return [getattr(self, "_%s" % parse)(m) for m in checkins] else: return []
def _search(self, **args): data = network.Download("http://search.twitter.com/search.json", util.compact(args)) data = data.get_json()["results"] return [self._result(m) for m in data]
def _get(self, path, parse="message", post=False, single=False, **args): url = "/".join((API_PREFIX, path)) request = oauth.OAuthRequest.from_consumer_and_token( self.consumer, self.token, http_method=post and "POST" or "GET", http_url=url, parameters=util.compact(args)) request.sign_request(self.sigmethod, self.consumer, self.token) if post: data = network.Download(request.to_url(), util.compact(args), post).get_json() else: data = network.Download(request.to_url(), None, post).get_json() resources.dump(self.account["service"], self.account["id"], data) if isinstance(data, dict) and data.get("errors", 0): if "authenticate" in data["errors"][0]["message"]: logstr = """%s: %s - %s""" % (PROTOCOL_INFO["name"], _("Authentication failed"), error["message"]) log.logger.error("%s", logstr) return [{ "error": { "type": "auth", "account": self.account, "message": data["errors"][0]["message"] } }] else: for error in data["errors"]: logstr = """%s: %s - %s""" % (PROTOCOL_INFO["name"], _("Unknown failure"), error["message"]) return [{ "error": { "type": "unknown", "account": self.account, "message": error["message"] } }] elif isinstance(data, dict) and data.get("error", 0): if "Incorrect signature" in data["error"]: logstr = """%s: %s - %s""" % ( PROTOCOL_INFO["name"], _("Request failed"), data["error"]) log.logger.error("%s", logstr) return [{ "error": { "type": "auth", "account": self.account, "message": data["error"] } }] elif isinstance(data, str): logstr = """%s: %s - %s""" % (PROTOCOL_INFO["name"], _("Request failed"), data) log.logger.error("%s", logstr) return [{ "error": { "type": "request", "account": self.account, "message": data } }] if parse == "list": return [self._list(l) for l in data["lists"]] if single: return [getattr(self, "_%s" % parse)(data)] if parse: return [getattr(self, "_%s" % parse)(m) for m in data] else: return []