forked from aqua5230/usage
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathai_updates_loader.py
More file actions
167 lines (142 loc) · 5.03 KB
/
Copy pathai_updates_loader.py
File metadata and controls
167 lines (142 loc) · 5.03 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright (C) 2026 lollapalooza <https://github.com/aqua5230>
#
# Part of "usage". Free software licensed under the GNU Affero General Public
# License v3.0 only; see the LICENSE file for full terms and the warranty disclaimer.
from __future__ import annotations
import contextlib
import json
import logging
import os
import tempfile
import time
import urllib.error
import urllib.request
from pathlib import Path
from typing import Any
logger = logging.getLogger(__name__)
AI_UPDATES_URL = "https://raw.githubusercontent.com/aqua5230/usage/main/ai_updates.json"
CACHE_PATH = Path(os.path.expanduser("~/.usage/ai_updates_cache.json"))
CACHE_TTL_SECONDS = 86400
USER_AGENT = "usage/0.9"
def load_ai_updates() -> list[dict[str, Any]] | None:
try:
cached = _read_cache()
if cached is not None and _cache_is_fresh(CACHE_PATH):
return cached
fetched_payload = _fetch_payload()
if fetched_payload is not None:
fetched = _normalize_payload(fetched_payload)
if fetched is not None:
_write_cache(fetched_payload)
return fetched
if cached is not None:
return cached
return None
except Exception:
_debug_warning("failed to load AI updates")
return None
def _cache_is_fresh(path: Path) -> bool:
try:
return (time.time() - path.stat().st_mtime) <= CACHE_TTL_SECONDS
except OSError:
return False
def _read_cache() -> list[dict[str, Any]] | None:
try:
with CACHE_PATH.open(encoding="utf-8") as file:
payload = json.load(file)
except (OSError, UnicodeDecodeError, json.JSONDecodeError):
_debug_warning(f"failed to read AI updates cache {CACHE_PATH}")
return None
return _normalize_payload(payload)
def _fetch_payload() -> Any | None:
request = urllib.request.Request(
AI_UPDATES_URL,
headers={"User-Agent": USER_AGENT},
)
try:
with urllib.request.urlopen(request, timeout=10) as response:
return json.loads(response.read().decode("utf-8"))
except (
OSError,
UnicodeDecodeError,
json.JSONDecodeError,
urllib.error.URLError,
):
_debug_warning(f"failed to fetch AI updates from {AI_UPDATES_URL}")
return None
def _write_cache(payload: Any) -> None:
tmp_path: str | None = None
try:
with contextlib.suppress(OSError):
CACHE_PATH.parent.mkdir(parents=True, exist_ok=True)
fd, tmp_path = tempfile.mkstemp(dir=CACHE_PATH.parent, suffix=".tmp")
with os.fdopen(fd, "w", encoding="utf-8") as file:
json.dump(payload, file, ensure_ascii=False, indent=2)
os.replace(tmp_path, CACHE_PATH)
tmp_path = None
except OSError:
_debug_warning(f"failed to write AI updates cache {CACHE_PATH}")
finally:
if tmp_path and os.path.exists(tmp_path):
with contextlib.suppress(OSError):
os.unlink(tmp_path)
def _normalize_payload(payload: Any) -> list[dict[str, Any]] | None:
if not isinstance(payload, dict):
return None
raw_tools = payload.get("tools")
if not isinstance(raw_tools, list):
return None
tools: list[dict[str, Any]] = []
for raw_tool in raw_tools:
if not isinstance(raw_tool, dict):
continue
required_keys = ("id", "name", "version", "period")
if not all(isinstance(raw_tool.get(key), str) for key in required_keys):
continue
raw_items = raw_tool.get("items")
if not isinstance(raw_items, list):
continue
items: list[dict[str, Any]] = []
for raw_item in raw_items:
if not isinstance(raw_item, dict):
continue
title = raw_item.get("title")
body = raw_item.get("body")
original = raw_item.get("original")
if not isinstance(title, dict) or not isinstance(body, dict):
continue
if not all(
isinstance(key, str) and isinstance(value, str)
for key, value in title.items()
):
continue
if not all(
isinstance(key, str) and isinstance(value, str)
for key, value in body.items()
):
continue
if not isinstance(original, str):
continue
items.append(
{
"title": title,
"body": body,
"original": original,
}
)
if not items:
continue
tools.append(
{
"id": raw_tool["id"],
"name": raw_tool["name"],
"version": raw_tool["version"],
"period": raw_tool["period"],
"items": items,
}
)
return tools
def _debug_warning(message: str) -> None:
if os.environ.get("USAGE_DEBUG") == "1":
logger.warning(message, exc_info=True)