|
19 | 19 | get_post_replies_count, |
20 | 20 | ) |
21 | 21 | from .utils import convert_to_locale |
| 22 | +from .draft_utils import ensure_drafts_file |
22 | 23 |
|
23 | 24 | ACCESS_TOKEN = os.getenv("ACCESS_TOKEN") |
24 | 25 |
|
25 | 26 | if not ACCESS_TOKEN: |
26 | 27 | print("Error: The required ACCESS_TOKEN is not set. Please set it in your environment or in your .env file.") |
27 | 28 | sys.exit(1) |
28 | 29 |
|
29 | | -# DRAFTS_FILE specifies the file name or path for saving draft data. |
30 | | -# If DRAFTS_FILE does not exist and contains only a simple filename (without any path separator), |
31 | | -# the application uses the XDG Base Directory Specification to determine the cache directory: |
32 | | -# - It first checks for the XDG_CACHE_HOME environment variable. |
33 | | -# - If not set, it defaults to HOME/.cache. |
34 | | -# Then, a sub-folder named "threads-cli" is created within the cache directory, |
35 | | -# and DRAFTS_FILE is placed inside that sub-folder. |
36 | | -# If DRAFTS_FILE exists as a simple filename, it is used as provided. |
37 | | -# If DRAFTS_FILE already contains a path separator (i.e., it's a complete path), |
38 | | -# the application will use it exactly as given. |
39 | | - |
40 | | -# Determine if DRAFTS_FILE not exist and contains a path separator. |
41 | | -if not os.path.exists(DRAFTS_FILE) and os.path.sep not in DRAFTS_FILE: |
42 | | - # Determine the cache directory |
43 | | - # XDG_CACHE_HOME defaults to HOME/.cache if not set |
44 | | - xdg_cache_home = os.getenv('XDG_CACHE_HOME') |
45 | | - if not xdg_cache_home: |
46 | | - home = os.getenv('HOME') |
47 | | - if not home: |
48 | | - raise EnvironmentError("HOME environment variable is not set.") |
49 | | - xdg_cache_home = os.path.join(home, '.cache') |
50 | | - # Define final path: XDG_CACHE_HOME/threads-cli/DRAFTS_FILE |
51 | | - drafts_dir = os.path.join(xdg_cache_home, "threads-cli") |
52 | | - os.makedirs(drafts_dir, exist_ok=True) # Create directory if it doesn't exist |
53 | | - DRAFTS_FILE = os.path.join(drafts_dir, DRAFTS_FILE) |
54 | | -# If it's not there, create an empty JSON file. |
55 | | -if not os.path.exists(DRAFTS_FILE): |
56 | | - with open(DRAFTS_FILE, "w") as f: |
57 | | - # Create an empty list or dict, depending on your needs. |
58 | | - # Here we write an empty dict. |
59 | | - json.dump({}, f) |
| 30 | +# Ensure DRAFTS_FILE path is resolved and the file exists, following XDG Base Directory Specification if necessary. |
| 31 | +DRAFTS_FILE = ensure_drafts_file(DRAFTS_FILE) |
60 | 32 |
|
61 | 33 | HEADERS = { |
62 | 34 | 'Authorization': f'Bearer {ACCESS_TOKEN}' |
|
0 commit comments