Enhance scrape_target method to handle HTTP redirects

- Implemented logic to follow HTTP redirects up to a specified maximum depth (max_redirects) in the scrape_target method.
- Added logging to track each redirect step, providing better visibility into the redirect chain.
- Set a limit on the number of redirects to prevent infinite loops in case of circular redirects.
- This change ensures that the UUID is checked on the final destination page, improving the accuracy of the scraping process.

	modified:   remarkbox/models/namespace_request.py
This commit is contained in:
Russell Ballestrini 2024-10-06 16:54:10 -04:00
parent 83adeb0a02
commit c5453d632d

View file

@ -12,6 +12,7 @@ from miniuri import Uri
log = logging.getLogger(__name__)
# Dictionary to track when scraping is allowed again for specific domains
domain_skip_until = {}
@ -62,7 +63,7 @@ class NamespaceRequest(RBase, Base):
else:
self.unverify()
def scrape_target(self):
def scrape_target(self, max_redirects=5):
"""Scrape the given target for NamespaceRequest id
Return True if found else False.
"""
@ -106,7 +107,19 @@ class NamespaceRequest(RBase, Base):
)
)
try:
resp = requests.get(self.target, timeout=8.50)
# Follow redirects up to max_redirects
resp = requests.get(
self.target, headers=headers, timeout=8.50, allow_redirects=True
)
redirect_count = 0
while resp.is_redirect and redirect_count < max_redirects:
redirect_count += 1
next_url = resp.headers.get("Location")
if not next_url:
break
log.info(f"Redirecting to {next_url}")
resp = requests.get(next_url, headers=headers, timeout=8.50)
if resp.ok:
if namespace_request_id in resp.text:
log.info(