spider.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. /*
  2. * @File : spider.js
  3. * @Author : jade
  4. * @Date : 2023/12/25 17:19
  5. * @Email : jadehh@1ive.com
  6. * @Software : Samples
  7. * @Desc :
  8. */
  9. import {JadeLogging} from "../lib/log.js";
  10. import * as Utils from "../lib/utils.js";
  11. import {VodDetail, VodShort} from "../lib/vod.js";
  12. import {_, load, Uri} from "../lib/cat.js";
  13. import * as HLS from "../lib/hls.js";
  14. import {hlsCache, tsCache} from "../lib/ffm3u8_open.js";
  15. import {DanmuSpider} from "../lib/danmuSpider.js";
  16. import {initAli} from "../lib/ali.js";
  17. class Result {
  18. constructor() {
  19. this.class = []
  20. this.list = []
  21. this.filters = []
  22. this.header = {"User-Agent": Utils.CHROME};
  23. this.format = "";
  24. this.danmaku = "";
  25. this.url = "";
  26. this.subs = [];
  27. this.parse = 0
  28. this.jx = 0;
  29. this.page = 0
  30. this.pagecount = 0
  31. this.limit = 0;
  32. this.total = 0;
  33. this.extra = {}
  34. }
  35. get() {
  36. return new Result()
  37. }
  38. home(classes, list, filters) {
  39. return JSON.stringify({
  40. "class": classes, "list": list, "filters": filters
  41. })
  42. }
  43. homeVod(vod_list) {
  44. return JSON.stringify({"page": this.page, "list": vod_list, "pagecount": this.page, "total": this.page})
  45. }
  46. category(vod_list, page, count, limit, total) {
  47. return JSON.stringify({
  48. page: parseInt(page), pagecount: count, limit: limit, total: total, list: vod_list,
  49. });
  50. }
  51. search(vod_list) {
  52. return JSON.stringify({"list": vod_list})
  53. }
  54. detail(vod_detail) {
  55. return JSON.stringify({"list": [vod_detail]})
  56. }
  57. play(url) {
  58. if (!_.isEmpty(this.danmaku)) {
  59. return JSON.stringify({
  60. "url": url,
  61. "parse": this.parse,
  62. "header": this.header,
  63. "format": this.format,
  64. "subs": this.subs,
  65. "danmaku": this.danmaku,
  66. "extra":this.extra,
  67. "jx": this.jx
  68. })
  69. } else {
  70. return JSON.stringify({
  71. "url": url,
  72. "parse": this.parse,
  73. "header": this.header,
  74. "format": this.format,
  75. "subs": this.subs,
  76. "extra":this.extra,
  77. "jx": this.jx
  78. })
  79. }
  80. }
  81. playTxt(url){
  82. return url
  83. }
  84. errorCategory(error_message) {
  85. let vodShort = new VodShort()
  86. vodShort.vod_name = "错误:打开无效"
  87. vodShort.vod_id = "error"
  88. vodShort.vod_pic = Utils.RESOURCEURL + "/resources/error.png"
  89. vodShort.vod_remarks = error_message
  90. return JSON.stringify({
  91. page: parseInt(0), pagecount: 0, limit: 0, total: 0, list: [vodShort],
  92. })
  93. }
  94. setClass(classes) {
  95. this.class = classes;
  96. return this;
  97. }
  98. setVod(list) {
  99. if (typeof list === "object" && Array.isArray(list)) {
  100. this.list = list;
  101. } else if (list !== undefined) {
  102. this.list = [list]
  103. }
  104. return this;
  105. }
  106. setFilters(filters) {
  107. this.filters = filters;
  108. return this;
  109. }
  110. setHeader(header) {
  111. this.header = header;
  112. return this;
  113. }
  114. setParse(parse) {
  115. this.parse = parse;
  116. return this;
  117. }
  118. setJx() {
  119. this.jx = 1;
  120. return this;
  121. }
  122. setUrl(url) {
  123. this.url = url;
  124. return this;
  125. }
  126. danmu(danmaku) {
  127. this.danmaku = danmaku;
  128. return this;
  129. }
  130. setFormat(format) {
  131. this.format = format;
  132. return this;
  133. }
  134. setSubs(subs) {
  135. this.subs = subs;
  136. return this;
  137. }
  138. dash() {
  139. this.format = "application/dash+xml";
  140. return this;
  141. }
  142. m3u8() {
  143. this.format = "application/x-mpegURL";
  144. return this;
  145. }
  146. rtsp() {
  147. this.format = "application/x-rtsp";
  148. return this;
  149. }
  150. octet() {
  151. this.format = "application/octet-stream";
  152. return this;
  153. }
  154. setPage(page, count, limit, total) {
  155. this.page = page
  156. this.limit = limit
  157. this.total = total
  158. this.pagecount = count
  159. return this;
  160. }
  161. toString() {
  162. return JSON.stringify(this);
  163. }
  164. }
  165. class Spider {
  166. constructor() {
  167. this.siteKey = ""
  168. this.siteType = 0
  169. this.jadeLog = new JadeLogging(this.getAppName(), "DEBUG")
  170. this.classes = []
  171. this.filterObj = {}
  172. this.result = new Result()
  173. this.catOpenStatus = true
  174. this.danmuStaus = false
  175. this.reconnectTimes = 0
  176. this.maxReconnectTimes = 5
  177. this.siteUrl = ""
  178. this.vodList = []
  179. this.homeVodList = []
  180. this.count = 0
  181. this.limit = 0
  182. this.total = 0
  183. this.page = 0
  184. this.vodDetail = new VodDetail()
  185. this.playUrl = ""
  186. this.header = {}
  187. this.remove18 = false
  188. this.type_id_18 = 0
  189. this.type_name_18 = "伦理片"
  190. this.episodeObj = {}
  191. this.danmuUrl = ""
  192. this.cfgObj = {}
  193. }
  194. async reconnnect(reqUrl, params, headers, redirect_url, return_cookie, buffer) {
  195. await this.jadeLog.error("请求失败,请检查url:" + reqUrl + ",两秒后重试")
  196. Utils.sleep(2)
  197. if (this.reconnectTimes < this.maxReconnectTimes) {
  198. this.reconnectTimes = this.reconnectTimes + 1
  199. return await this.fetch(reqUrl, params, headers, redirect_url, return_cookie, buffer)
  200. } else {
  201. await this.jadeLog.error("请求失败,重连失败")
  202. return null
  203. }
  204. }
  205. getClassIdList() {
  206. let class_id_list = []
  207. for (const class_dic of this.classes) {
  208. class_id_list.push(class_dic["type_id"])
  209. }
  210. return class_id_list
  211. }
  212. getTypeDic(type_name, type_id) {
  213. return {"type_name": type_name, "type_id": type_id}
  214. }
  215. async getHtml(url = this.siteUrl, headers = this.getHeader()) {
  216. let html = await this.fetch(url, null, headers)
  217. if (!_.isEmpty(html)) {
  218. return load(html)
  219. } else {
  220. await this.jadeLog.error(`html获取失败`, true)
  221. }
  222. }
  223. getClassNameList() {
  224. let class_name_list = []
  225. for (const class_dic of this.classes) {
  226. class_name_list.push(class_dic["type_name"])
  227. }
  228. return class_name_list
  229. }
  230. async postReconnect(reqUrl, params, headers) {
  231. await this.jadeLog.error("请求失败,请检查url:" + reqUrl + ",两秒后重试")
  232. Utils.sleep(2)
  233. if (this.reconnectTimes < this.maxReconnectTimes) {
  234. this.reconnectTimes = this.reconnectTimes + 1
  235. return await this.post(reqUrl, params, headers)
  236. } else {
  237. await this.jadeLog.error("请求失败,重连失败")
  238. return null
  239. }
  240. }
  241. getHeader() {
  242. return {"User-Agent": Utils.CHROME, "Referer": this.siteUrl + "/"};
  243. }
  244. async getResponse(reqUrl, params, headers, redirect_url, return_cookie, buffer, response) {
  245. {
  246. if (response.headers["location"] !== undefined) {
  247. if (redirect_url) {
  248. await this.jadeLog.debug(`返回重定向连接:${response.headers["location"]}`)
  249. return response.headers["location"]
  250. } else {
  251. return this.fetch(response.headers["location"], params, headers, redirect_url, return_cookie, buffer)
  252. }
  253. } else if (response.content.length > 0) {
  254. this.reconnectTimes = 0
  255. if (return_cookie) {
  256. return {"cookie": response.headers["set-cookie"], "content": response.content}
  257. } else {
  258. return response.content
  259. }
  260. } else if (buffer === 1) {
  261. this.reconnectTimes = 0
  262. return response.content
  263. } else {
  264. await this.jadeLog.error(`请求失败,请求url为:${reqUrl},回复内容为:${JSON.stringify(response)}`)
  265. return await this.reconnnect(reqUrl, params, headers, redirect_url, return_cookie, buffer)
  266. }
  267. }
  268. }
  269. async fetch(reqUrl, params, headers, redirect_url = false, return_cookie = false, buffer = 0) {
  270. let data = Utils.objectToStr(params)
  271. let url = reqUrl
  272. if (!_.isEmpty(data)) {
  273. url = reqUrl + "?" + data
  274. }
  275. let uri = new Uri(url);
  276. let response;
  277. if (redirect_url) {
  278. response = await req(uri.toString(), {
  279. method: "get", headers: headers, buffer: buffer, data: null, redirect: 2
  280. })
  281. } else {
  282. response = await req(uri.toString(), {method: "get", headers: headers, buffer: buffer, data: null});
  283. }
  284. if (response.code === 200 || response.code === 302 || response.code === 301 || return_cookie) {
  285. return await this.getResponse(reqUrl, params, headers, redirect_url, return_cookie, buffer, response)
  286. } else {
  287. await this.jadeLog.error(`请求失败,失败原因为:状态码出错,请求url为:${uri},回复内容为:${JSON.stringify(response)}`)
  288. return await this.reconnnect(reqUrl, params, headers, redirect_url, return_cookie, buffer)
  289. }
  290. }
  291. async redirect(response) {
  292. }
  293. async post(reqUrl, params, headers, postType = "form") {
  294. let uri = new Uri(reqUrl);
  295. let response = await req(uri.toString(), {
  296. method: "post", headers: headers, data: params, postType: postType
  297. });
  298. if (response.code === 200 || response.code === undefined || response.code === 302) {
  299. // 重定向
  300. if (response.headers["location"] !== undefined) {
  301. return await this.redirect(response)
  302. } else if (!_.isEmpty(response.content)) {
  303. this.reconnectTimes = 0
  304. return response.content
  305. } else {
  306. return await this.postReconnect(reqUrl, params, headers)
  307. }
  308. } else {
  309. await this.jadeLog.error(`请求失败,请求url为:${reqUrl},回复内容为${JSON.stringify(response)}`)
  310. return await this.postReconnect(reqUrl, params, headers)
  311. }
  312. }
  313. getName() {
  314. return `🍥┃基础┃🍥`
  315. }
  316. getAppName() {
  317. return `基础`
  318. }
  319. getJSName() {
  320. return "base"
  321. }
  322. getType() {
  323. return 3
  324. }
  325. async parseVodShortListFromDoc($) {
  326. }
  327. async parseVodShortListFromJson(obj) {
  328. }
  329. parseVodShortFromElement($, element) {
  330. }
  331. async parseVodShortListFromDocByCategory($) {
  332. }
  333. async getFilter($) {
  334. }
  335. async setClasses() {
  336. }
  337. async setFilterObj() {
  338. }
  339. async parseVodShortListFromDocBySearch($) {
  340. return []
  341. }
  342. async parseVodDetailFromDoc($) {
  343. }
  344. async parseVodDetailfromJson(obj) {
  345. }
  346. async parseVodPlayFromUrl(flag, play_url) {
  347. }
  348. async parseVodPlayFromDoc(flag, $) {
  349. }
  350. async SpiderInit(cfg) {
  351. try {
  352. this.siteKey = cfg["skey"]
  353. this.siteType = parseInt(cfg["stype"].toString())
  354. let extObj = null;
  355. if (typeof cfg.ext === "string") {
  356. await this.jadeLog.info(`读取配置文件,ext为:${cfg.ext}`)
  357. extObj = JSON.parse(cfg.ext)
  358. } else if (typeof cfg.ext === "object") {
  359. await this.jadeLog.info(`读取配置文件,所有参数为:${JSON.stringify(cfg)}`)
  360. await this.jadeLog.info(`读取配置文件,ext为:${JSON.stringify(cfg.ext)}`)
  361. extObj = cfg.ext
  362. } else {
  363. await this.jadeLog.error(`不支持的数据类型,数据类型为${typeof cfg.ext}`)
  364. }
  365. let boxType = extObj["box"]
  366. extObj["CatOpenStatus"] = boxType === "CatOpen";
  367. return extObj
  368. } catch (e) {
  369. await this.jadeLog.error("初始化失败,失败原因为:" + e.message)
  370. return {"token": null, "CatOpenStatus": false, "code": 0}
  371. }
  372. }
  373. async initAli(token, db = null) {
  374. await initAli(token, db)
  375. }
  376. async spiderInit() {
  377. }
  378. async init(cfg) {
  379. this.danmuSpider = new DanmuSpider()
  380. this.cfgObj = await this.SpiderInit(cfg)
  381. await this.jadeLog.debug(`初始化参数为:${JSON.stringify(cfg)}`)
  382. this.catOpenStatus = this.cfgObj.CatOpenStatus
  383. this.danmuStaus = this.cfgObj["danmu"] ?? this.danmuStaus
  384. try {
  385. if (await this.loadFilterAndClasses()) {
  386. await this.jadeLog.debug(`读取缓存列表和二级菜单成功`)
  387. } else {
  388. await this.jadeLog.warning(`读取缓存列表和二级菜单失败`)
  389. await this.writeFilterAndClasses()
  390. }
  391. } catch (e) {
  392. await local.set(this.siteKey, "classes", JSON.stringify([]));
  393. await local.set(this.siteKey, "filterObj", JSON.stringify({}));
  394. await this.jadeLog.error("读取缓存失败,失败原因为:" + e)
  395. }
  396. this.jsBase = await js2Proxy(true, this.siteType, this.siteKey, 'img/', {});
  397. this.douBanjsBase = await js2Proxy(true, this.siteType, this.siteKey, 'douban/', {});
  398. this.baseProxy = await js2Proxy(true, this.siteType, this.siteKey, 'img/', this.getHeader());
  399. this.videoProxy = await js2Proxy(true, this.siteType, this.siteKey, 'm3u8/', {});
  400. this.detailProxy = await js2Proxy(true, this.siteType, this.siteKey, 'detail/', this.getHeader());
  401. }
  402. async loadFilterAndClasses() {
  403. // 强制清空
  404. // await local.set(this.siteKey, "classes", JSON.stringify([]));
  405. // await local.set(this.siteKey, "filterObj", JSON.stringify({}));
  406. this.classes = await this.getClassesCache()
  407. this.filterObj = await this.getFiletObjCache()
  408. if (this.classes.length > 0) {
  409. return true
  410. } else {
  411. await local.set(this.siteKey, "classes", JSON.stringify([]));
  412. await local.set(this.siteKey, "filterObj", JSON.stringify({}));
  413. return false
  414. }
  415. }
  416. async writeFilterAndClasses() {
  417. if (this.catOpenStatus) {
  418. this.classes.push({"type_name": "最近更新", "type_id": "最近更新"})
  419. }
  420. await this.setClasses()
  421. await this.setFilterObj()
  422. await local.set(this.siteKey, "classes", JSON.stringify(this.classes));
  423. await local.set(this.siteKey, "filterObj", JSON.stringify(this.filterObj));
  424. }
  425. async getClassesCache() {
  426. let cacheClasses = await local.get(this.siteKey, "classes")
  427. if (!_.isEmpty(cacheClasses)) {
  428. return JSON.parse(cacheClasses)
  429. } else {
  430. return this.classes
  431. }
  432. }
  433. async getFiletObjCache() {
  434. let cacheFilterObj = await local.get(this.siteKey, "filterObj")
  435. if (!_.isEmpty(cacheFilterObj)) {
  436. return JSON.parse(cacheFilterObj)
  437. } else {
  438. return this.filterObj
  439. }
  440. }
  441. async setHome(filter) {
  442. }
  443. async home(filter) {
  444. this.vodList = []
  445. await this.jadeLog.info("正在解析首页类别", true)
  446. await this.setHome(filter)
  447. await this.jadeLog.debug(`首页类别内容为:${this.result.home(this.classes, [], this.filterObj)}`)
  448. await this.jadeLog.info("首页类别解析完成", true)
  449. return this.result.home(this.classes, [], this.filterObj)
  450. }
  451. async setHomeVod() {
  452. }
  453. async homeVod() {
  454. await this.jadeLog.info("正在解析首页内容", true)
  455. await this.setHomeVod()
  456. await this.jadeLog.debug(`首页内容为:${this.result.homeVod(this.homeVodList)}`)
  457. await this.jadeLog.info("首页内容解析完成", true)
  458. return this.result.homeVod(this.homeVodList)
  459. }
  460. async setCategory(tid, pg, filter, extend) {
  461. }
  462. async category(tid, pg, filter, extend) {
  463. this.page = parseInt(pg)
  464. await this.jadeLog.info(`正在解析分类页面,tid = ${tid},pg = ${pg},filter = ${filter},extend = ${JSON.stringify(extend)}`)
  465. if (tid === "最近更新") {
  466. this.page = 0
  467. return await this.homeVod()
  468. } else {
  469. try {
  470. this.vodList = []
  471. await this.setCategory(tid, pg, filter, extend)
  472. await this.jadeLog.debug(`分类页面内容为:${this.result.category(this.vodList, this.page, this.count, this.limit, this.total)}`)
  473. await this.jadeLog.info("分类页面解析完成", true)
  474. return this.result.category(this.vodList, this.page, this.count, this.limit, this.total)
  475. } catch (e) {
  476. await this.jadeLog.error(`分类页解析失败,失败原因为:${e}`)
  477. }
  478. }
  479. }
  480. async setDetail(id) {
  481. }
  482. setEpisodeCache() {
  483. // 记录每个播放链接的集数
  484. let episodeObj = {
  485. "vodDetail": this.vodDetail.to_dict(),
  486. }
  487. let vod_url_channels_list = this.vodDetail.vod_play_url.split("$$$")
  488. for (const vodItemsStr of vod_url_channels_list) {
  489. let vodItems = vodItemsStr.split("#")
  490. for (const vodItem of vodItems) {
  491. let episodeName = vodItem.split("$")[0].split(" ")[0]
  492. let episodeUrl = vodItem.split("$")[1]
  493. let matchers = episodeName.match(/\d+/g)
  494. if (matchers !== null && matchers.length > 0) {
  495. episodeName = matchers[0]
  496. }
  497. episodeObj[episodeUrl] = {"episodeName": episodeName, "episodeId": episodeName}
  498. }
  499. }
  500. return episodeObj
  501. }
  502. async detail(id) {
  503. this.vodDetail = new VodDetail();
  504. await this.jadeLog.info(`正在获取详情页面,id为:${id}`)
  505. try {
  506. await this.setDetail(id)
  507. await this.jadeLog.debug(`详情页面内容为:${this.result.detail(this.vodDetail)}`)
  508. await this.jadeLog.info("详情页面解析完成", true)
  509. this.vodDetail.vod_id = id
  510. if (this.siteType === 3) {
  511. this.episodeObj = this.setEpisodeCache()
  512. }
  513. return this.result.detail(this.vodDetail)
  514. } catch (e) {
  515. await this.jadeLog.error("详情界面获取失败,失败原因为:" + e)
  516. }
  517. }
  518. async setPlay(flag, id, flags) {
  519. this.playUrl = id
  520. }
  521. async setDanmu(id) {
  522. await this.jadeLog.debug(`${JSON.stringify(this.episodeObj)}`)
  523. let episodeId = this.episodeObj[id]
  524. let vodDetail = JSON.parse(this.episodeObj["vodDetail"])
  525. delete vodDetail.vod_content;
  526. delete vodDetail.vod_play_from;
  527. delete vodDetail.vod_play_url;
  528. delete vodDetail.vod_pic;
  529. await this.jadeLog.debug(`正在加载弹幕,视频详情为:${JSON.stringify(vodDetail)},集数:${JSON.stringify(this.episodeObj[id])}`)
  530. //区分电影还是电视剧
  531. return await this.danmuSpider.getDammu(vodDetail, episodeId)
  532. }
  533. async play(flag, id, flags) {
  534. await this.jadeLog.info(`正在解析播放页面,flag:${flag},id:${id},flags:${flags}`, true)
  535. try {
  536. let return_result;
  537. await this.setPlay(flag, id, flags)
  538. if (this.playUrl["content"] !== undefined) {
  539. return_result = this.result.playTxt(this.playUrl)
  540. } else {
  541. if (this.danmuStaus && !this.catOpenStatus) {
  542. if (!_.isEmpty(this.danmuUrl)) {
  543. await this.jadeLog.debug("播放详情页面有弹幕,所以不需要再查找弹幕")
  544. return_result = this.result.setHeader(this.header).danmu(this.danmuUrl).play(this.playUrl)
  545. } else {
  546. let danmuUrl;
  547. try {
  548. danmuUrl = await this.setDanmu(id)
  549. } catch (e) {
  550. await this.jadeLog.error(`弹幕加载失败,失败原因为:${e}`)
  551. }
  552. return_result = this.result.setHeader(this.header).danmu(danmuUrl).play(this.playUrl)
  553. }
  554. } else {
  555. await this.jadeLog.debug("不需要加载弹幕", true)
  556. return_result = this.result.setHeader(this.header).play(this.playUrl)
  557. }
  558. }
  559. await this.jadeLog.info("播放页面解析完成", true)
  560. await this.jadeLog.debug(`播放页面内容为:${return_result}`)
  561. return return_result;
  562. } catch (e) {
  563. await this.jadeLog.error("解析播放页面出错,失败原因为:" + e)
  564. }
  565. }
  566. async setSearch(wd, quick) {
  567. }
  568. async search(wd, quick) {
  569. this.vodList = []
  570. await this.jadeLog.info(`正在解析搜索页面,关键词为 = ${wd},quick = ${quick}`)
  571. await this.setSearch(wd, quick)
  572. if (this.vodList.length === 0) {
  573. if (wd.indexOf(" ") > -1) {
  574. await this.jadeLog.debug(`搜索关键词为:${wd},其中有空格,去除空格在搜索一次`)
  575. await this.search(wd.replaceAll(" ", "").replaceAll("", ""), quick)
  576. }
  577. }
  578. await this.jadeLog.debug(`搜索页面内容为:${this.result.search(this.vodList)}`)
  579. await this.jadeLog.info("搜索页面解析完成", true)
  580. return this.result.search(this.vodList)
  581. }
  582. async getImg(url, headers) {
  583. let resp;
  584. if (_.isEmpty(headers)) {
  585. headers = {Referer: url, 'User-Agent': Utils.CHROME}
  586. }
  587. resp = await req(url, {buffer: 2, headers: headers});
  588. try {
  589. Utils.base64Decode(resp.content)
  590. await this.jadeLog.error(`图片代理获取失败,重连失败`, true)
  591. this.reconnectTimes = 0
  592. return {"code": 500, "headers": headers, "content": "加载失败"}
  593. } catch (e) {
  594. await this.jadeLog.debug("图片代理成功", true)
  595. this.reconnectTimes = 0
  596. return resp
  597. }
  598. }
  599. async proxy(segments, headers) {
  600. await this.jadeLog.debug(`正在设置反向代理 segments = ${segments.join(",")},headers = ${JSON.stringify(headers)}`)
  601. let what = segments[0];
  602. let url = Utils.base64Decode(segments[1]);
  603. await this.jadeLog.debug(`反向代理参数为:${url}`)
  604. if (what === 'img') {
  605. await this.jadeLog.debug("通过代理获取图片", true)
  606. let resp = await this.getImg(url, headers)
  607. return JSON.stringify({
  608. code: resp.code, buffer: 2, content: resp.content, headers: resp.headers,
  609. });
  610. } else if (what === "douban") {
  611. let vod_list = await this.doubanSearch(url)
  612. if (vod_list !== null) {
  613. let vod_pic = vod_list[0].vod_pic
  614. let resp;
  615. if (!_.isEmpty(headers)) {
  616. resp = await req(vod_pic, {
  617. buffer: 2, headers: headers
  618. });
  619. } else {
  620. resp = await req(vod_pic, {
  621. buffer: 2, headers: {
  622. Referer: vod_pic, 'User-Agent': Utils.CHROME,
  623. },
  624. });
  625. }
  626. return JSON.stringify({
  627. code: resp.code, buffer: 2, content: resp.content, headers: resp.headers,
  628. });
  629. }
  630. } else if (what === "m3u8") {
  631. let content;
  632. if (!_.isEmpty(headers)) {
  633. content = await this.fetch(url, null, headers, false, false, 2)
  634. } else {
  635. content = await this.fetch(url, null, {"Referer": url, 'User-Agent': Utils.CHROME}, false, false, 2)
  636. }
  637. await this.jadeLog.debug(`m3u8返回内容为:${Utils.base64Decode(content)}`)
  638. if (!_.isEmpty(content)) {
  639. return JSON.stringify({
  640. code: 200, buffer: 2, content: content, headers: {},
  641. });
  642. } else {
  643. return JSON.stringify({
  644. code: 500, buffer: 2, content: content, headers: {},
  645. })
  646. }
  647. } else if (what === 'hls') {
  648. function hlsHeader(data, hls) {
  649. let hlsHeaders = {};
  650. if (data.headers['content-length']) {
  651. Object.assign(hlsHeaders, data.headers, {'content-length': hls.length.toString()});
  652. } else {
  653. Object.assign(hlsHeaders, data.headers);
  654. }
  655. delete hlsHeaders['transfer-encoding'];
  656. if (hlsHeaders['content-encoding'] == 'gzip') {
  657. delete hlsHeaders['content-encoding'];
  658. }
  659. return hlsHeaders;
  660. }
  661. const hlsData = await hlsCache(url, headers);
  662. if (hlsData.variants) {
  663. // variants -> variants -> .... ignore
  664. const hls = HLS.stringify(hlsData.plist);
  665. return {
  666. code: hlsData.code, content: hls, headers: hlsHeader(hlsData, hls),
  667. };
  668. } else {
  669. const hls = HLS.stringify(hlsData.plist, (segment) => {
  670. return js2Proxy(false, this.siteType, this.siteKey, 'ts/' + encodeURIComponent(hlsData.key + '/' + segment.mediaSequenceNumber.toString()), headers);
  671. });
  672. return {
  673. code: hlsData.code, content: hls, headers: hlsHeader(hlsData, hls),
  674. };
  675. }
  676. } else if (what === 'ts') {
  677. const info = url.split('/');
  678. const hlsKey = info[0];
  679. const segIdx = parseInt(info[1]);
  680. return await tsCache(hlsKey, segIdx, headers);
  681. } else if (what === "detail") {
  682. let $ = await this.getHtml(this.siteUrl + url)
  683. let vodDetail = await this.parseVodDetailFromDoc($)
  684. let resp = await this.getImg(vodDetail.vod_pic, headers)
  685. return JSON.stringify({
  686. code: resp.code, buffer: 2, content: resp.content, headers: resp.headers,
  687. });
  688. } else {
  689. return JSON.stringify({
  690. code: 500, content: '',
  691. });
  692. }
  693. }
  694. getSearchHeader() {
  695. const UserAgents = ["api-client/1 com.douban.frodo/7.22.0.beta9(231) Android/23 product/Mate 40 vendor/HUAWEI model/Mate 40 brand/HUAWEI rom/android network/wifi platform/AndroidPad", "api-client/1 com.douban.frodo/7.18.0(230) Android/22 product/MI 9 vendor/Xiaomi model/MI 9 brand/Android rom/miui6 network/wifi platform/mobile nd/1", "api-client/1 com.douban.frodo/7.1.0(205) Android/29 product/perseus vendor/Xiaomi model/Mi MIX 3 rom/miui6 network/wifi platform/mobile nd/1", "api-client/1 com.douban.frodo/7.3.0(207) Android/22 product/MI 9 vendor/Xiaomi model/MI 9 brand/Android rom/miui6 network/wifi platform/mobile nd/1"]
  696. let randomNumber = Math.floor(Math.random() * UserAgents.length); // 生成一个介于0到9之间的随机整数
  697. return {
  698. 'User-Agent': UserAgents[randomNumber]
  699. }
  700. }
  701. async parseDoubanVodShortListFromJson(obj) {
  702. let vod_list = []
  703. for (const item of obj) {
  704. let vod_short = new VodShort()
  705. vod_short.vod_id = "msearch:" + item["id"]
  706. if (item["title"] === undefined) {
  707. vod_short.vod_name = item["target"]["title"]
  708. } else {
  709. vod_short.vod_name = item["title"]
  710. }
  711. if (item["pic"] === undefined) {
  712. vod_short.vod_pic = item["target"]["cover_url"]
  713. } else {
  714. vod_short.vod_pic = item["pic"]["normal"]
  715. }
  716. if (item["rating"] === undefined) {
  717. vod_short.vod_remarks = "评分:" + item["target"]["rating"]["value"].toString()
  718. } else {
  719. vod_short.vod_remarks = "评分:" + item["rating"]["value"].toString()
  720. }
  721. vod_list.push(vod_short);
  722. }
  723. return vod_list
  724. }
  725. sign(url, ts, method = 'GET') {
  726. let _api_secret_key = "bf7dddc7c9cfe6f7"
  727. let url_path = "%2F" + url.split("/").slice(3).join("%2F")
  728. let raw_sign = [method.toLocaleUpperCase(), url_path, ts.toString()].join("&")
  729. return CryptoJS.HmacSHA1(raw_sign, _api_secret_key).toString(CryptoJS.enc.Base64)
  730. }
  731. async doubanSearch(wd) {
  732. try {
  733. let _api_url = "https://frodo.douban.com/api/v2"
  734. let _api_key = "0dad551ec0f84ed02907ff5c42e8ec70"
  735. let url = _api_url + "/search/movie"
  736. let date = new Date()
  737. let ts = date.getFullYear().toString() + (date.getMonth() + 1).toString() + date.getDate().toString()
  738. let params = {
  739. '_sig': this.sign(url, ts),
  740. '_ts': ts,
  741. 'apiKey': _api_key,
  742. 'count': 20,
  743. 'os_rom': 'android',
  744. 'q': encodeURIComponent(wd),
  745. 'start': 0
  746. }
  747. let content = await this.fetch(url, params, this.getSearchHeader())
  748. if (!_.isEmpty(content)) {
  749. let content_json = JSON.parse(content)
  750. await this.jadeLog.debug(`豆瓣搜索结果:${content}`)
  751. return await this.parseDoubanVodShortListFromJson(content_json["items"])
  752. }
  753. return null
  754. } catch (e) {
  755. await this.jadeLog.error("反向代理出错,失败原因为:" + e)
  756. }
  757. }
  758. }
  759. export {Spider, Result}