Refactor JSON handling and improve error logging in main functionality
This commit is contained in:
20
main.py
20
main.py
@@ -14,7 +14,12 @@ OUTPUT_FILE = os.getenv("OUTPUT_FILE", "tweets.json")
|
|||||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
COOKIES = os.getenv("COOKIES")
|
def check_if_json_exists(file_path):
|
||||||
|
return os.path.isfile(file_path) and os.path.getsize(file_path) > 0
|
||||||
|
|
||||||
|
def load_json(file_path):
|
||||||
|
with open(file_path, "r", encoding="utf-8") as f:
|
||||||
|
return json.load(f)
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
api = API() # or API("path-to.db") – default is `accounts.db`
|
api = API() # or API("path-to.db") – default is `accounts.db`
|
||||||
@@ -32,17 +37,22 @@ async def main():
|
|||||||
await api.pool.login_all() # try to login to receive account cookies
|
await api.pool.login_all() # try to login to receive account cookies
|
||||||
|
|
||||||
# NOTE 2: all methods have `raw` version (returns `httpx.Response` object):
|
# NOTE 2: all methods have `raw` version (returns `httpx.Response` object):
|
||||||
async for rep in api.search("AI"):
|
|
||||||
|
if check_if_json_exists(OUTPUT_FILE):
|
||||||
|
_results = load_json(OUTPUT_FILE).get("tweets", [])
|
||||||
|
else:
|
||||||
_results = []
|
_results = []
|
||||||
|
|
||||||
|
async for rep in api.search("AI", limit=5):
|
||||||
try:
|
try:
|
||||||
_results.append(rep.json())
|
_results.append(rep.json())
|
||||||
except Exception:
|
except Exception:
|
||||||
_results.append(rep.text)
|
logger.error("Failed to parse tweet JSON")
|
||||||
|
|
||||||
await asyncio.sleep(random.uniform(7, 15)) # random delay between 7 and 15 seconds
|
await asyncio.sleep(random.uniform(7, 15)) # random delay between 7 and 15 seconds
|
||||||
|
|
||||||
with open(OUTPUT_FILE, "w", encoding="utf-8") as f:
|
with open(OUTPUT_FILE, "w", encoding="utf-8") as f:
|
||||||
json.dump({"tweets": _results}, f, ensure_ascii=False, indent=2)
|
f.write(json.dumps({"tweets": _results}, ensure_ascii=False, indent=4))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user