33import jsonpath
44import requests
55from bs4 import BeautifulSoup
6+ from rich import json
67
78from cxapi .schema import QuestionModel
8-
99from . import SearcherBase , SearcherResp
1010
1111
1212class RestApiSearcher (SearcherBase ):
1313 """UrlQuery REST API 在线搜索器"""
14-
1514 session : requests .Session
1615 q_field : str
17- o_field : Optional [ list [str ]]
16+ o_field : list [str ] | None
1817 a_query : jsonpath .JSONPath
1918 url : str
2019 method : Literal ["GET" , "POST" ]
@@ -73,7 +72,7 @@ class JsonApiSearcher(SearcherBase):
7372
7473 session : requests .Session
7574 q_field : str
76- o_field : Optional [list [ str ] ]
75+ o_field : Optional [str ]
7776 a_query : jsonpath .JSONPath
7877 url : str
7978
@@ -86,6 +85,7 @@ def __init__(
8685 headers : Optional [dict ] = None , # 自定义头部
8786 ext_params : Optional [dict ] = None , # 扩展请求字段
8887 ) -> None :
88+ self .question : str | None = None
8989 self .session = requests .Session ()
9090 self .url = url
9191 if headers :
@@ -189,8 +189,10 @@ def __init__(self, token: str) -> None:
189189 super ().__init__ (
190190 url = "http://api.tikuhai.com/search" ,
191191 headers = {
192+ "Host" : "api.tikuhai.com" , # 缺少无法搜索
192193 "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.60" ,
193194 "referer" : "https://mooc1.chaoxing.com" ,
195+ "Content-Type" : "application/json" # 缺少无法搜索
194196 },
195197 a_field = "$.data.answer" ,
196198 ext_params = {
@@ -224,7 +226,9 @@ def __init__(self) -> None:
224226 super ().__init__ (
225227 url = "https://api.muketool.com/cx/v2/query" ,
226228 headers = {
229+ "Host" : "api.muketool.com" ,
227230 "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.60" ,
231+ "Content-Type" : "application/json"
228232 },
229233 a_field = "$.data" ,
230234 )
@@ -240,19 +244,42 @@ def parse(self, json_content: dict | list) -> SearcherResp:
240244class LyCk6Searcher (JsonApiSearcher ):
241245 """冷月题库搜索器"""
242246
243- def __init__ (self ) -> None :
244- super ().__init__ (
245- url = "https://lyck6.cn/scriptService/api/autoFreeAnswer" ,
246- a_field = "$.result.answers[0][0]" ,
247- )
247+ def __init__ (self , token : str , gpt : int ) -> None :
248+ if token is None or len (token ) != 10 :
249+ super ().__init__ (
250+ url = "https://lyck6.cn/scriptService/api/autoFreeAnswer" ,
251+ headers = {
252+ "Content-Type" : "application/json" ,
253+ },
254+ a_field = "$.result.answers[0][0]" ,
255+ )
256+ else :
257+ super ().__init__ (
258+ url = "https://lyck6.cn//scriptService/api/autoAnswer/" + token + "?gpt=" + str (gpt ),
259+ headers = {
260+ "Content-Type" : "application/json" ,
261+ },
262+ a_field = "$.result.answers[0][0]" ,
263+ )
264+
265+ @staticmethod
266+ def code_to_err (num ):
267+ numbers = {
268+ 403 : "请不要挂梯子或使用任何网络代理工具" ,
269+ 444 : "您请求速率过大,IP已经被封禁,请等待片刻或者更换IP" ,
270+ 415 : "请不要使用手机运行此脚本,否则可能出现异常" ,
271+ 429 : "免费题库搜题整体使用人数突增,系统繁忙,请耐心等待或使用付费题库..." ,
272+ 500 : "服务器发生预料之外的错误" ,
273+ 502 : "运维哥哥正在火速部署服务器,请稍等片刻,1分钟内恢复正常" ,
274+ 503 : "搜题服务不可见,请稍等片刻,1分钟内恢复正常" ,
275+ 504 : "系统超时"
276+ }
277+ return numbers .get (num , None )
248278
249279 def parse (self , json_content : dict ) -> SearcherResp :
250- if jsonpath .compile ("$.code" ).parse (json_content )[0 ] != 0 :
251- return SearcherResp (- 404 , "搜索失败" , self , self .question , None )
252- if jsonpath .compile ("$.code" ).parse (json_content )[0 ] == 429 :
253- return SearcherResp (- 429 , "访问频繁已限速" , self , self .question , None )
254- if jsonpath .compile ("$.code" ).parse (json_content )[0 ] == 444 :
255- return SearcherResp (- 444 , "服务繁忙,请稍后重试" , self , self .question , None )
280+ code = jsonpath .compile ("$.code" ).parse (json_content )[0 ]
281+ if code != 0 :
282+ return SearcherResp (code , self .code_to_err (code ), self , self .question , None )
256283 if result := self .rsp_query .parse (json_content ):
257284 return SearcherResp (0 , "ok" , self , self .question , result [0 ])
258285 return SearcherResp (- 500 , "未匹配答案字段" , self , self .question , None )
0 commit comments