Skip to content

Commit b868fee

Browse files
author
yassine
committed
fix: replace broken ondemand.s URL extraction for transaction-id bootstrap
1 parent 31a9601 commit b868fee

1 file changed

Lines changed: 30 additions & 4 deletions

File tree

Scweet/transaction.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,33 @@
1616
from .utils import as_str as _as_str
1717

1818

19+
def _extract_ondemand_url(html: str) -> str | None:
20+
marker = '"ondemand.s"'
21+
pos = html.find(marker)
22+
if pos == -1:
23+
return None
24+
comma = html.rfind(",", 0, pos)
25+
if comma == -1:
26+
return None
27+
colon = html.find(":", comma)
28+
if colon == -1:
29+
return None
30+
chunk_key = html[comma + 1 : colon].strip().strip('"').strip("'")
31+
search_start = pos + len(marker)
32+
needle = chunk_key + ':"'
33+
hash_start = html.find(needle, search_start)
34+
if hash_start == -1:
35+
return None
36+
hash_start += len(needle)
37+
hash_end = html.find('"', hash_start)
38+
if hash_end == -1:
39+
return None
40+
ondemand_hash = html[hash_start:hash_end]
41+
if not ondemand_hash or not all(c in "0123456789abcdef" for c in ondemand_hash):
42+
return None
43+
return f"https://abs.twimg.com/responsive-web/client-web/ondemand.s.{ondemand_hash}a.js"
44+
45+
1946
class TransactionIdProvider:
2047
def __init__(
2148
self,
@@ -76,7 +103,7 @@ def _ensure_dependencies(self) -> bool:
76103
self._deps_checked = True
77104
try:
78105
from x_client_transaction import ClientTransaction # noqa: F401
79-
from x_client_transaction.utils import get_ondemand_file_url, handle_x_migration # noqa: F401
106+
from x_client_transaction.utils import handle_x_migration # noqa: F401
80107

81108
self._deps_available = True
82109
except Exception:
@@ -88,7 +115,7 @@ def _build_client_transaction(self):
88115
if not self._ensure_dependencies():
89116
return None
90117
from x_client_transaction import ClientTransaction
91-
from x_client_transaction.utils import get_ondemand_file_url, handle_x_migration
118+
from x_client_transaction.utils import handle_x_migration
92119

93120
session = None
94121
try:
@@ -112,10 +139,9 @@ def _build_client_transaction(self):
112139
if session_headers is not None and hasattr(session_headers, "update"):
113140
session_headers.update(headers)
114141

115-
# Keep actor parity: run migration handling before extracting ondemand.js URL.
116142
home_page = handle_x_migration(session=session)
117143

118-
ondemand_url = get_ondemand_file_url(response=home_page)
144+
ondemand_url = _extract_ondemand_url(str(home_page))
119145
if not ondemand_url:
120146
logger.warning("Transaction-id bootstrap failed: ondemand URL not found")
121147
return None

0 commit comments

Comments
 (0)