[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [gettor/master] Finish twitter responder



commit b99a82918ebfe61dccd3c448d52219608db96290
Author: hiro <hiro@xxxxxxxxxxxxxx>
Date:   Thu Oct 10 18:55:21 2019 +0200

    Finish twitter responder
---
 gettor.conf.json.example             |  1 +
 gettor/main.py                       |  1 +
 gettor/parse/twitter.py              |  6 +++---
 gettor/services/twitter/twitterdm.py | 24 ++++++++++++++----------
 gettor/utils/twitter.py              |  4 ++--
 5 files changed, 21 insertions(+), 15 deletions(-)

diff --git a/gettor.conf.json.example b/gettor.conf.json.example
index 76450ce..bd5b71f 100644
--- a/gettor.conf.json.example
+++ b/gettor.conf.json.example
@@ -5,6 +5,7 @@
   "email_requests_limit": 30,
   "twitter_requests_limit": 1,
   "sendmail_interval": 10,
+  "twitter_interval": 10,
   "sendmail_addr": "gettor@xxxxxxxxxxxxxx",
   "sendmail_host": "localhost",
   "sendmail_port": 587,
diff --git a/gettor/main.py b/gettor/main.py
index f914deb..9cd18b7 100644
--- a/gettor/main.py
+++ b/gettor/main.py
@@ -28,6 +28,7 @@ def run(gettor, app):
     settings = options.parse_settings()
 
     sendmail = Sendmail(settings)
+    twitterdm = Twitterdm(settings)
 
     log.info("Starting services.")
     sendmail_service = BaseService(
diff --git a/gettor/parse/twitter.py b/gettor/parse/twitter.py
index ea7ad81..82ae80c 100644
--- a/gettor/parse/twitter.py
+++ b/gettor/parse/twitter.py
@@ -80,7 +80,7 @@ class TwitterParser(object):
         platforms = self.settings.get("platforms")
         languages = [*strings.get_locales().keys()]
 
-        hid = hashlib.sha256(twitter_id.encode('utf-8'))
+        hid = hashlib.sha256(str(twitter_id).encode('utf-8'))
         log.msg(
             "Request from {}".format(hid.hexdigest()), system="twitter parser"
         )
@@ -114,7 +114,7 @@ class TwitterParser(object):
             dbname = self.settings.get("dbname")
             conn = SQLite3(dbname)
 
-            hid = hashlib.sha256(request['id'].encode('utf-8'))
+            hid = hashlib.sha256(str(request['id']).encode('utf-8'))
             # check limits first
             num_requests = yield conn.get_num_requests(
                 id=hid.hexdigest(), service=request['service']
@@ -129,7 +129,7 @@ class TwitterParser(object):
 
             else:
                 conn.new_request(
-                    id=request['id'],
+                    id=str(request['id']),
                     command=request['command'],
                     platform=request['platform'],
                     language=request['language'],
diff --git a/gettor/services/twitter/twitterdm.py b/gettor/services/twitter/twitterdm.py
index b346946..abade53 100644
--- a/gettor/services/twitter/twitterdm.py
+++ b/gettor/services/twitter/twitterdm.py
@@ -14,7 +14,7 @@ from __future__ import absolute_import
 
 import gettext
 import hashlib
-
+import json
 
 import configparser
 
@@ -65,7 +65,7 @@ class Twitterdm(object):
         Errback if we don't/can't send the message.
         """
         log.debug("Could not send message.")
-        raise Error("{}".format(error))
+        raise RuntimeError("{}".format(error))
 
 
     def twitterdm(self, twitter_id, message):
@@ -80,9 +80,13 @@ class Twitterdm(object):
         details.
         """
 
-        return self.twitter.post_message(
+        post_data = self.twitter.post_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
     def get_new(self):
@@ -96,10 +100,10 @@ class Twitterdm(object):
 
         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")
-            tp = TwitterParser(settings, message_id)
+            tp = TwitterParser(self.settings, message_id)
             yield defer.maybeDeferred(
                 tp.parse, e['message_create']['message_data']['text'], message_id
             ).addCallback(tp.parse_callback).addErrback(tp.parse_errback)
@@ -119,7 +123,7 @@ class Twitterdm(object):
                 log.info("Got new help request.")
 
                 for request in help_requests:
-                    ids = json.load(request[0])
+                    ids = json.loads("{}".format(request[0].replace("'", '"')))
                     message_id = ids['id']
                     twitter_id = ids['twitter_handle']
                     date = request[5]
@@ -133,7 +137,7 @@ class Twitterdm(object):
 
                     yield self.twitterdm(
                         twitter_id=twitter_id,
-                        body=strings._("help_body")
+                        message=strings._("help_body")
                     )
 
                     yield self.conn.update_stats(
@@ -146,7 +150,7 @@ class Twitterdm(object):
                         service="twitter", date=date
                     )
 
-            except Error as e:
+            except RuntimeError as e:
                 log.info("Error sending twitter message: {}.".format(e))
 
         elif link_requests:
@@ -219,7 +223,7 @@ class Twitterdm(object):
                         service="twitter", date=date
                     )
 
-            except Error as e:
+            except RuntimeError as e:
                 log.info("Error sending message: {}.".format(e))
         else:
             log.debug("No pending twitter requests. Keep waiting.")
diff --git a/gettor/utils/twitter.py b/gettor/utils/twitter.py
index 2e3968b..f8fa90d 100644
--- a/gettor/utils/twitter.py
+++ b/gettor/utils/twitter.py
@@ -11,7 +11,6 @@
 # :license: This is Free Software. See LICENSE for license information.
 
 from requests_oauthlib import OAuth1Session
-import json
 
 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



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits