Skip to content

Commit f088551

Browse files
authored
Update app.py
- Reorder import. - Import access token and draft file from src/env.py . - Load new empty draft file from xdg cache dir if not exist or from user env DRAFTS_FILE if exist. - if user draft file is filename and not exist , use the user name from xdg cache dir as new file.
1 parent 6a11b2e commit f088551

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

src/app.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,51 @@
1-
import typer
21
import os
32
import threading
3+
from datetime import datetime, timedelta, timezone
44
import json
5+
6+
import typer
57
from rich.console import Console
68
from rich.table import Table
7-
from datetime import datetime, timedelta, timezone
8-
from dotenv import load_dotenv
99

10+
from .env import ACCESS_TOKEN, DRAFTS_FILE
1011
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
1112
from .utils import convert_to_locale
1213

13-
app = typer.Typer()
14-
console = Console()
15-
load_dotenv()
16-
1714
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+
1841
HEADERS = {
1942
'Authorization': f'Bearer {ACCESS_TOKEN}'
2043
}
21-
DRAFTS_FILE = 'drafts.json'
2244
SERVER_PROCESS_TIME = 10
2345

46+
app = typer.Typer()
47+
console = Console()
48+
2449
@app.command()
2550
def get_profile():
2651
"""

0 commit comments

Comments
 (0)