···
1
1
+
#!/usr/bin/env python3
2
2
+
import os
3
3
+
import json
4
4
+
import logging
5
5
+
from datetime import datetime
6
6
+
from dotenv import load_dotenv
7
7
+
from atproto import Client, models
8
8
+
9
9
+
# Define the paths
10
10
+
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
11
11
+
ASSETS_DIR = os.path.join(BASE_DIR, "../assets")
12
12
+
ENV_PATH = os.path.join(ASSETS_DIR, ".env")
13
13
+
JSON_PATH = os.path.join(ASSETS_DIR, "cids.json")
14
14
+
15
15
+
# Configure logging
16
16
+
logging.basicConfig(
17
17
+
filename="avatar_update.log",
18
18
+
level=logging.DEBUG,
19
19
+
format="%(asctime)s - %(levelname)s - %(message)s",
20
20
+
)
21
21
+
console_handler = logging.StreamHandler()
22
22
+
console_handler.setLevel(logging.INFO)
23
23
+
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
24
24
+
console_handler.setFormatter(formatter)
25
25
+
logging.getLogger().addHandler(console_handler)
26
26
+
27
27
+
def main():
28
28
+
logging.info("Starting avatar update script...")
29
29
+
30
30
+
if os.path.exists(ENV_PATH):
31
31
+
load_dotenv(ENV_PATH)
32
32
+
else:
33
33
+
logging.error(f"Missing .env file at {ENV_PATH}")
34
34
+
return
35
35
+
36
36
+
endpoint = os.getenv("ENDPOINT")
37
37
+
handle = os.getenv("HANDLE")
38
38
+
password = os.getenv("PASSWORD")
39
39
+
40
40
+
if not (endpoint and handle and password):
41
41
+
logging.error("Missing environment variables. Ensure ENDPOINT, HANDLE, and PASSWORD are set in .env file.")
42
42
+
return
43
43
+
44
44
+
try:
45
45
+
with open(JSON_PATH, "r") as f:
46
46
+
blob_dict = json.load(f)
47
47
+
logging.debug(f"Loaded blob CIDs from {JSON_PATH}: {blob_dict}")
48
48
+
except Exception as e:
49
49
+
logging.error(f"Error loading cids.json from {JSON_PATH}: {e}")
50
50
+
return
51
51
+
52
52
+
current_hour = datetime.now().strftime("%H")
53
53
+
logging.info(f"Current hour: {current_hour}")
54
54
+
new_blob_cid = blob_dict.get(current_hour)
55
55
+
if not new_blob_cid:
56
56
+
logging.warning(f"No blob CID found for hour {current_hour}")
57
57
+
return
58
58
+
logging.info(f"Selected blob CID: {new_blob_cid}")
59
59
+
60
60
+
client = Client(endpoint)
61
61
+
62
62
+
try:
63
63
+
profile = client.login(handle, password)
64
64
+
logging.info(f"Authentication successful. Welcome, {profile.display_name}")
65
65
+
did = profile.did
66
66
+
logging.info(f"User DID: {did}")
67
67
+
except Exception as e:
68
68
+
logging.error(f"Authentication failed: {e}")
69
69
+
return
70
70
+
71
71
+
updated_profile_data = {
72
72
+
"$type": "app.bsky.actor.profile",
73
73
+
"avatar": {
74
74
+
"cid": new_blob_cid
75
75
+
}
76
76
+
}
77
77
+
78
78
+
logging.debug(f"Updated profile data: {updated_profile_data}")
79
79
+
80
80
+
try:
81
81
+
client.com.atproto.repo.put_record(
82
82
+
repo=did,
83
83
+
collection="app.bsky.actor.profile",
84
84
+
rkey="self",
85
85
+
record=updated_profile_data
86
86
+
)
87
87
+
logging.info("Avatar updated successfully!")
88
88
+
except Exception as e:
89
89
+
logging.error(f"Failed to update profile record: {e}")
90
90
+
91
91
+
if __name__ == "__main__":
92
92
+
main()