Update api/validate.py
Browse files- api/validate.py +15 -16
api/validate.py
CHANGED
|
@@ -1,11 +1,10 @@
|
|
| 1 |
import requests
|
| 2 |
import re
|
| 3 |
import time
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
headers = {
|
| 7 |
-
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
| 8 |
-
}
|
| 9 |
|
| 10 |
# Cache variables
|
| 11 |
cached_hid = None
|
|
@@ -16,14 +15,14 @@ def getHid(force_refresh=False):
|
|
| 16 |
global cached_hid, cache_time
|
| 17 |
current_time = time.time()
|
| 18 |
|
| 19 |
-
# Check if we need to refresh
|
| 20 |
if not force_refresh and cached_hid and (current_time - cache_time) < CACHE_DURATION:
|
| 21 |
-
|
| 22 |
return cached_hid
|
| 23 |
|
| 24 |
try:
|
| 25 |
-
# Get
|
| 26 |
-
response = requests.get(
|
| 27 |
response.raise_for_status()
|
| 28 |
content = response.text
|
| 29 |
|
|
@@ -32,31 +31,31 @@ def getHid(force_refresh=False):
|
|
| 32 |
match = re.search(pattern, content)
|
| 33 |
|
| 34 |
if match:
|
| 35 |
-
# Construct
|
| 36 |
js_path = match.group()
|
| 37 |
-
full_url = f"{
|
| 38 |
|
| 39 |
-
#
|
| 40 |
js_response = requests.get(full_url, headers=headers)
|
| 41 |
js_response.raise_for_status()
|
| 42 |
|
| 43 |
-
# Search for h-value in
|
| 44 |
h_pattern = r'h="([0-9a-f-]+)"'
|
| 45 |
h_match = re.search(h_pattern, js_response.text)
|
| 46 |
|
| 47 |
if h_match:
|
| 48 |
h_value = h_match.group(1)
|
| 49 |
-
|
| 50 |
# Update cache
|
| 51 |
cached_hid = h_value
|
| 52 |
cache_time = current_time
|
| 53 |
return h_value
|
| 54 |
else:
|
| 55 |
-
|
| 56 |
return None
|
| 57 |
else:
|
| 58 |
-
|
| 59 |
return None
|
| 60 |
except requests.exceptions.RequestException as e:
|
| 61 |
-
|
| 62 |
return None
|
|
|
|
| 1 |
import requests
|
| 2 |
import re
|
| 3 |
import time
|
| 4 |
+
from api.config import BASE_URL, headers
|
| 5 |
+
from api.logger import setup_logger
|
| 6 |
|
| 7 |
+
logger = setup_logger(__name__)
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Cache variables
|
| 10 |
cached_hid = None
|
|
|
|
| 15 |
global cached_hid, cache_time
|
| 16 |
current_time = time.time()
|
| 17 |
|
| 18 |
+
# Check if we need to refresh or cache is valid
|
| 19 |
if not force_refresh and cached_hid and (current_time - cache_time) < CACHE_DURATION:
|
| 20 |
+
logger.info(f"Using cached_hid: {cached_hid}")
|
| 21 |
return cached_hid
|
| 22 |
|
| 23 |
try:
|
| 24 |
+
# Get initial HTML content
|
| 25 |
+
response = requests.get(BASE_URL, headers=headers)
|
| 26 |
response.raise_for_status()
|
| 27 |
content = response.text
|
| 28 |
|
|
|
|
| 31 |
match = re.search(pattern, content)
|
| 32 |
|
| 33 |
if match:
|
| 34 |
+
# Construct full URL of JS file
|
| 35 |
js_path = match.group()
|
| 36 |
+
full_url = f"{BASE_URL}/_next/{js_path}"
|
| 37 |
|
| 38 |
+
# Get JS file content
|
| 39 |
js_response = requests.get(full_url, headers=headers)
|
| 40 |
js_response.raise_for_status()
|
| 41 |
|
| 42 |
+
# Search for h-value in JS content
|
| 43 |
h_pattern = r'h="([0-9a-f-]+)"'
|
| 44 |
h_match = re.search(h_pattern, js_response.text)
|
| 45 |
|
| 46 |
if h_match:
|
| 47 |
h_value = h_match.group(1)
|
| 48 |
+
logger.info(f"Found h-value: {h_value}")
|
| 49 |
# Update cache
|
| 50 |
cached_hid = h_value
|
| 51 |
cache_time = current_time
|
| 52 |
return h_value
|
| 53 |
else:
|
| 54 |
+
logger.error("h-value not found in JS content")
|
| 55 |
return None
|
| 56 |
else:
|
| 57 |
+
logger.error("Specified JS file path not found in HTML content")
|
| 58 |
return None
|
| 59 |
except requests.exceptions.RequestException as e:
|
| 60 |
+
logger.error(f"An error occurred: {e}")
|
| 61 |
return None
|