From b1698384439495bd16b01cfd27b1d2443bf50ed2 Mon Sep 17 00:00:00 2001 From: anian Date: Tue, 9 Jun 2026 13:42:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=AD=A6=E6=9C=AF=E4=BC=9A?= =?UTF-8?q?=E8=AE=AE=E3=80=81=E6=9C=9F=E5=88=8A=E7=AD=89=E7=BA=A7=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 +++ main.py | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/README.md b/README.md index aab4cdf..7dc6e09 100644 --- a/README.md +++ b/README.md @@ -2,5 +2,8 @@ > 基于Python的本地Web API项目 +#### v260605 +1. 新增期刊查询接口(数据源easyscholar) + #### v260512 1. 新增农历、运势、唐诗、地址、时间、谜语等接口及对应数据 diff --git a/main.py b/main.py index ca0192f..ebc31a5 100755 --- a/main.py +++ b/main.py @@ -9,6 +9,9 @@ import ip2region.util as util from pathlib import Path import json import random +import urllib.error +import urllib.parse +import urllib.request class LowercasePathMiddleware: @@ -109,6 +112,7 @@ a:hover{text-decoration:underline;} 地址接口/ip/api/ip/help 时间接口/time/api/time/help 谜语接口/riddle/api/riddle/help +期刊接口/sci/api/sci/help """ @@ -514,6 +518,71 @@ def riddle(): return jsonify({"items": items, "params": params}) +@app.route("/sci/help") +def sci_help(): + return jsonify({ + "路径": "/sci/api", + "功能": "获取期刊信息", + "参数": { + "nane=xxx": "必填,期刊名称(可缩写)", + }, + }) + +@app.route("/sci/api") +def sci(): + args_lower = {k.lower(): v for k, v in request.args.items()} + if set(args_lower.keys()) - {"nane", "name"}: + return jsonify({ + "error": f"{set(args_lower.keys()) - {"nane", "name"}}参数非法", + "allowed_params": ["nane=xxx"], + }) + publication_name = args_lower.get("nane", "").strip() or args_lower.get("name", "").strip() + params = {"nane": publication_name} + if not publication_name: + resp = {"error": "nane参数不能为空"} + resp["params"] = params + return jsonify(resp) + query = urllib.parse.urlencode({ + "secretKey": "0361aa1c9a8b4a30889ab6b02eae902c", + "publicationName": publication_name, + }) + url = f"https://www.easyscholar.cc/open/getPublicationRank?{query}" + try: + request_obj = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"}) + with urllib.request.urlopen(request_obj, timeout=10) as response: + raw_data = response.read().decode("utf-8") + except urllib.error.HTTPError as error: + resp = {"error": f"期刊接口请求失败: HTTP {error.code}"} + resp["params"] = params + return jsonify(resp) + except urllib.error.URLError as error: + resp = {"error": f"期刊接口请求失败: {error.reason}"} + resp["params"] = params + return jsonify(resp) + try: + result = json.loads(raw_data) + except json.JSONDecodeError: + resp = {"error": "期刊接口返回内容不是有效JSON"} + resp["params"] = params + return jsonify(resp) + if result.get("code") != 200: + resp = {"error": result.get("msg", "期刊接口返回异常")} + resp["params"] = params + return jsonify(resp) + data = result.get("data") or {} + official_rank = data.get("officialRank") or {} + all_rank = official_rank.get("all") or {} + resp = { + "CCF等级": all_rank.get("ccf", ""), + "影响因子": all_rank.get("sciif", ""), + "JCR分区": all_rank.get("sci", ""), + "中科院分区": all_rank.get("sciUp", ""), + "TOP期刊": all_rank.get("sciUpTop", ""), + } + resp["params"] = params + return jsonify(resp) + + @app.errorhandler(404) def not_found(_error): return redirect("/"), 302