File size: 2,037 Bytes
0e86a5a
 
 
 
b48a03d
 
ce5a8a3
 
 
 
 
b48a03d
0e86a5a
d372e35
ce5a8a3
 
d372e35
 
ce5a8a3
 
 
d372e35
 
ce5a8a3
 
 
 
d372e35
0e86a5a
b48a03d
 
0e86a5a
 
 
ce5a8a3
0e86a5a
 
 
 
ce5a8a3
0e86a5a
e43189b
0e86a5a
ce5a8a3
b48a03d
0e86a5a
 
ce5a8a3
0e86a5a
 
 
 
 
b48a03d
ce5a8a3
 
 
0e86a5a
 
b48a03d
0e86a5a
 
ce5a8a3
0e86a5a
 
b48a03d
0e86a5a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import requests
import re
import time

BASE_URL = "https://www.blackbox.ai"
HEADERS = {
    "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"
    )
}

# Cache variables
_cached_hid = None
_cache_time = 0
CACHE_DURATION = 36000  # Cache duration in seconds (10 hours)


def get_hid(force_refresh=False):
    global _cached_hid, _cache_time
    current_time = time.time()

    # Use cached value if not expired
    if not force_refresh and _cached_hid and (current_time - _cache_time) < CACHE_DURATION:
        print("Using cached hid:", _cached_hid)
        return _cached_hid

    try:
        # Get initial HTML content
        response = requests.get(BASE_URL, headers=HEADERS)
        response.raise_for_status()
        content = response.text

        # Find the specific JS file path
        pattern = r"static/chunks/app/layout-[a-zA-Z0-9]+\.js"
        match = re.search(pattern, content)

        if match:
            # Construct full JS file URL
            js_path = match.group()
            full_url = f"{BASE_URL}/_next/{js_path}"

            # Request JS file content
            js_response = requests.get(full_url, headers=HEADERS)
            js_response.raise_for_status()

            # Search for h-value in JS content
            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)
                print("Found h-value:", h_value)
                # Update cache
                _cached_hid = h_value
                _cache_time = current_time
                return h_value
            else:
                print("h-value not found in JS content")
                return None
        else:
            print("JS file path not found in HTML content")
            return None
    except requests.exceptions.RequestException as e:
        print(f"An error occurred: {e}")
        return None