Commit b9c52313 authored by Alp Deniz Ogut's avatar Alp Deniz Ogut
Browse files

Init daemon mode

parent 2a6230b6
Showing with 32 additions and 9 deletions
+32 -9
......@@ -41,13 +41,20 @@ class WhatsappPlugin(PluginBase):
return
try:
self.get_me_profile_picture()
self.collect_picture(self.me_id)
self.collect_chat_messages()
self.collect_pictures()
except KeyboardInterrupt:
print("Keyboard interrupt")
except Exception as e:
print("Error during import:", e)
finally:
print("[+] Whatsapp plugin import complete")
try:
self.setup_daemon_mode()
except Exception as e:
print("Error while listening whatsapp", e)
finally:
self.socket.close()
print("[+] Whatsapp plugin run complete")
......@@ -86,15 +93,25 @@ class WhatsappPlugin(PluginBase):
self.store_credentials()
print("[+] Saved session")
def setup_daemon_mode(self):
self.socket.keepAlive()
self.set_run_status("daemon")
self.setup_run_status_listener()
def setup_run_status_listener(self):
# plugin run listener for daemons
while True:
sleep(1)
run = self.client.get(self.pluginRun.id)
if run.status == "aborted":
print(f"{SERVICE_NAME} plugin is being aborted")
return
def wait_for(self, ctr):
while self.counter[ctr][0] < self.counter[ctr][1]:
sleep(0.2)
sleep(0.5)
print("GOT ALL", ctr.upper())
def get_me_profile_picture(self):
self.socket.get_profile_picture(self.me_id, self.process_profile_picture_response)
self.counter[GOT_PICS][1] += 1
def collect_picture(self, jid):
print(f"\tRequesting profile picture of {jid}", flush=True)
self.socket.get_profile_picture(jid, self.process_profile_picture_response)
......@@ -106,7 +123,7 @@ class WhatsappPlugin(PluginBase):
for contact in self.contacts.values():
# request if not already fetched
if len(contact.profilePicture) == 0:
self.collect_profile_picture(contact.externalId)
self.collect_picture(contact.externalId)
sleep(0.5)
self.wait_for(GOT_PICS)
......@@ -168,6 +185,7 @@ class WhatsappPlugin(PluginBase):
def handle_contacts(self, response_data):
accounts = []
# existing_account_ids = helpers.get_external_ids(type='Account')
_, _, contacts = response_data
for _, contact, _ in contacts:
account = helpers.contact_to_account(contact)
......@@ -248,7 +266,7 @@ class WhatsappPlugin(PluginBase):
photo.externalId = jid
self.client.create_photo(photo)
if jid != self.me_id:
if jid in self.chats:
# set conversation (either group or contact) picture
self.chats[jid].add_edge('photo', photo)
self.chats[jid].update(self.client)
......
......@@ -6,7 +6,7 @@ sys.dont_write_bytecode = True;
import os
import base64;
from threading import Thread
from threading import Thread, Timer
import json
import io
from time import sleep;
......@@ -133,6 +133,11 @@ class WhatsAppWebClient:
except Exception as e:
print("Error while handling incoming message", traceback.format_exc());
def keepAlive(self):
if self.activeWs is not None:
self.activeWs.send("?,,")
Timer(20.0, self.keepAlive).start()
def handle_init_response(self, message_content, pend):
if not self.logged_in:
try:
......
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