-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
43 lines (34 loc) · 1.36 KB
/
utils.py
File metadata and controls
43 lines (34 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import os
from datetime import date, timezone
from time import sleep
from typing import TypeAlias, Callable
from dotenv import load_dotenv
from dateutil import parser as dateparser
from atproto_client.request import Response
APICall: TypeAlias = Callable[[str, str | None, int | None], Response]
def dumb_retry(call: APICall, max_attempts: int = 25, wait: float = 2.0, inc_factor: float = 0.4) -> APICall:
def wrapper(*args, **kwargs):
w = wait
for i in range(max_attempts):
try:
return call(*args, **kwargs)
except Exception as e:
print(f"Error: {e}\nRetrying in {w:.2f} seconds...")
sleep(w)
w += w * inc_factor
else:
raise RuntimeError(f"Failed after {max_attempts} attempts")
return wrapper
def load_credentials(
dotenv_path: str = ".env",
username_var: str = "BLUESKY_USERNAME",
password_var: str = "BLUESKY_PASSWORD",
) -> tuple[str, str]:
load_dotenv(dotenv_path)
username = os.getenv(username_var)
password = os.getenv(password_var)
if username is None or password is None:
raise ValueError(f"Environment variables {username_var} and {password_var} must be set")
return username, password
def parse_utc_date(timestamp: str) -> date:
return dateparser.parse(timestamp).replace(tzinfo=timezone.utc).date()