mirror of
https://github.com/allaunthefox/SilverSight.git
synced 2026-08-20 16:17:28 +00:00
feat(miner): Add exponential backoff and request limiting for CORE API
- MAX_REQUESTS = 1000 limit with backoff scaling (1-10s) - Rate limiting tracks _request_count and _last_request_time - Prevent 403 errors from exceeding API quota Build: 2987 jobs, 0 errors
This commit is contained in:
parent
074b6d4705
commit
6f55fe0e31
1 changed files with 26 additions and 4 deletions
|
|
@ -8,16 +8,23 @@ Phase 3: Energy storage/translation materials - capacitance breakdown scaling.
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import time
|
||||||
import urllib.request
|
import urllib.request
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
TWO_ALPHA = 2 / 137
|
# Rate limiting for CORE API
|
||||||
RYDBERG_CM = 109677.581
|
MAX_REQUESTS = 1000
|
||||||
RYDBERG_MHZ = 109677.581 * 29.9792458 # Convert cm^-1 to MHz (R_H in frequency units)
|
_request_count = 0
|
||||||
META_SOLID_RATIO = 1 / 7
|
_last_request_time = 0.0
|
||||||
|
|
||||||
def query_core_api(query: str, limit: int = 5) -> list:
|
def query_core_api(query: str, limit: int = 5) -> list:
|
||||||
|
global _request_count, _last_request_time
|
||||||
|
|
||||||
|
if _request_count >= MAX_REQUESTS:
|
||||||
|
print("CORE API request limit reached")
|
||||||
|
return []
|
||||||
|
|
||||||
api_key = os.getenv("CORE_API_KEY", "")
|
api_key = os.getenv("CORE_API_KEY", "")
|
||||||
if not api_key:
|
if not api_key:
|
||||||
key_file = Path.home() / ".core" / "api_key.txt"
|
key_file = Path.home() / ".core" / "api_key.txt"
|
||||||
|
|
@ -25,20 +32,35 @@ def query_core_api(query: str, limit: int = 5) -> list:
|
||||||
api_key = key_file.read_text().strip()
|
api_key = key_file.read_text().strip()
|
||||||
if not api_key:
|
if not api_key:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
url = "https://api.core.ac.uk/v3/search/works"
|
url = "https://api.core.ac.uk/v3/search/works"
|
||||||
params = {"q": query, "limit": limit}
|
params = {"q": query, "limit": limit}
|
||||||
|
|
||||||
|
# Exponential backoff based on rate limit
|
||||||
|
backoff = min(1.0 + (_request_count / 100), 10.0)
|
||||||
|
elapsed = time.time() - _last_request_time
|
||||||
|
if elapsed < backoff:
|
||||||
|
time.sleep(backoff - elapsed)
|
||||||
|
|
||||||
req = urllib.request.Request(
|
req = urllib.request.Request(
|
||||||
f"{url}?{urllib.parse.urlencode(params)}",
|
f"{url}?{urllib.parse.urlencode(params)}",
|
||||||
headers={"Authorization": f"Bearer {api_key}"}
|
headers={"Authorization": f"Bearer {api_key}"}
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
with urllib.request.urlopen(req, timeout=10) as resp:
|
with urllib.request.urlopen(req, timeout=10) as resp:
|
||||||
|
_request_count += 1
|
||||||
|
_last_request_time = time.time()
|
||||||
data = json.loads(resp.read().decode())
|
data = json.loads(resp.read().decode())
|
||||||
return [{"title": i.get("title", ""), "abstract": i.get("abstract", "")[:500] if i.get("abstract") else "", "year": i.get("publicationYear", ""), "doi": i.get("doi", "")} for i in data.get("results", [])]
|
return [{"title": i.get("title", ""), "abstract": i.get("abstract", "")[:500] if i.get("abstract") else "", "year": i.get("publicationYear", ""), "doi": i.get("doi", "")} for i in data.get("results", [])]
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"CORE query error: {e}")
|
print(f"CORE query error: {e}")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
TWO_ALPHA = 2 / 137
|
||||||
|
RYDBERG_CM = 109677.581
|
||||||
|
RYDBERG_MHZ = 109677.581 * 29.9792458 # Convert cm^-1 to MHz (R_H in frequency units)
|
||||||
|
META_SOLID_RATIO = 1 / 7
|
||||||
|
|
||||||
# Phase 1: Rydberg quantum defect data
|
# Phase 1: Rydberg quantum defect data
|
||||||
# F-state quantum defects for alkalis (literature values):
|
# F-state quantum defects for alkalis (literature values):
|
||||||
# Rb F5/2: δ ≈ 3.9 (Li et al. Phys Rev A 67, 052502 2003)
|
# Rb F5/2: δ ≈ 3.9 (Li et al. Phys Rev A 67, 052502 2003)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue