Skip to content

fix: extract LIVESTREET_SECURITY_KEY from full HTML page instead of PJAX fragment#4

Open
ofkindness wants to merge 1 commit into
nyakokitsu:mainfrom
ofkindness:fix/security-key-extraction-from-full-page
Open

fix: extract LIVESTREET_SECURITY_KEY from full HTML page instead of PJAX fragment#4
ofkindness wants to merge 1 commit into
nyakokitsu:mainfrom
ofkindness:fix/security-key-extraction-from-full-page

Conversation

@ofkindness

Copy link
Copy Markdown

The security key is only present in the full HTML page source, not in PJAX responses which return only content fragments. This commit fixes the extraction logic to parse the key from the initial full-page request.

  • Read and store initial response body instead of discarding it
  • Move security key extraction to the initial page request
  • Remove failed key extraction attempt from PJAX response
  • Add debug logging for key extraction process

Before This Change

The scraper would attempt to extract the LIVESTREET_SECURITY_KEY from
the PJAX response, which only contains a fragment of the page (the main
content area). This caused the key extraction to fail:

🔹 Sending initial request to https://akniga.org/
Initial request successful. Cookies (if any) stored in jar.
🔹 Sending PJAX request to https://akniga.org/[book-url]
[DEBUG] Content length: 118225 bytes
[DEBUG] ✗ 'LIVESTREET_SECURITY_KEY' NOT found anywhere in content
Failed to parse security key: LIVESTREET_SECURITY_KEY string not found in content

The Issue

  • PJAX responses (with X-PJAX: true header) return only the content
    fragment.
  • The LIVESTREET_SECURITY_KEY is embedded in the <head> or
    <script> tags.
  • These script tags are stripped from PJAX responses, making the key
    inaccessible.

The Solution

  • Read the initial response body instead of discarding it.
  • Extract the security key from the full HTML page
    (https://akniga.org/).
  • Reuse the extracted key for all subsequent requests.

After This Change

The scraper now successfully extracts the security key from the initial
full-page request:

🔹 Sending initial request to https://akniga.org/
Initial request successful. Downloaded 185861 bytes
[DEBUG] ✓ Found 'LIVESTREET_SECURITY_KEY' string in content
[DEBUG] ✓ Successfully extracted key: [SECURITY_KEY]
🔑 Found LIVESTREET_SECURITY_KEY: [SECURITY_KEY]
🔹 Sending PJAX request to https://akniga.org/[book-url]
🆔 Found bid: [BOOK_ID]
✅ Got AJAX Response: 200 OK
📚 Got book data!

Testing Performed

  • Verified extraction works with multiple book URLs.
  • Confirmed the security key is now correctly passed to the AJAX POST
    request.
  • Validated that the audio download process completes successfully.

Additional Improvements

  • Added debug logging to track key extraction process.
  • Improved error messages for better troubleshooting.
  • The initial request now properly stores the full page content for
    parsing.

Breaking Changes

None. This change is backward compatible with existing
functionality.

…JAX fragment

The security key is only present in the full HTML page source, not in PJAX
responses which return only content fragments. This commit fixes the
extraction logic to parse the key from the initial full-page request.

- Read and store initial response body instead of discarding it
- Move security key extraction to the initial page request
- Remove failed key extraction attempt from PJAX response
- Add debug logging for key extraction process
@nyakokitsu

Copy link
Copy Markdown
Owner

Спасибо большое! Проверю и смержу если не будет проблем

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants