XClientTransaction

repository·master·Indexed 42 days ago

https://github.com/isarabjitdhiman/xclienttransaction

A Python library for generating the 'X-Client-Transaction-Id' (tid) header required for X (formerly Twitter) APIs. It automates the retrieval of session data, including the home page and the 'ondemand.s' file, and provides both synchronous and asynchronous workflows using requests and httpx to compute valid transaction IDs.

Tokens
1.4K
Snippets
4
Records
4
Agent score
5%

What's inside XClientTransaction

  1. Async workflow to get Home Page and ondemand.s response

    master

    To perform an asynchronous scrape/request flow using httpx:

    1. Initialize an httpx.AsyncClient with headers from generate_headers().
    2. Use await handle_x_migration_async(session=session) to get the home page response.
    3. Use get_ondemand_file_url(response=home_page_response) to find the ondemand.s URL.
    4. Await the session.get() for the ondemand.s file.

    Troubleshooting: If you encounter a "Couldn't get KEY_BYTE indices" error, try passing the raw response text (ondemand_file.text) instead of the BeautifulSoup object to ClientTransaction.

    import bs4
    import httpx
    from x_client_transaction.utils import generate_headers, handle_x_migration_async, get_ondemand_file_url
    
    # 1. Initialize Async Session
    session = httpx.AsyncClient(headers=generate_headers())
    
    # 2. Get Home Page
    home_page_response = await handle_x_migration_async(session=session)
    
    # 3. Get ondemand.s
    ondemand_file_url = get_ondemand_file_url(response=home_page_response)
    ondemand_file = await session.get(url=ondemand_file_url)
    
    # 4. Parse
    ondemand_file_response = bs4.BeautifulSoup(ondemand_file.content, 'html.parser')
    # Alternative if error occurs:
    # ondemand_file_response = ondemand_file.text
  2. Synchronous workflow to get Home Page and ondemand.s response

    master

    To perform a synchronous scrape/request flow:

    1. Initialize a requests.Session and set headers using generate_headers().
    2. Use handle_x_migration(session=session) to get the home page response (required if hitting twitter.com instead of x.com). If using x.com directly, simply perform a session.get() and parse with BeautifulSoup.
    3. Use get_ondemand_file_url(response=home_page_response) to find the ondemand.s URL.
    4. Fetch the ondemand.s file and parse it.

    Troubleshooting: If you encounter a "Couldn't get KEY_BYTE indices" error, try passing the raw response text (ondemand_file.text) instead of the BeautifulSoup object to ClientTransaction.

    import bs4
    import requests
    from x_client_transaction.utils import generate_headers, handle_x_migration, get_ondemand_file_url
    
    # 1. Initialize Session
    session = requests.Session()
    session.headers = generate_headers()
    
    # 2. Get Home Page
    home_page_response = handle_x_migration(session=session)
    
    # 3. Get ondemand.s
    ondemand_file_url = get_ondemand_file_url(response=home_page_response)
    ondemand_file = session.get(url=ondemand_file_url)
    
    # 4. Parse (use .text if BeautifulSoup fails)
    ondemand_file_response = bs4.BeautifulSoup(ondemand_file.content, 'html.parser')
    # Alternative if error occurs:
    # ondemand_file_response = ondemand_file.text
  3. Generate X-Client-Transaction-Id (tid)

    master

    Use the ClientTransaction class to generate a transaction ID (tid) for specific X (formerly Twitter) API endpoints. You must provide the home_page_response and the ondemand_file_response (typically BeautifulSoup objects or raw text) obtained during the session initialization.

    To generate the ID, call generate_transaction_id(method, path) where method is the HTTP verb (e.g., POST, GET) and path is the URL path extracted from the target endpoint.

    from urllib.parse import urlparse
    from x_client_transaction import ClientTransaction
    
    # Setup requirements
    url = "https://x.com/i/api/1.1/jot/client_event.json"
    method = "POST"
    path = urlparse(url=url).path
    
    # Initialize with responses obtained from the session
    ct = ClientTransaction(home_page_response=home_page_response, ondemand_file_response=ondemand_file_response)
    
    # Generate the ID
    transaction_id = ct.generate_transaction_id(method=method, path=path)
    print(transaction_id)