Commit b99a8291 authored by Hiro's avatar Hiro 🏄
Browse files

Finish twitter responder

parent 2082fa2b
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
"email_requests_limit": 30, "email_requests_limit": 30,
"twitter_requests_limit": 1, "twitter_requests_limit": 1,
"sendmail_interval": 10, "sendmail_interval": 10,
"twitter_interval": 10,
"sendmail_addr": "gettor@torproject.org", "sendmail_addr": "gettor@torproject.org",
"sendmail_host": "localhost", "sendmail_host": "localhost",
"sendmail_port": 587, "sendmail_port": 587,
......
...@@ -28,6 +28,7 @@ def run(gettor, app): ...@@ -28,6 +28,7 @@ def run(gettor, app):
settings = options.parse_settings() settings = options.parse_settings()
sendmail = Sendmail(settings) sendmail = Sendmail(settings)
twitterdm = Twitterdm(settings)
log.info("Starting services.") log.info("Starting services.")
sendmail_service = BaseService( sendmail_service = BaseService(
......
...@@ -80,7 +80,7 @@ class TwitterParser(object): ...@@ -80,7 +80,7 @@ class TwitterParser(object):
platforms = self.settings.get("platforms") platforms = self.settings.get("platforms")
languages = [*strings.get_locales().keys()] languages = [*strings.get_locales().keys()]
hid = hashlib.sha256(twitter_id.encode('utf-8')) hid = hashlib.sha256(str(twitter_id).encode('utf-8'))
log.msg( log.msg(
"Request from {}".format(hid.hexdigest()), system="twitter parser" "Request from {}".format(hid.hexdigest()), system="twitter parser"
) )
...@@ -114,7 +114,7 @@ class TwitterParser(object): ...@@ -114,7 +114,7 @@ class TwitterParser(object):
dbname = self.settings.get("dbname") dbname = self.settings.get("dbname")
conn = SQLite3(dbname) conn = SQLite3(dbname)
hid = hashlib.sha256(request['id'].encode('utf-8')) hid = hashlib.sha256(str(request['id']).encode('utf-8'))
# check limits first # check limits first
num_requests = yield conn.get_num_requests( num_requests = yield conn.get_num_requests(
id=hid.hexdigest(), service=request['service'] id=hid.hexdigest(), service=request['service']
...@@ -129,7 +129,7 @@ class TwitterParser(object): ...@@ -129,7 +129,7 @@ class TwitterParser(object):
else: else:
conn.new_request( conn.new_request(
id=request['id'], id=str(request['id']),
command=request['command'], command=request['command'],
platform=request['platform'], platform=request['platform'],
language=request['language'], language=request['language'],
......
...@@ -14,7 +14,7 @@ from __future__ import absolute_import ...@@ -14,7 +14,7 @@ from __future__ import absolute_import
import gettext import gettext
import hashlib import hashlib
import json
import configparser import configparser
...@@ -65,7 +65,7 @@ class Twitterdm(object): ...@@ -65,7 +65,7 @@ class Twitterdm(object):
Errback if we don't/can't send the message. Errback if we don't/can't send the message.
""" """
log.debug("Could not send message.") log.debug("Could not send message.")
raise Error("{}".format(error)) raise RuntimeError("{}".format(error))
def twitterdm(self, twitter_id, message): def twitterdm(self, twitter_id, message):
...@@ -80,9 +80,13 @@ class Twitterdm(object): ...@@ -80,9 +80,13 @@ class Twitterdm(object):
details. details.
""" """
return self.twitter.post_message( post_data = self.twitter.post_message(
twitter_id, message twitter_id, message
).addCallback(self.twitterdm_callback).addErrback(self.twitterdm_errback) )
if post_data.status_code == 200:
self.twitter_callback
else:
self.twitter_errback
@defer.inlineCallbacks @defer.inlineCallbacks
def get_new(self): def get_new(self):
...@@ -96,10 +100,10 @@ class Twitterdm(object): ...@@ -96,10 +100,10 @@ class Twitterdm(object):
for e in data['events']: for e in data['events']:
message_id = { 'id': e['id'], 'twitter_handle': e['message_create']['sender_id'] } message_id = { "id": e['id'], "twitter_handle": e['message_create']['sender_id'] }
log.debug("Parsing message") log.debug("Parsing message")
tp = TwitterParser(settings, message_id) tp = TwitterParser(self.settings, message_id)
yield defer.maybeDeferred( yield defer.maybeDeferred(
tp.parse, e['message_create']['message_data']['text'], message_id tp.parse, e['message_create']['message_data']['text'], message_id
).addCallback(tp.parse_callback).addErrback(tp.parse_errback) ).addCallback(tp.parse_callback).addErrback(tp.parse_errback)
...@@ -119,7 +123,7 @@ class Twitterdm(object): ...@@ -119,7 +123,7 @@ class Twitterdm(object):
log.info("Got new help request.") log.info("Got new help request.")
for request in help_requests: for request in help_requests:
ids = json.load(request[0]) ids = json.loads("{}".format(request[0].replace("'", '"')))
message_id = ids['id'] message_id = ids['id']
twitter_id = ids['twitter_handle'] twitter_id = ids['twitter_handle']
date = request[5] date = request[5]
...@@ -133,7 +137,7 @@ class Twitterdm(object): ...@@ -133,7 +137,7 @@ class Twitterdm(object):
yield self.twitterdm( yield self.twitterdm(
twitter_id=twitter_id, twitter_id=twitter_id,
body=strings._("help_body") message=strings._("help_body")
) )
yield self.conn.update_stats( yield self.conn.update_stats(
...@@ -146,7 +150,7 @@ class Twitterdm(object): ...@@ -146,7 +150,7 @@ class Twitterdm(object):
service="twitter", date=date service="twitter", date=date
) )
except Error as e: except RuntimeError as e:
log.info("Error sending twitter message: {}.".format(e)) log.info("Error sending twitter message: {}.".format(e))
elif link_requests: elif link_requests:
...@@ -219,7 +223,7 @@ class Twitterdm(object): ...@@ -219,7 +223,7 @@ class Twitterdm(object):
service="twitter", date=date service="twitter", date=date
) )
except Error as e: except RuntimeError as e:
log.info("Error sending message: {}.".format(e)) log.info("Error sending message: {}.".format(e))
else: else:
log.debug("No pending twitter requests. Keep waiting.") log.debug("No pending twitter requests. Keep waiting.")
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
# :license: This is Free Software. See LICENSE for license information. # :license: This is Free Software. See LICENSE for license information.
from requests_oauthlib import OAuth1Session from requests_oauthlib import OAuth1Session
import json
class Twitter(object): class Twitter(object):
""" """
...@@ -58,4 +57,5 @@ class Twitter(object): ...@@ -58,4 +57,5 @@ class Twitter(object):
} }
} }
data = self.twitter_client.post(self.twitter_new_message_endpoint, message) data = self.twitter_client.post(twitter_new_message_endpoint, json=message)
return data
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment