feat: Implement session management with retries for HTTP requests in stats processing

This commit is contained in:
zhangyuheng
2026-03-09 17:03:36 +08:00
parent 7294088ed8
commit 4724ddbd8c

View File

@@ -1,6 +1,8 @@
import os import os
import json import json
import requests import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
import re import re
import time import time
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
@@ -10,12 +12,23 @@ from tqdm import tqdm # Add tqdm for progress bars
BASE_URL = "http://x2.sjcmc.cn:15960/stats/" BASE_URL = "http://x2.sjcmc.cn:15960/stats/"
STATS_DIR = "stats" STATS_DIR = "stats"
# Create a session that bypasses system proxy and retries on failure
session = requests.Session()
session.trust_env = False # Ignore HTTP_PROXY / HTTPS_PROXY env vars
retry_strategy = Retry(
total=3,
backoff_factor=1,
status_forcelist=[429, 500, 502, 503, 504],
)
session.mount("http://", HTTPAdapter(max_retries=retry_strategy))
session.mount("https://", HTTPAdapter(max_retries=retry_strategy))
# Ensure directories exist # Ensure directories exist
os.makedirs(STATS_DIR, exist_ok=True) os.makedirs(STATS_DIR, exist_ok=True)
print("Fetching file list...") print("Fetching file list...")
try: try:
response = requests.get(BASE_URL, timeout=10) response = session.get(BASE_URL, timeout=10)
response.raise_for_status() response.raise_for_status()
content = response.text content = response.text
# Regex for UUID.json # Regex for UUID.json
@@ -29,7 +42,7 @@ except Exception as e:
def get_player_name(uuid): def get_player_name(uuid):
# Try Ashcon first # Try Ashcon first
try: try:
r = requests.get(f"https://api.ashcon.app/mojang/v2/user/{uuid}", timeout=5) r = session.get(f"https://api.ashcon.app/mojang/v2/user/{uuid}", timeout=5)
if r.status_code == 200: if r.status_code == 200:
return r.json().get('username') return r.json().get('username')
except: except:
@@ -37,7 +50,7 @@ def get_player_name(uuid):
# Try Mojang Session # Try Mojang Session
try: try:
r = requests.get(f"https://sessionserver.mojang.com/session/minecraft/profile/{uuid}", timeout=5) r = session.get(f"https://sessionserver.mojang.com/session/minecraft/profile/{uuid}", timeout=5)
if r.status_code == 200: if r.status_code == 200:
return r.json().get('name') return r.json().get('name')
except: except:
@@ -54,7 +67,7 @@ def process_player(filename):
try: try:
# Check if we already have it locally and it's valid, maybe skip download? # Check if we already have it locally and it's valid, maybe skip download?
# User implies fetching updates, so we download. # User implies fetching updates, so we download.
r = requests.get(BASE_URL + filename, timeout=10) r = session.get(BASE_URL + filename, timeout=10)
if r.status_code == 200: if r.status_code == 200:
data = r.json() data = r.json()
else: else: