twitter-cli 0.4.5__tar.gz → 0.4.6__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/PKG-INFO +1 -1
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/pyproject.toml +1 -1
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/twitter_cli/auth.py +73 -3
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/twitter_cli/cli.py +11 -5
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/uv.lock +1 -1
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/.github/workflows/ci.yml +0 -0
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/.github/workflows/publish-clawhub.yml +0 -0
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/.github/workflows/publish.yml +0 -0
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/.gitignore +0 -0
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/LICENSE +0 -0
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/README.md +0 -0
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/SKILL.md +0 -0
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/config.yaml +0 -0
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/tests/conftest.py +0 -0
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/tests/test_cli.py +0 -0
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/tests/test_client.py +0 -0
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/tests/test_config.py +0 -0
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/tests/test_config_normalization.py +0 -0
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/tests/test_filter.py +0 -0
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/tests/test_serialization.py +0 -0
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/twitter_cli/__init__.py +0 -0
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/twitter_cli/client.py +0 -0
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/twitter_cli/config.py +0 -0
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/twitter_cli/constants.py +0 -0
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/twitter_cli/filter.py +0 -0
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/twitter_cli/formatter.py +0 -0
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/twitter_cli/models.py +0 -0
- {twitter_cli-0.4.5 → twitter_cli-0.4.6}/twitter_cli/serialization.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: twitter-cli
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.6
|
|
4
4
|
Summary: A CLI for Twitter/X — feed, bookmarks, and user timeline in terminal
|
|
5
5
|
Project-URL: Homepage, https://github.com/jackwener/twitter-cli
|
|
6
6
|
Project-URL: Repository, https://github.com/jackwener/twitter-cli
|
|
@@ -252,7 +252,7 @@ def extract_from_browser() -> Optional[Dict[str, str]]:
|
|
|
252
252
|
|
|
253
253
|
|
|
254
254
|
def get_cookies() -> Dict[str, str]:
|
|
255
|
-
"""Get Twitter cookies. Priority: env vars -> browser extraction
|
|
255
|
+
"""Get Twitter cookies. Priority: env vars -> cache file -> browser extraction.
|
|
256
256
|
|
|
257
257
|
Raises RuntimeError if no cookies found.
|
|
258
258
|
"""
|
|
@@ -263,9 +263,17 @@ def get_cookies() -> Dict[str, str]:
|
|
|
263
263
|
if cookies:
|
|
264
264
|
logger.info("Loaded cookies from environment variables")
|
|
265
265
|
|
|
266
|
-
# 2. Try
|
|
266
|
+
# 2. Try cached cookies (file cache with TTL)
|
|
267
|
+
if not cookies:
|
|
268
|
+
cookies = _load_cookie_cache()
|
|
269
|
+
if cookies:
|
|
270
|
+
logger.info("Loaded cookies from cache")
|
|
271
|
+
|
|
272
|
+
# 3. Try browser extraction (auto-detect)
|
|
267
273
|
if not cookies:
|
|
268
274
|
cookies = extract_from_browser()
|
|
275
|
+
if cookies:
|
|
276
|
+
_save_cookie_cache(cookies)
|
|
269
277
|
|
|
270
278
|
if not cookies:
|
|
271
279
|
raise RuntimeError(
|
|
@@ -275,7 +283,69 @@ def get_cookies() -> Dict[str, str]:
|
|
|
275
283
|
)
|
|
276
284
|
|
|
277
285
|
# Verify only for explicit auth failures; transient endpoint issues are tolerated.
|
|
278
|
-
|
|
286
|
+
try:
|
|
287
|
+
verify_cookies(cookies["auth_token"], cookies["ct0"], cookies.get("cookie_string"))
|
|
288
|
+
except RuntimeError:
|
|
289
|
+
# Auth failure — invalidate cache and re-extract from browser
|
|
290
|
+
logger.info("Cookie verification failed, invalidating cache and re-extracting")
|
|
291
|
+
invalidate_cookie_cache()
|
|
292
|
+
fresh_cookies = extract_from_browser()
|
|
293
|
+
if fresh_cookies:
|
|
294
|
+
_save_cookie_cache(fresh_cookies)
|
|
295
|
+
# Verify fresh cookies — if this also fails, let it raise
|
|
296
|
+
verify_cookies(fresh_cookies["auth_token"], fresh_cookies["ct0"], fresh_cookies.get("cookie_string"))
|
|
297
|
+
return fresh_cookies
|
|
298
|
+
raise
|
|
279
299
|
return cookies
|
|
280
300
|
|
|
281
301
|
|
|
302
|
+
# ── Cookie file cache ───────────────────────────────────────────────────
|
|
303
|
+
|
|
304
|
+
_CACHE_DIR = os.path.join(os.path.expanduser("~"), ".cache", "twitter-cli")
|
|
305
|
+
_CACHE_FILE = os.path.join(_CACHE_DIR, "cookies.json")
|
|
306
|
+
_CACHE_TTL_SECONDS = 24 * 3600 # 24 hours
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
def _load_cookie_cache():
|
|
310
|
+
# type: () -> Optional[Dict[str, str]]
|
|
311
|
+
"""Load cookies from file cache if within TTL."""
|
|
312
|
+
try:
|
|
313
|
+
if not os.path.exists(_CACHE_FILE):
|
|
314
|
+
return None
|
|
315
|
+
import time as _time
|
|
316
|
+
mtime = os.path.getmtime(_CACHE_FILE)
|
|
317
|
+
if _time.time() - mtime > _CACHE_TTL_SECONDS:
|
|
318
|
+
logger.debug("Cookie cache expired (>%ds)", _CACHE_TTL_SECONDS)
|
|
319
|
+
return None
|
|
320
|
+
with open(_CACHE_FILE, "r", encoding="utf-8") as f:
|
|
321
|
+
data = json.load(f)
|
|
322
|
+
if isinstance(data, dict) and "auth_token" in data and "ct0" in data:
|
|
323
|
+
return data
|
|
324
|
+
except Exception as exc:
|
|
325
|
+
logger.debug("Failed to load cookie cache: %s", exc)
|
|
326
|
+
return None
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def _save_cookie_cache(cookies):
|
|
330
|
+
# type: (Dict[str, str]) -> None
|
|
331
|
+
"""Save cookies to file cache."""
|
|
332
|
+
try:
|
|
333
|
+
os.makedirs(_CACHE_DIR, exist_ok=True)
|
|
334
|
+
with open(_CACHE_FILE, "w", encoding="utf-8") as f:
|
|
335
|
+
json.dump(cookies, f, ensure_ascii=False)
|
|
336
|
+
# Restrict permissions — cookies are sensitive
|
|
337
|
+
os.chmod(_CACHE_FILE, 0o600)
|
|
338
|
+
logger.info("Saved cookies to cache (%s)", _CACHE_FILE)
|
|
339
|
+
except Exception as exc:
|
|
340
|
+
logger.debug("Failed to save cookie cache: %s", exc)
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
def invalidate_cookie_cache():
|
|
344
|
+
# type: () -> None
|
|
345
|
+
"""Delete the cookie cache file."""
|
|
346
|
+
try:
|
|
347
|
+
if os.path.exists(_CACHE_FILE):
|
|
348
|
+
os.remove(_CACHE_FILE)
|
|
349
|
+
logger.info("Cookie cache invalidated")
|
|
350
|
+
except Exception as exc:
|
|
351
|
+
logger.debug("Failed to invalidate cookie cache: %s", exc)
|
|
@@ -28,6 +28,8 @@ import sys
|
|
|
28
28
|
import time
|
|
29
29
|
from pathlib import Path
|
|
30
30
|
|
|
31
|
+
import json
|
|
32
|
+
|
|
31
33
|
import click
|
|
32
34
|
from rich.console import Console
|
|
33
35
|
|
|
@@ -43,7 +45,7 @@ from .formatter import (
|
|
|
43
45
|
print_user_profile,
|
|
44
46
|
print_user_table,
|
|
45
47
|
)
|
|
46
|
-
from .serialization import tweets_from_json, tweets_to_json, users_to_json
|
|
48
|
+
from .serialization import tweets_from_json, tweets_to_json, user_profile_to_dict, users_to_json
|
|
47
49
|
|
|
48
50
|
|
|
49
51
|
console = Console(stderr=True)
|
|
@@ -223,8 +225,9 @@ def favorites(max_count, as_json, output_file, do_filter):
|
|
|
223
225
|
|
|
224
226
|
@cli.command()
|
|
225
227
|
@click.argument("screen_name")
|
|
226
|
-
|
|
227
|
-
|
|
228
|
+
@click.option("--json", "as_json", is_flag=True, help="Output as JSON.")
|
|
229
|
+
def user(screen_name, as_json):
|
|
230
|
+
# type: (str, bool) -> None
|
|
228
231
|
"""View a user's profile. SCREEN_NAME is the @handle (without @)."""
|
|
229
232
|
screen_name = screen_name.lstrip("@")
|
|
230
233
|
config = load_config()
|
|
@@ -236,8 +239,11 @@ def user(screen_name):
|
|
|
236
239
|
console.print("[red]❌ %s[/red]" % exc)
|
|
237
240
|
sys.exit(1)
|
|
238
241
|
|
|
239
|
-
|
|
240
|
-
|
|
242
|
+
if as_json:
|
|
243
|
+
click.echo(json.dumps(user_profile_to_dict(profile), ensure_ascii=False, indent=2))
|
|
244
|
+
else:
|
|
245
|
+
console.print()
|
|
246
|
+
print_user_profile(profile, console)
|
|
241
247
|
|
|
242
248
|
|
|
243
249
|
@cli.command("user-posts")
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|