Niansuh commited on
Commit
7737401
·
verified ·
1 Parent(s): 6aa04bb

Update api/validate.py

Browse files
Files changed (1) hide show
  1. api/validate.py +12 -14
api/validate.py CHANGED
@@ -1,10 +1,8 @@
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,13 +13,13 @@ def getHid(force_refresh=False):
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
@@ -31,31 +29,31 @@ def getHid(force_refresh=False):
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 even when force_refresh is True
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 while fetching h-value: {e}")
61
  return None
 
1
  import requests
2
  import re
3
  import time
 
 
4
 
5
+ from api.config import BASE_URL, headers
6
 
7
  # Cache variables
8
  cached_hid = None
 
13
  global cached_hid, cache_time
14
  current_time = time.time()
15
 
16
+ # Check if cache is valid and not forced to refresh
17
  if not force_refresh and cached_hid and (current_time - cache_time) < CACHE_DURATION:
18
+ print("using cached_hid:", cached_hid)
19
  return cached_hid
20
 
21
  try:
22
+ # Fetch initial HTML content
23
  response = requests.get(BASE_URL, headers=headers)
24
  response.raise_for_status()
25
  content = response.text
 
29
  match = re.search(pattern, content)
30
 
31
  if match:
32
+ # Construct the full URL of the JS file
33
  js_path = match.group()
34
  full_url = f"{BASE_URL}/_next/{js_path}"
35
 
36
+ # Request JS file content
37
  js_response = requests.get(full_url, headers=headers)
38
  js_response.raise_for_status()
39
 
40
+ # Search for h-value in the JS content using regex
41
  h_pattern = r'h="([0-9a-f-]+)"'
42
  h_match = re.search(h_pattern, js_response.text)
43
 
44
  if h_match:
45
  h_value = h_match.group(1)
46
+ print("Found h-value:", h_value)
47
+ # Update cache
48
  cached_hid = h_value
49
  cache_time = current_time
50
  return h_value
51
  else:
52
+ print("h-value not found in JS content")
53
  return None
54
  else:
55
+ print("JS file path not found in HTML content")
56
  return None
57
  except requests.exceptions.RequestException as e:
58
+ print(f"Error occurred: {e}")
59
  return None