#!/usr/bin/env python3 """ 提交小说到 148 wanshuge(杰奇)—— 客户端示例 依赖:pip install requests 用法: python qu30_submit.py ping python qu30_submit.py sorts python qu30_submit.py demo # 提交一本测试书(3 章)再查回 python qu30_submit.py dir <书目录> # 目录里: info.json + cover.jpg(可选) + 001_第一章.txt ... python qu30_submit.py info python qu30_submit.py delete # 只能删 API 提交的书 info.json 格式: {"articlename":"书名","author":"作者","sortname":"都市","intro":"简介","fullflag":0,"cover_url":"https://..."} 章节 txt:文件名排序即章节顺序,文件名去掉 "001_" 前缀和 .txt 后缀就是章节名;内容 UTF-8。 """ import base64 import json import os import sys import time import requests API = os.environ.get("QU30_API", "https://api.qu30.cc/") TOKEN = os.environ.get("QU30_TOKEN", "在这里填 token") TARGET = os.environ.get("QU30_TARGET", "") # 目标库名(下载页「⑦ 目标数据库」),空 = 默认库 BATCH = 50 # 每次提交章节数(服务端上限 200) def call(action, **data): data["action"] = action if TARGET and "target" not in data: data["target"] = TARGET for attempt in range(3): try: r = requests.post(API, json=data, headers={"X-Token": TOKEN}, timeout=120) j = r.json() if j.get("code") != 0 and not (j.get("code") == 404 and action == "info"): raise RuntimeError(f"{action} 失败: {j.get('msg')}") return j except (requests.RequestException, ValueError) as e: if attempt == 2: raise print(f" 网络错误,重试: {e}") time.sleep(3) def submit_book(articlename, author, chapters, sortname="", sortid=0, intro="", fullflag=0, cover_url="", cover_path="", update=False): """chapters: [(章节名, 正文), ...]。已存在的书/同名章节会自动跳过,可重复运行(断点续传)。""" book = dict(articlename=articlename, author=author, sortname=sortname, sortid=sortid, intro=intro, fullflag=fullflag, update=update) if cover_path and os.path.isfile(cover_path): book["cover_b64"] = base64.b64encode(open(cover_path, "rb").read()).decode() elif cover_url: book["cover_url"] = cover_url r = call("book", **book) aid = r["data"]["articleid"] print(f"[{r['msg']}] articleid={aid} 《{articlename}》 封面={r['data'].get('cover')}") total = 0 for i in range(0, len(chapters), BATCH): part = [{"chaptername": t, "content": c} for t, c in chapters[i:i + BATCH]] r = call("chapters", articleid=aid, chapters=part, skip_exist=True) d = r["data"] total += d["added"] errs = [x for x in d["results"] if x["status"] == "error"] print(f" 章节 {i + 1}-{i + len(part)}: 新增 {d['added']},跳过 {sum(x['status'] == 'skip' for x in d['results'])},错误 {len(errs)}") for e in errs: print(f" ✗ {part[e['i']]['chaptername']}: {e['msg']}") print(f" 完成:本次新增 {total} 章,书内共 {d['book']['chapters']} 章,最新:{d['book']['lastchapter']}") return aid def submit_dir(path): info = json.load(open(os.path.join(path, "info.json"), encoding="utf-8")) files = sorted(f for f in os.listdir(path) if f.endswith(".txt")) chapters = [] for f in files: name = os.path.splitext(f)[0] if "_" in name and name.split("_", 1)[0].isdigit(): name = name.split("_", 1)[1] chapters.append((name, open(os.path.join(path, f), encoding="utf-8").read())) cover = os.path.join(path, "cover.jpg") return submit_book(chapters=chapters, cover_path=cover, **info) if __name__ == "__main__": cmd = sys.argv[1] if len(sys.argv) > 1 else "ping" if cmd == "ping": print(call("ping")) elif cmd == "sorts": for s in call("sorts")["data"]: print(s["sortid"], s["caption"], s["shortname"]) elif cmd == "demo": chs = [(f"第{n}章 测试章节{n}", f"这是测试书第{n}章的第一段正文。\n这是第二段,用来检查分段。\n第三段:「引号」……省略号——破折号。") for n in range(1, 4)] aid = submit_book("API提交测试书", "测试作者", chs, sortname="都市", intro="这是一本通过 API 提交的测试书。", cover_url="https://p3-tt.byteimg.com/img/novel-pic/4900f950c7af7f82fdc14cf528e0e288~200x260.jpg") print(json.dumps(call("info", articleid=aid)["data"], ensure_ascii=False, indent=1)[:800]) elif cmd == "dir": submit_dir(sys.argv[2]) elif cmd == "info": print(json.dumps(call("info", articleid=int(sys.argv[2]))["data"], ensure_ascii=False, indent=1)) elif cmd == "delete": print(call("delete", articleid=int(sys.argv[2]))) else: print(__doc__)