Light-Beacon vor 2 Monaten
Ursprung
Commit
9ee819ebad
2 geänderte Dateien mit 19 neuen und 3 gelöschten Zeilen
  1. 18 2
      Project/modules/mojang_status.py
  2. 1 1
      Project/pages/News.yml

+ 18 - 2
Project/modules/mojang_status.py

@@ -12,6 +12,20 @@ YELLOW_COLOR = "#D1A041"
 RED_COLOR = "#D14241"
 GRAY_COLOR = "#888888"
 
+
+USER_AGENT = (
+    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
+    "AppleWebKit/537.36 (KHTML, like Gecko) "
+    "Chrome/139.0 Safari/537.36"
+)
+
+HEADERS = {
+    "User-Agent": USER_AGENT,
+    "Accept": (
+        "application/json"
+    ),
+}
+
 StatusState: TypeAlias = Literal['up', 'down', 'unknown']
 class StatusDetials(TypedDict):
     session: StatusState = "unknown"
@@ -30,10 +44,10 @@ UPDATE_INTERVAL = 150
 UPDATE_TIME = datetime.min
 STATUS_RECORDS: Queue[StatusRecord | None] = Queue(maxsize=QUEUE_MAX_SIZE)
 CALCULATED_STATUS_CACHE: StatusRecord = None
-
+    
 def get_status():
     try:
-        response = requests.get(API_URL)
+        response = requests.get(API_URL, headers=HEADERS, timeout=10)
         response.raise_for_status()  # Raise an error for bad responses
         return response.json()  # Return the JSON response as a dictionary
     except requests.RequestException as e:
@@ -55,6 +69,8 @@ def update_status():
         status['status']['textures'] = get_service_status(result, 'textures')
         status['status']['website'] = get_service_status(result, 'website')
         status['check_time'] = datetime.strptime(result.get('checkedAt'), "%Y-%m-%dT%H:%M:%S.%fZ") + timedelta(hours=8)
+    else:
+        status['check_time'] = datetime.now()
     if STATUS_RECORDS.full():
         STATUS_RECORDS.get()
     STATUS_RECORDS.put(status)

+ 1 - 1
Project/pages/News.yml

@@ -6,7 +6,7 @@ alias:
 cards:
  - animated_noticebar | s = false
  - ${@IF|${args.debug}|debug}
- - ${@MojangStatusCardReference}
+# - ${@MojangStatusCardReference} 因为上游 API 接口防护弃用
  - ${@NewsList}
  - ${@MainPageVersions|new}
 # - VersionLatestListCard | title=更新日志 | enable-card-style=True