twitter-cli 0.3.4__tar.gz → 0.4.0__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.
Files changed (27) hide show
  1. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/PKG-INFO +1 -1
  2. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/pyproject.toml +1 -1
  3. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/twitter_cli/auth.py +90 -11
  4. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/twitter_cli/client.py +28 -1
  5. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/uv.lock +1 -1
  6. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/.github/workflows/ci.yml +0 -0
  7. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/.github/workflows/publish-clawhub.yml +0 -0
  8. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/.github/workflows/publish.yml +0 -0
  9. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/.gitignore +0 -0
  10. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/LICENSE +0 -0
  11. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/README.md +0 -0
  12. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/SKILL.md +0 -0
  13. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/config.yaml +0 -0
  14. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/tests/conftest.py +0 -0
  15. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/tests/test_cli.py +0 -0
  16. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/tests/test_config.py +0 -0
  17. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/tests/test_config_normalization.py +0 -0
  18. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/tests/test_filter.py +0 -0
  19. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/tests/test_serialization.py +0 -0
  20. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/twitter_cli/__init__.py +0 -0
  21. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/twitter_cli/cli.py +0 -0
  22. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/twitter_cli/config.py +0 -0
  23. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/twitter_cli/constants.py +0 -0
  24. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/twitter_cli/filter.py +0 -0
  25. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/twitter_cli/formatter.py +0 -0
  26. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/twitter_cli/models.py +0 -0
  27. {twitter_cli-0.3.4 → twitter_cli-0.4.0}/twitter_cli/serialization.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: twitter-cli
3
- Version: 0.3.4
3
+ Version: 0.4.0
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
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "twitter-cli"
7
- version = "0.3.4"
7
+ version = "0.4.0"
8
8
  description = "A CLI for Twitter/X — feed, bookmarks, and user timeline in terminal"
9
9
  readme = "README.md"
10
10
  license = "Apache-2.0"
@@ -2,8 +2,10 @@
2
2
 
3
3
  Supports:
4
4
  1. Environment variables: TWITTER_AUTH_TOKEN + TWITTER_CT0
5
- 2. Auto-extract from browser via browser-cookie3 (subprocess)
5
+ 2. Auto-extract from browser via browser-cookie3
6
6
  Extracts ALL Twitter cookies for full browser-like fingerprint.
7
+ Prefers in-process extraction (required on macOS for Keychain access),
8
+ falls back to subprocess if in-process fails (e.g. SQLite lock).
7
9
  """
8
10
 
9
11
  from __future__ import annotations
@@ -21,6 +23,14 @@ from .constants import BEARER_TOKEN, USER_AGENT
21
23
 
22
24
  logger = logging.getLogger(__name__)
23
25
 
26
+ # Domains to match for Twitter cookies
27
+ _TWITTER_DOMAINS = {"x.com", "twitter.com", ".x.com", ".twitter.com"}
28
+
29
+
30
+ def _is_twitter_domain(domain):
31
+ # type: (str) -> bool
32
+ return domain in _TWITTER_DOMAINS or domain.endswith(".x.com") or domain.endswith(".twitter.com")
33
+
24
34
 
25
35
  def load_from_env() -> Optional[Dict[str, str]]:
26
36
  """Load cookies from environment variables."""
@@ -39,6 +49,8 @@ def verify_cookies(auth_token, ct0, cookie_string=None):
39
49
  Tries multiple endpoints. Only raises on clear auth failures (401/403).
40
50
  For other errors (404, network), returns empty dict (proceed without verification).
41
51
  """
