|
import requests |
|
import re |
|
import time |
|
|
|
from api.config import BASE_URL, headers |
|
from api.logger import setup_logger |
|
|
|
logger = setup_logger(__name__) |
|
|
|
|
|
cached_hid = None |
|
cache_time = 0 |
|
CACHE_DURATION = 36000 |
|
|
|
def getHid(force_refresh=False): |
|
global cached_hid, cache_time |
|
current_time = time.time() |
|
|
|
|
|
if not force_refresh and cached_hid and (current_time - cache_time) < CACHE_DURATION: |
|
logger.info(f"Using cached_hid: {cached_hid}") |
|
return cached_hid |
|
|
|
try: |
|
|
|
response = requests.get(BASE_URL, headers=headers) |
|
response.raise_for_status() |
|
content = response.text |
|
|
|
|
|
pattern = r"static/chunks/app/layout-[a-zA-Z0-9]+\.js" |
|
match = re.search(pattern, content) |
|
|
|
if match: |
|
|
|
js_path = match.group() |
|
full_url = f"{BASE_URL}/_next/{js_path}" |
|
|
|
|
|
js_response = requests.get(full_url, headers=headers) |
|
js_response.raise_for_status() |
|
|
|
|
|
h_pattern = r'h="([0-9a-f-]+)"' |
|
h_match = re.search(h_pattern, js_response.text) |
|
|
|
if h_match: |
|
h_value = h_match.group(1) |
|
logger.info(f"Found new h-value: {h_value}") |
|
|
|
cached_hid = h_value |
|
cache_time = current_time |
|
return h_value |
|
else: |
|
logger.error("h-value not found in JS content") |
|
return None |
|
else: |
|
logger.error("JS file path not found in HTML content") |
|
return None |
|
except requests.exceptions.RequestException as e: |
|
logger.error(f"Error occurred while fetching hid: {e}") |
|
return None |
|
|