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