Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions twikit/x_client_transaction/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
from .utils import float_to_hex, is_odd, base64_encode, handle_x_migration

ON_DEMAND_FILE_REGEX = re.compile(
r"""['|\"]{1}ondemand\.s['|\"]{1}:\s*['|\"]{1}([\w]*)['|\"]{1}""", flags=(re.VERBOSE | re.MULTILINE))
INDICES_REGEX = re.compile(
r"""(\(\w{1}\[(\d{1,2})\],\s*16\))+""", flags=(re.VERBOSE | re.MULTILINE))
r""",(\d+):["']ondemand\.s["']""", flags=(re.VERBOSE | re.MULTILINE))
ON_DEMAND_HASH_PATTERN = r',{}:"([0-9a-f]+)"'

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): ON_DEMAND_FILE_REGEX supports both quote types, but the hash pattern assumes double quotes only.

Because ON_DEMAND_HASH_PATTERN is fixed to :"...", it will fail when the source uses single quotes, even though ON_DEMAND_FILE_REGEX would still match. This creates a silent hash_match is None path. Consider updating ON_DEMAND_HASH_PATTERN to accept both quote types (e.g., [:]["']([0-9a-fA-F]+)["']).

Suggested change
ON_DEMAND_HASH_PATTERN = r',{}:"([0-9a-f]+)"'
ON_DEMAND_HASH_PATTERN = r',{}:["\']([0-9a-fA-F]+)["\']'

INDICES_REGEX = re.compile(r"\[(\d+)\],\s*16")


class ClientTransaction:
Expand All @@ -42,14 +42,19 @@ async def get_indices(self, home_page_response, session, headers):
key_byte_indices = []
response = self.validate_response(
home_page_response) or self.home_page_response
on_demand_file = ON_DEMAND_FILE_REGEX.search(str(response))
response_str = str(response)
on_demand_file = ON_DEMAND_FILE_REGEX.search(response_str)
if on_demand_file:
on_demand_file_url = f"https://abs.twimg.com/responsive-web/client-web/ondemand.s.{on_demand_file.group(1)}a.js"
on_demand_file_response = await session.request(method="GET", url=on_demand_file_url, headers=headers)
key_byte_indices_match = INDICES_REGEX.finditer(
str(on_demand_file_response.text))
for item in key_byte_indices_match:
key_byte_indices.append(item.group(2))
on_demand_file_index = on_demand_file.group(1)
hash_regex = re.compile(ON_DEMAND_HASH_PATTERN.format(on_demand_file_index))
hash_match = hash_regex.search(response_str)
if hash_match:
filename = hash_match.group(1)
on_demand_file_url = f"https://abs.twimg.com/responsive-web/client-web/ondemand.s.{filename}a.js"
on_demand_file_response = await session.request(method="GET", url=on_demand_file_url, headers=headers)
key_byte_indices_match = INDICES_REGEX.finditer(str(on_demand_file_response.text))
for item in key_byte_indices_match:
key_byte_indices.append(item.group(1))
if not key_byte_indices:
raise Exception("Couldn't get KEY_BYTE indices")
key_byte_indices = list(map(int, key_byte_indices))
Expand Down