def handle_result(rpc): try: result = rpc.get_result() except urlfetch.Error: return else: jid = self.jids[id(rpc)] emails_map = parse(result.content) emails = list() for email in emails_map: if not Mail.get_by_key_name(email['id']): if Mail(key_name=email['id']).put(): str = 'From: %(author)s\nTitle: %(title)s\nSummary: %(summary)s\nTime: %(time)s\n%(url)s' % email emails.insert(0, str) if emails: while CapabilitySet('xmpp').is_enabled(): try: xmpp.send_message(jid, '\n\n'.join(emails)) except xmpp.Error: pass else: break
def check(self): u = User.get_by_key_name(self._jid) if not u: return 'Please bind your account first.' token = oauth.OAuthToken(u.access_key, u.access_secret) consumer = oauth.OAuthConsumer(config.OAUTH_CONSUMER_KEY, config.OAUTH_CONSUMER_SECRET) oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, token=token, http_url=config.RESOURCE_URL) signature_method_hmac_sha1 = oauth.OAuthSignatureMethod_HMAC_SHA1() oauth_request.sign_request(signature_method_hmac_sha1, consumer, token) result = urlfetch.fetch(oauth_request.http_url, headers=oauth_request.to_header()) emails_map = parse(result.content) emails = list() for email in emails_map: str = 'From: %(author)s\nTitle: %(title)s\nSummary: %(summary)s\nTime: %(time)s\n%(url)s' % email emails.insert(0, str) if db.WRITE_CAPABILITY: try: Mail(key_name=email['id']).put() except db.BadKeyError: pass if emails: return '\n\n'.join(emails) else: return 'No new emails.'