From a24627942687ac39c827628bff5b858c9903d0bf Mon Sep 17 00:00:00 2001 From: fzzinchemical Date: Thu, 13 Nov 2025 19:56:28 +0100 Subject: [PATCH] Add environment configuration and update main functionality - Introduced .env.local for environment variables - Updated .gitignore to include new files - Enhanced main.py to utilize environment variables for user credentials - Implemented random sleep delay in API calls and output JSON formatting --- .env.local | 6 ++++++ .gitignore | 4 +++- main.py | 47 ++++++++++++++++++++++++----------------------- 3 files changed, 33 insertions(+), 24 deletions(-) create mode 100644 .env.local diff --git a/.env.local b/.env.local new file mode 100644 index 0000000..dcdbef9 --- /dev/null +++ b/.env.local @@ -0,0 +1,6 @@ +OUTPUT_FILE="" +COOKIES="" +USERNAME="" +PASSWORD="" +EMAIL="" +EMAIL_PASSWORD="" \ No newline at end of file diff --git a/.gitignore b/.gitignore index 6c4ffc4..e66c4b6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .env -.venv/ \ No newline at end of file +.venv/ +accounts.db +tweets.json \ No newline at end of file diff --git a/main.py b/main.py index f090d00..b8674c9 100644 --- a/main.py +++ b/main.py @@ -1,48 +1,49 @@ +import asyncio +import random +from twscrape import API import os +import json import logging from dotenv import load_dotenv -from twscrape import API, gather, set_log_level # Configuration load_dotenv() OUTPUT_FILE = os.getenv("OUTPUT_FILE", "tweets.json") -SLEEP_MIN = 5 -SLEEP_MAX = 10 -LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO").upper() -logging.basicConfig(level=LOG_LEVEL, format="%(asctime)s %(levelname)s %(message)s") +logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s") logger = logging.getLogger(__name__) -COOKIES = {} -async def main(): - - api = API() # or API("path-to.db") – default is `accounts.db` +COOKIES = os.getenv("COOKIES") +async def main(): + api = API() # or API("path-to.db") – default is `accounts.db` # ADD ACCOUNTS (for CLI usage see next readme section) # Option 1. Adding account with cookies (more stable) load_dotenv() cookies = os.getenv("COOKIES") - await api.pool.add_account("user3", "pass3", "u3@mail.com", "mail_pass3", cookies=cookies) + username = os.getenv("USERNAME") + password = os.getenv("PASSWORD") + email = os.getenv("EMAIL") + email_password = os.getenv("EMAIL_PASSWORD") + await api.pool.add_account(username, password, email, email_password, cookies=cookies) await api.pool.login_all() # try to login to receive account cookies # NOTE 2: all methods have `raw` version (returns `httpx.Response` object): - async for rep in api.search_raw("elon musk"): - print(rep.status_code, rep.json()) # rep is `httpx.Response` object + async for rep in api.search("AI"): + _results = [] + try: + _results.append(rep.json()) + except Exception: + _results.append(rep.text) - # change log level, default info - set_log_level("DEBUG") - - # Tweet & User model can be converted to regular dict or json, e.g.: - doc = await api.user_by_id(user_id) # User - doc.dict() # -> python dict - doc.json() # -> json string - -if __name__ == "__main__": - asyncio.run(main()) + await asyncio.sleep(random.uniform(7, 15)) # random delay between 7 and 15 seconds + + with open(OUTPUT_FILE, "w", encoding="utf-8") as f: + json.dump({"tweets": _results}, f, ensure_ascii=False, indent=2) if __name__ == "__main__": - main() \ No newline at end of file + asyncio.run(main()) \ No newline at end of file