From bfa3e4ac12832eb640924897b55115eb5ea5c972 Mon Sep 17 00:00:00 2001 From: OjusWiZard Date: Mon, 16 Dec 2024 21:29:52 +0530 Subject: [PATCH] feat: integrate `TWIKIT_COOKIES` Signed-off-by: OjusWiZard --- operate/services/manage.py | 2 ++ operate/services/utils/memeooorr.py | 19 ++++++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/operate/services/manage.py b/operate/services/manage.py index eb223dc4..bdbf86b4 100644 --- a/operate/services/manage.py +++ b/operate/services/manage.py @@ -575,6 +575,7 @@ def _deploy_service_onchain_from_safe( # pylint: disable=too-many-statements,to "TWIKIT_USERNAME", "TWIKIT_EMAIL", "TWIKIT_PASSWORD", + "TWIKIT_COOKIES", "TWIKIT_COOKIES_PATH", ] ): @@ -591,6 +592,7 @@ def _deploy_service_onchain_from_safe( # pylint: disable=too-many-statements,to username=service.env_variables["TWIKIT_USERNAME"]["value"], email=service.env_variables["TWIKIT_EMAIL"]["value"], password=service.env_variables["TWIKIT_PASSWORD"]["value"], + cookies=service.env_variables["TWIKIT_COOKIES"]["value"], cookies_path=cookies_path, ), "TWIKIT_COOKIES_PATH": str(cookies_path), diff --git a/operate/services/utils/memeooorr.py b/operate/services/utils/memeooorr.py index 7d9c02e0..8ec31abc 100644 --- a/operate/services/utils/memeooorr.py +++ b/operate/services/utils/memeooorr.py @@ -49,7 +49,7 @@ def await_for_cookies() -> dict: async def async_get_twitter_cookies( - username: str, email: str, password: str, cookies_path: Path + username: str, email: str, password: str, cookies: str, cookies_path: Path ) -> Optional[str]: """Verifies that the Twitter credentials are correct and get the cookies""" @@ -59,13 +59,22 @@ async def async_get_twitter_cookies( valid_cookies = False cookies_path.parent.mkdir(exist_ok=True, parents=True) - # If cookies exist, try with those and validate - if cookies_path.exists(): + if not valid_cookies and cookies: + print("Checking the provided cookies") + client.set_cookies(json.loads(cookies)) + user = await client.user() + print(f"User from cookies: {user.screen_name}") + valid_cookies = user.screen_name == username + + # If cookies file exist, try with those and validate + if not valid_cookies and cookies_path.exists(): + print("Checking the cookies file") with open(cookies_path, "r", encoding="utf-8") as cookies_file: cookies = json.load(cookies_file) client.set_cookies(cookies) user = await client.user() + print(f"User from cookies file: {user.screen_name}") valid_cookies = user.screen_name == username if not valid_cookies: @@ -89,9 +98,9 @@ async def async_get_twitter_cookies( def get_twitter_cookies( - username: str, email: str, password: str, cookies_path: Path + username: str, email: str, password: str, cookies: str, cookies_path: Path ) -> Optional[str]: """get_twitter_cookies""" return asyncio.run( - async_get_twitter_cookies(username, email, password, cookies_path) + async_get_twitter_cookies(username, email, password, cookies, cookies_path) )