gengwiki 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
gengwiki/__init__.py ADDED
@@ -0,0 +1,55 @@
1
+ __version__ = "0.1.0"
2
+
3
+ from .client import GengWiki # noqa: E402
4
+ from .errors import ( # noqa: E402
5
+ AccountApiUnavailable,
6
+ AuthError,
7
+ BadRequestError,
8
+ BadResponseError,
9
+ ConflictError,
10
+ ForbiddenError,
11
+ GengWikiError,
12
+ NetworkError,
13
+ NotFoundError,
14
+ RateLimitError,
15
+ ServerError,
16
+ UnavailableError,
17
+ )
18
+ from .models import ( # noqa: E402
19
+ ApiKey,
20
+ Lexicon,
21
+ LexiconEntry,
22
+ LocalKey,
23
+ Meme,
24
+ MemeSummary,
25
+ Persona,
26
+ RateLimit,
27
+ Sticker,
28
+ StickerPack,
29
+ )
30
+
31
+ __all__ = [
32
+ "AccountApiUnavailable",
33
+ "ApiKey",
34
+ "AuthError",
35
+ "BadRequestError",
36
+ "BadResponseError",
37
+ "ConflictError",
38
+ "ForbiddenError",
39
+ "GengWiki",
40
+ "GengWikiError",
41
+ "Lexicon",
42
+ "LexiconEntry",
43
+ "LocalKey",
44
+ "Meme",
45
+ "MemeSummary",
46
+ "NetworkError",
47
+ "NotFoundError",
48
+ "Persona",
49
+ "RateLimit",
50
+ "RateLimitError",
51
+ "ServerError",
52
+ "Sticker",
53
+ "StickerPack",
54
+ "UnavailableError",
55
+ ]
gengwiki/__main__.py ADDED
@@ -0,0 +1,179 @@
1
+ """The command line. Mostly for first-time setup, so a password or key never has to sit in a script or shell history."""
2
+
3
+ import argparse
4
+ import getpass
5
+ import sys
6
+
7
+ from . import GengWiki, GengWikiError, __version__
8
+
9
+
10
+ def _memes(rows) -> None:
11
+ if not rows:
12
+ print("没有找到")
13
+ for m in rows:
14
+ print(f"{m.title} [{m.slug}] {m.summary}")
15
+
16
+
17
+ def _login(c: GengWiki, a) -> None:
18
+ user = c.keys.login(a.email or input("邮箱 "), getpass.getpass("密码 "))
19
+ print(f"已登录 {user.get('email', '')},7 天内有效,密码没有保存")
20
+
21
+
22
+ def _logout(c: GengWiki, a) -> None:
23
+ c.keys.logout()
24
+ print("已退出")
25
+
26
+
27
+ def _status(c: GengWiki, a) -> None:
28
+ print(f"站点 {c.base_url}")
29
+ print(f"登录 {'已登录' if c.keys.logged_in else '未登录'}")
30
+ if not c._key:
31
+ print(f"Key 没有,到 {c.site_url}/me 创建后运行 gengwiki keys add 名字")
32
+ return
33
+ saved = next((k for k in c.keys.local() if k.current), None)
34
+ print(f"Key {saved.name if saved else '来自参数或环境变量'} {c._key[:15]}")
35
+ persona = c.persona()
36
+ print(f"人设 {persona.title if persona else '没有'}")
37
+ try:
38
+ r = c.rate_limit()
39
+ print(f"额度 今天 {r.used_today}/{r.per_day},这一分钟 {r.used_this_minute}/{r.per_minute}")
40
+ except GengWikiError as e:
41
+ print(f"额度 暂时查不到,{e.message}")
42
+
43
+
44
+ def _keys_local(c: GengWiki, a) -> None:
45
+ rows = c.keys.local()
46
+ if not rows:
47
+ print(f"还没有保存 Key,到 {c.site_url}/me 创建后运行 gengwiki keys add 名字")
48
+ for k in rows:
49
+ print(f"{'*' if k.current else ' '} {k.name} {k.prefix} {k.persona_title or ''}".rstrip())
50
+
51
+
52
+ def _keys_add(c: GengWiki, a) -> None:
53
+ print(f"已保存 {c.keys.add(a.name, getpass.getpass('粘贴 Key ').strip()).name}")
54
+
55
+
56
+ def _keys_use(c: GengWiki, a) -> None:
57
+ print(f"已切换到 {c.keys.use(a.name).name}")
58
+
59
+
60
+ def _keys_remove(c: GengWiki, a) -> None:
61
+ c.keys.remove(a.name)
62
+ print(f"已从本机删除 {a.name},服务器上依然有效")
63
+
64
+
65
+ def _keys_secret(c: GengWiki, a) -> None:
66
+ print(c.keys.secret(a.name))
67
+
68
+
69
+ def _keys_remote(c: GengWiki, a) -> None:
70
+ for k in c.keys.list():
71
+ print(f"{k.name} {k.prefix} 最后使用 {k.last_used_at or '从未'}")
72
+
73
+
74
+ def _keys_rotate(c: GengWiki, a) -> None:
75
+ print(f"已换新密钥并保存到 {c.keys.rotate(a.name).name},旧密钥立即失效")
76
+
77
+
78
+ def _keys_revoke(c: GengWiki, a) -> None:
79
+ c.keys.revoke(a.name)
80
+ print(f"已吊销 {a.name}")
81
+
82
+
83
+ def _personas(c: GengWiki, a) -> None:
84
+ rows = c.personas(a.q)
85
+ if not rows:
86
+ print("没有找到")
87
+ for p in rows:
88
+ print(f"{p.title} {p.description}".rstrip())
89
+
90
+
91
+ def _use(c: GengWiki, a) -> None:
92
+ print(f"已切换到 {c.use_persona(a.persona).title}")
93
+
94
+
95
+ def _search(c: GengWiki, a) -> None:
96
+ _memes(c.search_memes(a.q))
97
+
98
+
99
+ def _trending(c: GengWiki, a) -> None:
100
+ _memes(c.trending(a.tag))
101
+
102
+
103
+ def _meme(c: GengWiki, a) -> None:
104
+ m = c.meme(a.slug)
105
+ print(m.title)
106
+ if m.aliases:
107
+ print("别名 " + "、".join(m.aliases))
108
+ print(f"\n{m.body or m.summary}")
109
+ if m.usage_scenario:
110
+ print(f"\n使用场景 {m.usage_scenario}")
111
+ for example in m.usage_examples:
112
+ print(f"\n {example}")
113
+
114
+
115
+ def _sync(c: GengWiki, a) -> None:
116
+ s = c.sync(images=a.images)
117
+ print(f"梗 {s['memes']} 个,表情 {s['stickers']} 个,图片 {s['images']} 张,发出 {s['requests']} 个请求")
118
+
119
+
120
+ def _parser() -> argparse.ArgumentParser:
121
+ parser = argparse.ArgumentParser(prog="gengwiki", description="梗Wiki 命令行")
122
+ parser.add_argument("--version", action="version", version=__version__)
123
+ parser.set_defaults(run=None)
124
+ sub = parser.add_subparsers(metavar="命令")
125
+
126
+ def command(parent, name, run, help_text, *args):
127
+ p = parent.add_parser(name, help=help_text)
128
+ p.set_defaults(run=run)
129
+ for arg, kwargs in args:
130
+ p.add_argument(arg, **kwargs)
131
+ return p
132
+
133
+ optional = {"nargs": "?", "default": ""}
134
+ command(sub, "status", _status, "当前站点、Key、人设和额度")
135
+ command(sub, "login", _login, "登录以管理 Key", ("email", optional))
136
+ command(sub, "logout", _logout, "退出登录")
137
+ command(sub, "search", _search, "搜索梗", ("q", optional))
138
+ command(sub, "meme", _meme, "查看一个梗", ("slug", {}))
139
+ command(sub, "trending", _trending, "热门榜", ("tag", optional))
140
+ command(sub, "personas", _personas, "浏览人设", ("q", optional))
141
+ command(sub, "use", _use, "切换人设", ("persona", {}))
142
+ command(sub, "sync", _sync, "下载全部内容供离线使用", ("--images", {"action": "store_true"}))
143
+
144
+ keys = command(sub, "keys", _keys_local, "列出已保存的 Key").add_subparsers(metavar="操作")
145
+ command(keys, "add", _keys_add, "保存在网站上创建的 Key", ("name", {}))
146
+ command(keys, "use", _keys_use, "切换到已保存的 Key", ("name", {}))
147
+ command(keys, "remove", _keys_remove, "只从本机删除", ("name", {}))
148
+ command(keys, "secret", _keys_secret, "输出密钥,交给别的程序", ("name", {}))
149
+ command(keys, "remote", _keys_remote, "列出账号上的 Key,需要登录")
150
+ command(keys, "rotate", _keys_rotate, "换新密钥,需要登录", ("name", {}))
151
+ command(keys, "revoke", _keys_revoke, "吊销并从本机删除,需要登录", ("name", {}))
152
+ return parser
153
+
154
+
155
+ def main(argv: list[str] | None = None) -> int:
156
+ for stream in (sys.stdout, sys.stderr):
157
+ if hasattr(stream, "reconfigure"):
158
+ # A pipe on a non-Chinese Windows is cp1252, which would turn all the text into question marks.
159
+ # On any encoding, one character it lacks must not end the program.
160
+ encoding = stream.encoding if "梗".encode(stream.encoding, "ignore") else "utf-8"
161
+ stream.reconfigure(encoding=encoding, errors="replace")
162
+ parser = _parser()
163
+ args = parser.parse_args(argv)
164
+ if args.run is None:
165
+ parser.print_help()
166
+ return 0
167
+ try:
168
+ with GengWiki() as client:
169
+ args.run(client, args)
170
+ except (GengWikiError, ValueError) as e:
171
+ print(f"错误 {getattr(e, 'message', e)}", file=sys.stderr)
172
+ return 1
173
+ except (KeyboardInterrupt, EOFError):
174
+ return 130
175
+ return 0
176
+
177
+
178
+ if __name__ == "__main__":
179
+ sys.exit(main())
gengwiki/_http.py ADDED
@@ -0,0 +1,136 @@
1
+ """Stdlib HTTP with typed errors. Knows nothing about caching."""
2
+
3
+ import email.message
4
+ import gzip
5
+ import http.client
6
+ import ipaddress
7
+ import json
8
+ import time
9
+ import urllib.error
10
+ import urllib.parse
11
+ import urllib.request
12
+ import zlib
13
+
14
+ from . import __version__
15
+ from .errors import (
16
+ AuthError,
17
+ BadRequestError,
18
+ BadResponseError,
19
+ ConflictError,
20
+ ForbiddenError,
21
+ GengWikiError,
22
+ NetworkError,
23
+ NotFoundError,
24
+ RateLimitError,
25
+ ServerError,
26
+ UnavailableError,
27
+ )
28
+
29
+ _BY_STATUS = {400: BadRequestError, 401: AuthError, 403: ForbiddenError, 404: NotFoundError, 409: ConflictError}
30
+
31
+
32
+ class _NoRedirect(urllib.request.HTTPRedirectHandler):
33
+ """urllib would forward the Authorization header to wherever a redirect points."""
34
+
35
+ def redirect_request(self, *args, **kwargs):
36
+ return None
37
+
38
+
39
+ def check_url(url: str) -> str:
40
+ """Bearer tokens only travel over HTTPS, plain HTTP is for a dev server on this machine."""
41
+ parts = urllib.parse.urlsplit(url)
42
+ if parts.scheme == "https" and parts.hostname:
43
+ return url.rstrip("/")
44
+ if parts.scheme == "http" and parts.hostname:
45
+ try:
46
+ loopback = ipaddress.ip_address(parts.hostname).is_loopback
47
+ except ValueError:
48
+ loopback = parts.hostname == "localhost"
49
+ if loopback:
50
+ return url.rstrip("/")
51
+ raise ValueError(f"{url!r} must be https, or http on localhost")
52
+
53
+
54
+ def _raise_for(status: int, body: bytes, headers: email.message.Message) -> None:
55
+ code, message = None, f"HTTP {status}"
56
+ try:
57
+ err = json.loads(body)["error"]
58
+ code, message = err["code"], err["message"]
59
+ except (ValueError, KeyError, TypeError):
60
+ pass
61
+ if status == 429:
62
+ try:
63
+ retry_after = float(headers.get("Retry-After", 0))
64
+ except ValueError:
65
+ retry_after = 0
66
+ raise RateLimitError(message, retry_after=retry_after)
67
+ if status >= 500:
68
+ raise ServerError(message, status, code)
69
+ raise _BY_STATUS.get(status, GengWikiError)(message, status, code)
70
+
71
+
72
+ class Http:
73
+ def __init__(self, timeout: float = 15):
74
+ self.timeout = timeout
75
+ self.count = 0
76
+ self._opener = urllib.request.build_opener(_NoRedirect)
77
+
78
+ # ponytail: urllib opens one connection per request. Requests are rare by design, pool with http.client if that changes.
79
+ def _once(self, req: urllib.request.Request) -> tuple[bytes, email.message.Message]:
80
+ self.count += 1
81
+ try:
82
+ with self._opener.open(req, timeout=self.timeout) as res:
83
+ body, headers = res.read(), res.headers
84
+ if headers.get("Content-Encoding") == "gzip":
85
+ body = gzip.decompress(body)
86
+ return body, headers
87
+ except urllib.error.HTTPError as e:
88
+ with e:
89
+ _raise_for(e.code, e.read(), e.headers)
90
+ raise # unreachable, _raise_for always raises
91
+ # A connection cut mid-response or a damaged body is the same outage to the caller.
92
+ except (OSError, EOFError, http.client.HTTPException, zlib.error) as e:
93
+ raise NetworkError(f"cannot reach {req.host}: {e!r}") from e
94
+
95
+ def request(
96
+ self,
97
+ method: str,
98
+ url: str,
99
+ *,
100
+ token: str | None = None,
101
+ params: dict | None = None,
102
+ body: dict | None = None,
103
+ retries: int = 0,
104
+ ) -> tuple[bytes, email.message.Message]:
105
+ if params:
106
+ url += "?" + urllib.parse.urlencode(params)
107
+ headers = {"User-Agent": f"gengwiki-python/{__version__}", "Accept-Encoding": "gzip"}
108
+ if token:
109
+ headers["Authorization"] = f"Bearer {token}"
110
+ data = None
111
+ if body is not None:
112
+ data = json.dumps(body).encode()
113
+ headers["Content-Type"] = "application/json"
114
+ req = urllib.request.Request(url, data=data, headers=headers, method=method)
115
+ for attempt in range(retries + 1):
116
+ try:
117
+ return self._once(req)
118
+ except UnavailableError as e:
119
+ # A long Retry-After is the daily limit, waiting it out would hang the caller.
120
+ wait = e.retry_after if isinstance(e, RateLimitError) else 0.5 * 2**attempt
121
+ if attempt == retries or wait > 5:
122
+ raise
123
+ time.sleep(wait)
124
+ raise AssertionError("unreachable")
125
+
126
+ def json(self, method: str, url: str, *, expect: type | None = None, **kwargs):
127
+ """The decoded body. With expect, anything but that type is a bad response, so it never reaches the cache."""
128
+ body, headers = self.request(method, url, **kwargs)
129
+ try:
130
+ value = json.loads(body) if body else None
131
+ except ValueError as e:
132
+ raise BadResponseError(f"expected JSON from {url}, got {headers.get_content_type()}", code="bad_response") from e
133
+ # ponytail: checks the outer type, not each row. Check fields if something in front ever rewrites rows.
134
+ if expect and not isinstance(value, expect):
135
+ raise BadResponseError(f"expected a JSON {expect.__name__} from {url}", code="bad_response")
136
+ return value
gengwiki/_keyring.py ADDED
@@ -0,0 +1,40 @@
1
+ """Saved API keys and the login token, in one JSON file only this user can read."""
2
+
3
+ import contextlib
4
+ import json
5
+ import os
6
+ import threading
7
+ from pathlib import Path
8
+ from typing import Iterator
9
+
10
+ from .errors import GengWikiError
11
+
12
+
13
+ # ponytail: a plaintext 0600 file, same as ~/.aws/credentials. Move to the OS keychain if someone needs that.
14
+ class Keyring:
15
+ def __init__(self, path: Path):
16
+ self.path = path
17
+ self._lock = threading.Lock()
18
+
19
+ def read(self) -> dict:
20
+ try:
21
+ data = json.loads(self.path.read_text("utf-8"))
22
+ except FileNotFoundError:
23
+ data = {}
24
+ except ValueError as e:
25
+ # Secrets may be in there and are shown only once, so never overwrite this file.
26
+ raise GengWikiError(f"{self.path} is not valid JSON, fix or delete it: {e}") from e
27
+ data.setdefault("keys", {})
28
+ return data
29
+
30
+ @contextlib.contextmanager
31
+ def edit(self) -> Iterator[dict]:
32
+ # ponytail: locks this process only. Two processes saving at once lose one edit, a file lock fixes that.
33
+ with self._lock:
34
+ data = self.read()
35
+ yield data
36
+ tmp = self.path.with_name(self.path.name + ".tmp")
37
+ fd = os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
38
+ with os.fdopen(fd, "w", encoding="utf-8") as f:
39
+ json.dump(data, f, ensure_ascii=False, indent=2)
40
+ os.replace(tmp, self.path)
gengwiki/_search.py ADDED
@@ -0,0 +1,54 @@
1
+ """The server's search, run locally, so a synced key answers without a request."""
2
+
3
+ import re
4
+
5
+
6
+ def _trigrams(text: str) -> set[str]:
7
+ """What pg_trgm's show_trgm returns: lowercased alphanumeric words, padded with two spaces in front and one behind."""
8
+ grams: set[str] = set()
9
+ for word in re.findall(r"[^\W_]+", text.lower()):
10
+ padded = f" {word} "
11
+ grams.update(padded[i : i + 3] for i in range(len(padded) - 2))
12
+ return grams
13
+
14
+
15
+ def similarity(a: str, b: str) -> float:
16
+ ga, gb = _trigrams(a), _trigrams(b)
17
+ return len(ga & gb) / len(ga | gb) if ga and gb else 0.0
18
+
19
+
20
+ def search_memes(q: str, rows: list[dict], limit: int) -> list[dict]:
21
+ """Same match and order as common::memes::search over search_text, which is the title and the aliases."""
22
+ q = q.strip()
23
+ if not q:
24
+ return rows[:limit]
25
+ scored = []
26
+ for r in rows:
27
+ text = " ".join([r["title"], *r["aliases"]])
28
+ sim = similarity(text, q)
29
+ if q.lower() in text.lower() or sim > 0.2:
30
+ scored.append((sim, r))
31
+ scored.sort(key=lambda x: (-x[0], -x[1]["heat"]))
32
+ return [r for _, r in scored[:limit]]
33
+
34
+
35
+ def search_stickers(q: str, packs: list[dict], limit: int) -> list[dict]:
36
+ """Same match as the API's strpos over the caption, the pack title and the pack tags, in pack order."""
37
+ q = q.strip().lower()
38
+ found = []
39
+ for p in packs:
40
+ for s in p["stickers"]:
41
+ if q in " ".join([s["caption"], p["title"], *p["tags"]]).lower():
42
+ found.append({**s, "pack_id": p["id"], "pack_title": p["title"]})
43
+ return found[:limit]
44
+
45
+
46
+ def search_personas(q: str, rows: list[dict]) -> list[dict]:
47
+ """Same match as the catalog's strpos over the title, the description and the tags."""
48
+ q = q.strip().lower()
49
+ return [r for r in rows if q in " ".join([r["title"], r.get("description", ""), *r.get("tags", [])]).lower()]
50
+
51
+
52
+ def search_entries(q: str, entries: list[dict], limit: int) -> list[dict]:
53
+ q = q.strip().lower()
54
+ return [e for e in entries if q in " ".join([e["term"], *e["aliases"], e["note"], *e["tags"]]).lower()][:limit]
gengwiki/_store.py ADDED
@@ -0,0 +1,76 @@
1
+ """SQLite cache of raw API responses. A cache must never break the caller, so its failures read as misses."""
2
+
3
+ import hashlib
4
+ import json
5
+ import sqlite3
6
+ import threading
7
+ import time
8
+ from pathlib import Path
9
+ from typing import Any, NamedTuple
10
+
11
+ # The table name carries the format version, a new format simply starts empty.
12
+ _SCHEMA = "create table if not exists cache_v1 (ns text, key text, value text, fetched_at real, primary key (ns, key))"
13
+
14
+
15
+ def namespace(key: str | None) -> str:
16
+ """Cache rows are filed under a hash, the key itself never reaches the cache."""
17
+ return hashlib.sha256((key or "").encode()).hexdigest()[:16]
18
+
19
+
20
+ class Hit(NamedTuple):
21
+ value: Any
22
+ age: float
23
+
24
+
25
+ def _connect(path: Path | str) -> sqlite3.Connection:
26
+ # The default busy timeout covers several processes sharing one cache.
27
+ db = sqlite3.connect(path, check_same_thread=False)
28
+ db.execute("pragma journal_mode=wal")
29
+ db.execute(_SCHEMA)
30
+ return db
31
+
32
+
33
+ class Store:
34
+ def __init__(self, path: Path):
35
+ self._lock = threading.Lock()
36
+ try:
37
+ self._db = _connect(path)
38
+ except sqlite3.DatabaseError:
39
+ # Corrupt or read-only. Nothing in here is precious, start over and stay in memory if the disk refuses.
40
+ try:
41
+ path.unlink(missing_ok=True)
42
+ self._db = _connect(path)
43
+ except (OSError, sqlite3.Error):
44
+ self._db = _connect(":memory:")
45
+
46
+ def get(self, ns: str, key: str) -> Hit | None:
47
+ try:
48
+ with self._lock:
49
+ row = self._db.execute("select value, fetched_at from cache_v1 where ns = ? and key = ?", (ns, key)).fetchone()
50
+ except sqlite3.Error:
51
+ return None
52
+ return Hit(json.loads(row[0]), time.time() - row[1]) if row else None
53
+
54
+ def put(self, ns: str, key: str, value: Any) -> None:
55
+ self._run("insert or replace into cache_v1 values (?, ?, ?, ?)", (ns, key, json.dumps(value, ensure_ascii=False), time.time()))
56
+
57
+ def expire(self, ns: str) -> None:
58
+ """Keep the rows as a fallback, but make the next read go to the network."""
59
+ self._run("update cache_v1 set fetched_at = 0 where ns = ?", (ns,))
60
+
61
+ def drop(self, ns: str) -> None:
62
+ self._run("delete from cache_v1 where ns = ?", (ns,))
63
+
64
+ def rename(self, old: str, new: str) -> None:
65
+ self._run("update cache_v1 set ns = ? where ns = ?", (new, old))
66
+
67
+ def _run(self, sql: str, args: tuple) -> None:
68
+ try:
69
+ with self._lock, self._db:
70
+ self._db.execute(sql, args)
71
+ except sqlite3.Error:
72
+ pass
73
+
74
+ def close(self) -> None:
75
+ with self._lock:
76
+ self._db.close()