52
+ from .client import _best_chrome_target
53
+
42
54
  urls = [
43
55
  "https://api.x.com/1.1/account/verify_credentials.json",
44
56
  "https://x.com/i/api/1.1/account/settings.json",
@@ -58,7 +70,7 @@ def verify_cookies(auth_token, ct0, cookie_string=None):
58
70
 
59
71
  proxy = os.environ.get("TWITTER_PROXY", "")
60
72
  session = _cffi_requests.Session(
61
- impersonate="chrome133",
73
+ impersonate=_best_chrome_target(),
62
74
  proxies={"https": proxy, "http": proxy} if proxy else None,
63
75
  )
64
76
 
@@ -85,16 +97,66 @@ def verify_cookies(auth_token, ct0, cookie_string=None):
85
97
  return {}
86
98
 
87
99
 
88
- def extract_from_browser() -> Optional[Dict[str, str]]:
89
- """Auto-extract ALL Twitter cookies from local browser using browser-cookie3.
100
+ def _extract_cookies_from_jar(jar):
101
+ # type: (Any) -> Optional[Dict[str, str]]
102
+ """Extract Twitter cookies from a cookie jar."""
103
+ result = {} # type: Dict[str, str]
104
+ all_cookies = {} # type: Dict[str, str]
105
+ for cookie in jar:
106
+ domain = cookie.domain or ""
107
+ if _is_twitter_domain(domain):
108
+ if cookie.name == "auth_token":
109
+ result["auth_token"] = cookie.value
110
+ elif cookie.name == "ct0":
111
+ result["ct0"] = cookie.value
112
+ if cookie.name and cookie.value:
113
+ all_cookies[cookie.name] = cookie.value
114
+ if "auth_token" in result and "ct0" in result:
115
+ cookies = {"auth_token": result["auth_token"], "ct0": result["ct0"]}
116
+ if all_cookies:
117
+ cookies["cookie_string"] = "; ".join("%s=%s" % (k, v) for k, v in all_cookies.items())
118
+ logger.info("Extracted %d total cookies for full browser fingerprint", len(all_cookies))
119
+ return cookies
120
+ return None
90
121
 
91
- Extracts every cookie for .x.com and .twitter.com domains, not just
92
- auth_token and ct0. This makes requests indistinguishable from real
93
- browser traffic at the cookie level.
94
122
 
95
- Tries browsers in order: Chrome -> Edge -> Firefox -> Brave.
96
- Runs in a subprocess to avoid SQLite database lock issues.
123
+ def _extract_in_process():
124
+ # type: () -> Optional[Dict[str, str]]
125
+ """Extract cookies in the main process (required on macOS for Keychain access).
126
+
127
+ On macOS, Chrome encrypts cookies using a key stored in the system Keychain.
128
+ Child processes do NOT inherit the parent's Keychain authorization, so
129
+ browser_cookie3 must run in the main process to decrypt cookies.
97
130
  """
131
+ try:
132
+ import browser_cookie3
133
+ except ImportError:
134
+ logger.debug("browser_cookie3 not installed, skipping in-process extraction")
135
+ return None
136
+
137
+ browsers = [
138
+ ("chrome", browser_cookie3.chrome),
139
+ ("edge", browser_cookie3.edge),
140
+ ("firefox", browser_cookie3.firefox),
141
+ ("brave", browser_cookie3.brave),
142
+ ]
143
+
144
+ for name, fn in browsers:
145
+ try:
146
+ jar = fn()
147
+ except Exception as e:
148
+ logger.debug("%s in-process extraction failed: %s", name, e)
149
+ continue
150
+ cookies = _extract_cookies_from_jar(jar)
151
+ if cookies:
152
+ logger.info("Found cookies in %s (in-process)", name)
153
+ return cookies
154
+ return None
155
+
156
+
157
+ def _extract_via_subprocess():
158
+ # type: () -> Optional[Dict[str, str]]
159
+ """Extract cookies via subprocess (fallback if in-process fails, e.g. SQLite lock)."""
98
160
  extract_script = '''
99
161
  import json, sys
100
162
  try:
@@ -124,7 +186,6 @@ for name, fn in browsers:
124
186
  result["auth_token"] = cookie.value
125
187
  elif cookie.name == "ct0":
126
188
  result["ct0"] = cookie.value
127
- # Collect ALL cookies for full browser fingerprint
128
189
  if cookie.name and cookie.value:
129
190
  all_cookies[cookie.name] = cookie.value
130
191
  if "auth_token" in result and "ct0" in result:
@@ -164,7 +225,7 @@ sys.exit(1)
164
225
  data = json.loads(output)
165
226
  if "error" in data:
166
227
  return None
167
- logger.info("Found cookies in %s", data.get("browser", "unknown"))
228
+ logger.info("Found cookies in %s (subprocess)", data.get("browser", "unknown"))
168
229
 
169
230
  # Build full cookie string from all extracted cookies
170
231
  cookies = {"auth_token": data["auth_token"], "ct0": data["ct0"]}
@@ -178,6 +239,23 @@ sys.exit(1)
178
239
  return None
179
240
 
180
241
 
242
+ def extract_from_browser() -> Optional[Dict[str, str]]:
243
+ """Auto-extract ALL Twitter cookies from local browser using browser-cookie3.
244
+
245
+ Strategy:
246
+ 1. Try in-process first (required on macOS for Keychain access)
247
+ 2. Fall back to subprocess (handles SQLite lock when browser is running)
248
+ """
249
+ # 1. In-process (works on macOS, may fail with SQLite lock)
250
+ cookies = _extract_in_process()
251
+ if cookies:
252
+ return cookies
253
+
254
+ # 2. Subprocess fallback (handles SQLite lock, but fails on macOS Keychain)
255
+ logger.debug("In-process extraction failed, trying subprocess fallback")
256
+ return _extract_via_subprocess()
257
+
258
+
181
259
  def get_cookies() -> Dict[str, str]:
182
260
  """Get Twitter cookies. Priority: env vars -> browser extraction (Chrome/Edge/Firefox/Brave).
183
261
 
@@ -205,3 +283,4 @@ def get_cookies() -> Dict[str, str]:
205
283
  verify_cookies(cookies["auth_token"], cookies["ct0"], cookies.get("cookie_string"))
206
284
  return cookies
207
285
 
286
+
@@ -106,6 +106,31 @@ class TwitterAPIError(RuntimeError):
106
106
  super().__init__(message)
107
107
  self.status_code = status_code
108
108
 
109
+ def _best_chrome_target():
110
+ # type: () -> str
111
+ """Detect the best available Chrome impersonation target at runtime.
112
+
113
+ curl_cffi versions differ in which Chrome targets they ship.
114
+ e.g. 0.14.0 has chrome133a but not chrome133.
115
+ """
116
+ try:
117
+ from curl_cffi.requests import BrowserType
118
+ available = {e.value for e in BrowserType}
119
+ except Exception:
120
+ available = set()
121
+
122
+ # Preference order: exact chrome versions, then suffixed variants
123
+ for target in ("chrome133", "chrome133a", "chrome136", "chrome131", "chrome130"):
124
+ if target in available:
125
+ return target
126
+ # Fallback: pick highest chrome* with a pure numeric suffix
127
+ chrome_targets = sorted(
128
+ [v for v in available if v.startswith("chrome") and v.replace("chrome", "").isdigit()],
129
+ key=lambda x: int(x.replace("chrome", "")),
130
+ reverse=True,
131
+ )
132
+ return chrome_targets[0] if chrome_targets else "chrome131"
133
+
109
134
 
110
135
  def _get_cffi_session():
111
136
  # type: () -> Any
@@ -114,10 +139,12 @@ def _get_cffi_session():
114
139
  if _cffi_session is None:
115
140
  import os
116
141
  proxy = os.environ.get("TWITTER_PROXY", "")
142
+ target = _best_chrome_target()
117
143
  _cffi_session = _cffi_requests.Session(
118
- impersonate="chrome133",
144
+ impersonate=target,
119
145
  proxies={"https": proxy, "http": proxy} if proxy else None,
120
146
  )
147
+ logger.info("curl_cffi impersonating %s", target)
121
148
  if proxy:
122
149
  logger.info("Using proxy: %s", proxy[:20] + "...")
123
150
  return _cffi_session
@@ -950,7 +950,7 @@ wheels = [
950
950
 
951
951
  [[package]]
952
952
  name = "twitter-cli"
953
- version = "0.3.3"
953
+ version = "0.3.4"
954
954
  source = { editable = "." }
955
955
  dependencies = [
956
956
  { name = "beautifulsoup4" },
File without changes
File without changes
File without changes
File without changes
File without changes