Update api/validate.py
Browse files- api/validate.py +11 -11
api/validate.py
CHANGED
@@ -16,47 +16,47 @@ def getHid(force_refresh=False):
|
|
16 |
global cached_hid, cache_time
|
17 |
current_time = time.time()
|
18 |
|
19 |
-
#
|
20 |
if not force_refresh and cached_hid and (current_time - cache_time) < CACHE_DURATION:
|
21 |
print("using cached_hid:", cached_hid)
|
22 |
return cached_hid
|
23 |
|
24 |
try:
|
25 |
-
#
|
26 |
response = requests.get(base_url, headers=headers)
|
27 |
response.raise_for_status()
|
28 |
content = response.text
|
29 |
|
30 |
-
#
|
31 |
pattern = r"static/chunks/app/layout-[a-zA-Z0-9]+\.js"
|
32 |
match = re.search(pattern, content)
|
33 |
|
34 |
if match:
|
35 |
-
#
|
36 |
js_path = match.group()
|
37 |
full_url = f"{base_url}/_next/{js_path}"
|
38 |
|
39 |
-
#
|
40 |
js_response = requests.get(full_url, headers=headers)
|
41 |
js_response.raise_for_status()
|
42 |
|
43 |
-
#
|
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 |
-
print("
|
50 |
-
#
|
51 |
cached_hid = h_value
|
52 |
cache_time = current_time
|
53 |
return h_value
|
54 |
else:
|
55 |
-
print("
|
56 |
return None
|
57 |
else:
|
58 |
-
print("
|
59 |
return None
|
60 |
except requests.exceptions.RequestException as e:
|
61 |
-
print(f"
|
62 |
return None
|
|
|
16 |
global cached_hid, cache_time
|
17 |
current_time = time.time()
|
18 |
|
19 |
+
# Check if a forced refresh is needed or if the cached values are still valid.
|
20 |
if not force_refresh and cached_hid and (current_time - cache_time) < CACHE_DURATION:
|
21 |
print("using cached_hid:", cached_hid)
|
22 |
return cached_hid
|
23 |
|
24 |
try:
|
25 |
+
# Retrieve the initial HTML content.
|
26 |
response = requests.get(base_url, headers=headers)
|
27 |
response.raise_for_status()
|
28 |
content = response.text
|
29 |
|
30 |
+
# Use a regular expression to find specific `static/chunks` paths.
|
31 |
pattern = r"static/chunks/app/layout-[a-zA-Z0-9]+\.js"
|
32 |
match = re.search(pattern, content)
|
33 |
|
34 |
if match:
|
35 |
+
# Construct the full URL of the JS file.
|
36 |
js_path = match.group()
|
37 |
full_url = f"{base_url}/_next/{js_path}"
|
38 |
|
39 |
+
# Request the content of the JS file.
|
40 |
js_response = requests.get(full_url, headers=headers)
|
41 |
js_response.raise_for_status()
|
42 |
|
43 |
+
# In JS content, use a regular expression to search for h-value.
|
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 |
+
print("Found the h-value.", h_value)
|
50 |
+
# Update the cache.
|
51 |
cached_hid = h_value
|
52 |
cache_time = current_time
|
53 |
return h_value
|
54 |
else:
|
55 |
+
print("The h-value was not found in the JS content.")
|
56 |
return None
|
57 |
else:
|
58 |
+
print("The specified JS file path was not found in the HTML content.")
|
59 |
return None
|
60 |
except requests.exceptions.RequestException as e:
|
61 |
+
print(f"An error occurred. {e}")
|
62 |
return None
|