diff options
author | Alex Auvolat <alex@adnab.me> | 2020-03-05 16:59:37 +0100 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2020-03-05 16:59:37 +0100 |
commit | 62dae5a0a2ad9cead08aa13810561e9436d5a40f (patch) | |
tree | e9f219c5dcfce4363014720a48b43941f9c266ef /external | |
parent | b7090546a9baf7ca95f5709ca1ec9f09879078a4 (diff) | |
download | easybridge-62dae5a0a2ad9cead08aa13810561e9436d5a40f.tar.gz easybridge-62dae5a0a2ad9cead08aa13810561e9436d5a40f.zip |
`client_pickle` messenger configuration param
so that a serialized client object can be passed, bypassing the need to re-login at each restart
Diffstat (limited to 'external')
-rwxr-xr-x | external/messenger.py | 49 |
1 files changed, 30 insertions, 19 deletions
diff --git a/external/messenger.py b/external/messenger.py index b5dd2d3..cd175e1 100755 --- a/external/messenger.py +++ b/external/messenger.py @@ -10,6 +10,9 @@ import time import traceback from urllib.parse import unquote as UrlUnquote +import base64 +import getpass +import zlib import hashlib import fbchat @@ -210,27 +213,17 @@ class MessengerBridge: self.init_backlog_length = int(cmd["data"]["initial_backlog"]) client_file = "/tmp/fbclient_" + hashlib.sha224(cmd["data"]["email"].encode("utf-8")).hexdigest() - try: - f = open(client_file, "rb") - self.client = pickle.load(f) - f.close() - sys.stderr.write("(python messenger) using previous client: {}\n".format(client_file)) - except: - self.client = None - - if self.client is None: + if "client_pickle" in cmd["data"]: + data = base64.b64decode(cmd["data"]["client_pickle"]) + data = zlib.decompress(data) + self.client = pickle.loads(data) + else: email, password = cmd["data"]["email"], cmd["data"]["password"] self.client = MessengerBridgeClient(email=email, password=password, max_tries=1) + ## TODO: save client in new client_pickle config value if not self.client.isLoggedIn(): - return {"_type": "rep_error", "error": "Unable to login (?)"} - - try: - f = open(client_file, "wb") - pickle.dump(self.client, f) - f.close() - except: - pass + return {"_type": "rep_error", "error": "Unable to login (invalid pickle?)"} self.client.setBridge(self) @@ -524,7 +517,25 @@ class MessengerBridge: "data": {"name": new_title}, }) +# ---- CLI ---- + +def createClientPickle(): + email = input("Email address of Facebook account: ") + password = getpass.getpass() + client = MessengerBridgeClient(email, password, max_tries=1) + if not client.isLoggedIn(): + print("Could not log in (why???)") + return + print("") + data = pickle.dumps(client) + data = zlib.compress(data) + data = base64.b64encode(data).decode('ascii') + print(data) + if __name__ == "__main__": - bridge = MessengerBridge() - bridge.run() + if "create_client_pickle" in sys.argv: + createClientPickle() + else: + bridge = MessengerBridge() + bridge.run() |