Skip to content

Commit

Permalink
feat: integrate TWIKIT_COOKIES
Browse files Browse the repository at this point in the history
Signed-off-by: OjusWiZard <[email protected]>
  • Loading branch information
OjusWiZard committed Dec 16, 2024
1 parent af7128a commit bfa3e4a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
2 changes: 2 additions & 0 deletions operate/services/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
):
Expand All @@ -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),
Expand Down
19 changes: 14 additions & 5 deletions operate/services/utils/memeooorr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""

Expand All @@ -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:
Expand All @@ -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)
)

0 comments on commit bfa3e4a

Please sign in to comment.