This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

add main script

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