|
1 | | -import typer |
2 | 1 | import os |
3 | 2 | import threading |
| 3 | +from datetime import datetime, timedelta, timezone |
4 | 4 | import json |
| 5 | + |
| 6 | +import typer |
5 | 7 | from rich.console import Console |
6 | 8 | from rich.table import Table |
7 | | -from datetime import datetime, timedelta, timezone |
8 | | -from dotenv import load_dotenv |
9 | 9 |
|
| 10 | +from .env import ACCESS_TOKEN, DRAFTS_FILE |
10 | 11 | from .api import get_user_id, get_user_profile, get_user_posts, get_post_insights, fetch_all_posts, create_post, get_post_replies, get_post_replies_count |
11 | 12 | from .utils import convert_to_locale |
12 | 13 |
|
13 | | -app = typer.Typer() |
14 | | -console = Console() |
15 | | -load_dotenv() |
16 | | - |
17 | 14 | ACCESS_TOKEN = os.getenv("ACCESS_TOKEN") |
| 15 | + |
| 16 | +if not ACCESS_TOKEN: |
| 17 | + print("Error: The required ACCESS_TOKEN is not set. Please set it in your environment or in your .env file.") |
| 18 | + sys.exit(1) |
| 19 | + |
| 20 | +# Determine if DRAFTS_FILE contains a path separator. |
| 21 | +if not os.path.exists(DRAFTS_FILE) and os.path.sep not in DRAFTS_FILE: |
| 22 | + # Determine the cache directory |
| 23 | + # XDG_CACHE_HOME defaults to HOME/.cache if not set |
| 24 | + xdg_cache_home = os.getenv('XDG_CACHE_HOME') |
| 25 | + if not xdg_cache_home: |
| 26 | + home = os.getenv('HOME') |
| 27 | + if not home: |
| 28 | + raise EnvironmentError("HOME environment variable is not set.") |
| 29 | + xdg_cache_home = os.path.join(home, '.cache') |
| 30 | + # Define final path: XDG_CACHE_HOME/threads-cli/DRAFTS_FILE |
| 31 | + drafts_dir = os.path.join(xdg_cache_home, "threads-cli") |
| 32 | + os.makedirs(drafts_dir, exist_ok=True) # Create directory if it doesn't exist |
| 33 | + DRAFTS_FILE = os.path.join(drafts_dir, DRAFTS_FILE) |
| 34 | +# If it's not there, create an empty JSON file. |
| 35 | +if not os.path.exists(DRAFTS_FILE): |
| 36 | + with open(DRAFTS_FILE, "w") as f: |
| 37 | + # Create an empty list or dict, depending on your needs. |
| 38 | + # Here we write an empty dict. |
| 39 | + json.dump({}, f) |
| 40 | + |
18 | 41 | HEADERS = { |
19 | 42 | 'Authorization': f'Bearer {ACCESS_TOKEN}' |
20 | 43 | } |
21 | | -DRAFTS_FILE = 'drafts.json' |
22 | 44 | SERVER_PROCESS_TIME = 10 |
23 | 45 |
|
| 46 | +app = typer.Typer() |
| 47 | +console = Console() |
| 48 | + |
24 | 49 | @app.command() |
25 | 50 | def get_profile(): |
26 | 51 | """ |
|
0 commit comments