gitcafe.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * @File : gitcafe.js
  3. * @Author : jade
  4. * @Date : 2024/1/18 9:56
  5. * @Email : jadehh@1ive.com
  6. * @Software : Samples
  7. * @Desc : 阿里纸条
  8. */
  9. import {_, load} from "../lib/cat.js";
  10. import {Spider} from "./spider.js";
  11. import { detailContent,initCloud,playContent,getHeaders} from "../lib/cloud.js";
  12. import * as Utils from "../lib/utils.js";
  13. import {VodDetail, VodShort} from "../lib/vod.js";
  14. class GitCafeSpider extends Spider {
  15. constructor() {
  16. super();
  17. this.siteUrl = "https://ali.gitcafe.ink"
  18. }
  19. getName() {
  20. return "🦊┃阿里纸条┃🦊"
  21. }
  22. getAppName() {
  23. return "阿里纸条"
  24. }
  25. getJSName() {
  26. return "gitcafe"
  27. }
  28. getType() {
  29. return 3
  30. }
  31. async getApiUrl() {
  32. let html = await this.fetch(this.siteUrl, null, this.getHeader())
  33. if (!_.isEmpty(html)) {
  34. let paper_js_url = Utils.getStrByRegex(/<script src='(.*?)'><\/script>/, html)
  35. let paper_js_content = await this.fetch(paper_js_url, null, this.getHeader())
  36. return {
  37. "api": "https:" + Utils.getStrByRegex(/ return '(.*?)' \+ /, paper_js_content) + new Date().getTime(),
  38. "search_api": Utils.getStrByRegex(/const SEARCH_API = '(.*?)';/, paper_js_content)
  39. }
  40. }
  41. }
  42. async getContentJson() {
  43. let url_json = await this.getApiUrl()
  44. let content = await this.fetch(url_json["api"], null, this.getHeader())
  45. this.search_api = url_json["search_api"]
  46. if (!_.isEmpty(content)) {
  47. return JSON.parse(content)
  48. }
  49. }
  50. async spiderInit() {
  51. this.content_json = await this.getContentJson()
  52. this.token_dic = await this.load_cache()
  53. }
  54. async init(cfg) {
  55. await this.spiderInit()
  56. await super.init(cfg);
  57. await initCloud(this.cfgObj);
  58. }
  59. async parseClassFromJson(obj) {
  60. let data_list = Object.keys(obj["data"]).slice(0, 19)
  61. for (const data_key of data_list) {
  62. this.classes.push({"type_name": obj["data"][data_key]["name"], "type_id": data_key})
  63. }
  64. }
  65. async parseVodShortListFromJson(obj) {
  66. let vod_list = []
  67. let class_id_list = this.getClassIdList()
  68. for (const data_obj of obj) {
  69. let vodShort = new VodShort()
  70. if (class_id_list.includes(data_obj["cat"])) {
  71. vodShort.vod_id = JSON.stringify(data_obj)
  72. vodShort.vod_name = data_obj["title"]
  73. vodShort.vod_remarks = data_obj["date"]
  74. vod_list.push(vodShort)
  75. }
  76. }
  77. return vod_list
  78. }
  79. async parseVodDetailfromJson(obj) {
  80. let classNamesList = this.getClassNameList()
  81. let classIdList = this.getClassIdList()
  82. let vodDetail = new VodDetail()
  83. vodDetail.vod_name = obj["title"]
  84. vodDetail.vod_remarks = obj["creatime"] ?? obj["date"]
  85. vodDetail.type_name = classNamesList[classIdList.indexOf(obj["cat"])]
  86. vodDetail.vod_content = obj["des"]
  87. let ali_url = "https://www.aliyundrive.com/s/" + obj["alikey"]
  88. let playVod = await detailContent([ali_url],vodDetail.type_name)
  89. vodDetail.vod_play_from = _.keys(playVod).join('$$$');
  90. vodDetail.vod_play_url = _.values(playVod).join('$$$');
  91. return vodDetail
  92. }
  93. async setClasses() {
  94. await this.parseClassFromJson(this.content_json)
  95. }
  96. async setHomeVod() {
  97. this.homeVodList = await this.parseVodShortListFromJson(this.content_json["info"]["new"])
  98. }
  99. async setDetail(id) {
  100. let content_json = JSON.parse(id)
  101. this.vodDetail = await this.parseVodDetailfromJson(content_json)
  102. }
  103. async setCategory(tid, pg, filter, extend) {
  104. this.vodList = await this.parseVodShortListFromJson(this.content_json["data"][tid]["catdata"])
  105. }
  106. async setSearch(wd, quick) {
  107. await this.refreshToken();
  108. let params = {
  109. "action": "search", "from": "web", "token": this.token_dic["token"], "keyword": wd
  110. }
  111. let content = await this.post(this.search_api, params, this.getHeader())
  112. if (!_.isEmpty(content)) {
  113. let content_json = JSON.parse(content)
  114. this.vodList = await this.parseVodShortListFromJson(content_json["data"])
  115. }
  116. }
  117. async refreshToken() {
  118. let this_time = new Date().getTime()
  119. if (_.isEmpty(this.token_dic["token"])) {
  120. await this.get_token()
  121. await this.jadeLog.debug("Token为空,刷新Token")
  122. } else if (this_time - parseInt(this.token_dic["date"]) > 24 * 60 * 60 * 1000) {
  123. await this.jadeLog.debug(`Token到期,上次获取Token时间为:${this_time},当前时间为:${parseInt(this.token_dic["date"])},刷新Token`)
  124. await this.get_token()
  125. } else {
  126. await this.jadeLog.debug(`Token仍然有效,无需刷新`, true)
  127. }
  128. }
  129. async get_token() {
  130. try {
  131. let params = {
  132. "action": "get_token", "from": "web",
  133. }
  134. let content = await this.post(this.search_api, params, this.getHeader())
  135. if (!_.isEmpty(content)) {
  136. let content_json = JSON.parse(content)
  137. let this_time = new Date().getTime()
  138. this.token_dic["token"] = content_json["data"]
  139. this.token_dic["date"] = this_time.toString()
  140. await this.write_cache()
  141. }
  142. } catch (e) {
  143. await this.jadeLog.error("获取Token失败,失败原因为:" + e)
  144. }
  145. }
  146. async write_cache() {
  147. await local.set("gitcafe_token", "token", JSON.stringify(this.token_dic))
  148. }
  149. async load_cache() {
  150. try {
  151. let str = await local.get("gitcafe_token", "token")
  152. return JSON.parse(str)
  153. } catch (e) {
  154. return {"token": "", "date": ""}
  155. }
  156. }
  157. async setPlay(flag, id, flags) {
  158. this.playUrl = await playContent(flag, id, flags);
  159. this.result.setHeader(getHeaders(flag))
  160. }
  161. }
  162. let spider = new GitCafeSpider()
  163. async function init(cfg) {
  164. await spider.init(cfg)
  165. }
  166. async function home(filter) {
  167. return await spider.home(filter)
  168. }
  169. async function homeVod() {
  170. return await spider.homeVod()
  171. }
  172. async function category(tid, pg, filter, extend) {
  173. return await spider.category(tid, pg, filter, extend)
  174. }
  175. async function detail(id) {
  176. return await spider.detail(id)
  177. }
  178. async function play(flag, id, flags) {
  179. return await spider.play(flag, id, flags)
  180. }
  181. async function search(wd, quick) {
  182. return await spider.search(wd, quick)
  183. }
  184. export function __jsEvalReturn() {
  185. return {
  186. init: init, home: home, homeVod: homeVod, category: category, detail: detail, play: play, search: search,
  187. };
  188. }
  189. export {spider}