jable.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * @File : jable.js
  3. * @Author : jade
  4. * @Date : 2024/3/4 9:44
  5. * @Email : jadehh@1ive.com
  6. * @Software : Samples
  7. * @Desc :
  8. */
  9. import {_, load} from '../lib/cat.js';
  10. import {VodDetail, VodShort} from "../lib/vod.js"
  11. import * as Utils from "../lib/utils.js";
  12. import {Spider} from "./spider.js";
  13. class JableTVSpider extends Spider {
  14. constructor() {
  15. super();
  16. this.siteUrl = "https://jable.tv"
  17. this.cookie = ""
  18. }
  19. async spiderInit(inReq = null) {
  20. if (inReq !== null) {
  21. this.jsBase = await js2Proxy(inReq, "img", this.getImgHeaders());
  22. } else {
  23. this.jsBase = await js2Proxy(true, this.siteType, this.siteKey, 'img/', this.getImgHeaders());
  24. }
  25. }
  26. getImgHeaders(){
  27. return {
  28. "User-Agent": "PostmanRuntime/7.37.3",
  29. "Postman-Token": "c2602692-1a05-4bb0-93cd-270afad97e87",
  30. "Host": "assets-cdn.jable.tv",
  31. "Proxy": true
  32. }
  33. }
  34. async init(cfg) {
  35. await super.init(cfg);
  36. await this.spiderInit(null)
  37. }
  38. getAppName() {
  39. return "Jable"
  40. }
  41. getName() {
  42. return "🔞┃Jable┃🔞"
  43. }
  44. getJSName() {
  45. return "jable"
  46. }
  47. getType() {
  48. return 3
  49. }
  50. getHeader() {
  51. // let header = super.getHeader()
  52. let header = {}
  53. header["User-Agent"] = "PostmanRuntime/7.36.3"
  54. header["Host"] = "jable.tv"
  55. header["Postman-Token"] = "33290483-3c8d-413f-a160-0d3aea9e6f95"
  56. return header
  57. }
  58. async getHtml(url = this.siteUrl, proxy = false, headers = this.getHeader()) {
  59. return super.getHtml(url, true, headers);
  60. }
  61. async setClasses() {
  62. let $ = await this.getHtml(this.siteUrl)
  63. let navElements = $("[class=\"title-box\"]")
  64. let defaultTypeIdElements = $("div.row")
  65. for (const navElement of $(defaultTypeIdElements[0]).find("a")) {
  66. let type_name = $(navElement).text()
  67. let type_id = navElement.attribs.href
  68. if (type_id.indexOf(this.siteUrl) > -1) {
  69. this.classes.push(this.getTypeDic(type_name, type_id))
  70. }
  71. }
  72. navElements = navElements.slice(1, 9)
  73. defaultTypeIdElements = defaultTypeIdElements.slice(1, 9)
  74. for (let i = 0; i < navElements.length; i++) {
  75. let typeId = $(defaultTypeIdElements[i]).find("a")[0].attribs["href"]
  76. this.classes.push(this.getTypeDic("标签", typeId));
  77. break
  78. }
  79. }
  80. async getSortFilter($) {
  81. let sortElements = $("[class=\"sorting-nav\"]").find("a")
  82. let extend_dic = {"name": "排序", "key": "sort", "value": []}
  83. for (const sortElement of sortElements) {
  84. let typeId = sortElement.attribs["data-parameters"].split("sort_by:")[1]
  85. let typeName = $(sortElement).text()
  86. extend_dic["value"].push({"n": typeName, "v": typeId})
  87. }
  88. return extend_dic
  89. }
  90. async getFilter($, index, type_id, type_name) {
  91. let extend_list = []
  92. if (index < 4) {
  93. let extend_dic = {"name": type_name, "key": "type", "value": []}
  94. let type_seletc_list = ["div.img-box > a", "[class=\"horizontal-img-box ml-3 mb-3\"] > a", "", "sort"]
  95. let type_id_select_list = ["div.absolute-center > h4", "div.detail"]
  96. let default$ = await this.getHtml(type_id)
  97. for (const element of default$(type_seletc_list[index])) {
  98. let typeId = element.attribs["href"]
  99. let typeName = $($(element).find(type_id_select_list[index])).text().replaceAll("\t", "").replaceAll("\n", '').replaceAll(" ", "");
  100. extend_dic["value"].push({"n": typeName, "v": typeId})
  101. }
  102. if (extend_dic.value.length > 0) {
  103. extend_list.push(extend_dic)
  104. //排序
  105. let sortDetail$ = await this.getHtml(extend_dic["value"][0]["v"])
  106. let sort_extend_dic = await this.getSortFilter(sortDetail$)
  107. if (sort_extend_dic.value.length > 0) {
  108. extend_list.push(sort_extend_dic)
  109. }
  110. } else {
  111. //排序
  112. let sort_extend_dic = await this.getSortFilter(default$)
  113. if (sort_extend_dic.value.length > 0) {
  114. extend_list.push(sort_extend_dic)
  115. }
  116. }
  117. } else {
  118. let defaultTypeIdElements = $("div.row").slice(1, 9)
  119. let navElements = $("[class=\"title-box\"]").slice(1, 9)
  120. for (let i = 0; i < navElements.length; i++) {
  121. let extend_dic = {"name": $($(navElements[i]).find("h2")).text(), "key": "type", "value": []}
  122. for (const filterElement of $(defaultTypeIdElements[i]).find("a")) {
  123. let filter_type_id = filterElement.attribs.href
  124. if (filter_type_id.indexOf(this.siteUrl) > -1) {
  125. extend_dic["value"].push({"n": $(filterElement).text(), "v": filter_type_id})
  126. }
  127. }
  128. extend_list.push(extend_dic)
  129. }
  130. let sortDetail$ = await this.getHtml(type_id)
  131. let sort_extend_dic = await this.getSortFilter(sortDetail$)
  132. if (sort_extend_dic.value.length > 0) {
  133. extend_list.push(sort_extend_dic)
  134. }
  135. }
  136. return extend_list
  137. }
  138. async setFilterObj() {
  139. let $ = await this.getHtml(this.siteUrl)
  140. let classes = this.classes.slice(1)
  141. for (let i = 0; i < classes.length; i++) {
  142. let type_name = classes[i].type_name
  143. let type_id = classes[i].type_id
  144. // if (type_id.indexOf("models") > 1) {
  145. // type_id = `https://jable.tv/models/?mode=async&function=get_block&block_id=list_models_models_list&sort_by=total_videos&_=${new Date().getTime()}`
  146. // }
  147. let extend_list = await this.getFilter($, i, type_id, type_name)
  148. if (extend_list.length > 1 && i < 4) {
  149. type_id = extend_list[0]["value"][0]["v"]
  150. this.classes[i + 1] = this.getTypeDic(type_name, type_id)
  151. }
  152. this.filterObj[type_id] = extend_list
  153. }
  154. }
  155. async parseVodShortListFromDoc($) {
  156. let vod_list = []
  157. let vodElements = $("div.video-img-box")
  158. for (const element of vodElements) {
  159. let vodShort = new VodShort()
  160. let vod_pic = $(element).find("img").attr("data-src")
  161. if (vod_pic !== undefined) {
  162. vodShort.vod_pic = vod_pic
  163. // if (this.catOpenStatus) {
  164. // vodShort.vod_pic = this.jsBase + Utils.base64Encode(vod_pic)
  165. // } else {
  166. // vodShort.vod_pic = vod_pic
  167. // }
  168. let url = $(element).find("a").attr("href");
  169. vodShort.vod_id = url.split("/")[4];
  170. vodShort.vod_name = url.split("/")[4];
  171. let remarks_list = $($(element).find("[class=\"sub-title\"]")).text().split("\n")
  172. if (remarks_list.length > 1) {
  173. vodShort.vod_remarks = remarks_list[1].replaceAll(" ", "").replaceAll("\t", "")
  174. } else {
  175. vodShort.vod_remarks = "精选"
  176. }
  177. if (!_.isEmpty(vodShort.vod_pic) && vodShort.vod_remarks !== "[限時優惠]只需1元即可無限下載") {
  178. vod_list.push(vodShort);
  179. }
  180. }
  181. }
  182. return vod_list
  183. }
  184. async parseVodDetailFromDoc($) {
  185. let vodDetail = new VodDetail();
  186. let leftElement = $("[class=\"header-left\"]")
  187. vodDetail.vod_name = $($(leftElement).find("h4")).text();
  188. let vod_pic = Utils.getStrByRegex(/<video poster="(.*?)" id=/, $.html())
  189. vodDetail.vod_pic = vod_pic
  190. // if (this.catOpenStatus) {
  191. // vodDetail.vod_pic = this.jsBase + Utils.base64Encode(vod_pic)
  192. // } else {
  193. // vodDetail.vod_pic = vod_pic
  194. // }
  195. vodDetail.vod_year = $($("[class=\"inactive-color\"]")).text()
  196. let episodeName = $($("[class=\"header-right d-none d-md-block\"] > h6")).text().replaceAll("\n", "").replaceAll("●", "")
  197. let vodItems = []
  198. let episodeUrl = Utils.getStrByRegex(/var hlsUrl = '(.*?)';/, $.html())
  199. vodItems.push(episodeName + "$" + episodeUrl)
  200. let vod_play_list = []
  201. vod_play_list.push(vodItems.join("#"))
  202. let vod_play_from_list = ["Jable"]
  203. vodDetail.vod_play_from = vod_play_from_list.join("$$$")
  204. vodDetail.vod_play_url = vod_play_list.join("$$$")
  205. return vodDetail
  206. }
  207. async setHomeVod() {
  208. let $ = await this.getHtml(this.siteUrl)
  209. this.homeVodList = await this.parseVodShortListFromDoc($)
  210. }
  211. async setDetail(id) {
  212. let $ = await this.getHtml(this.siteUrl + "/videos/" + id + "/")
  213. this.vodDetail = await this.parseVodDetailFromDoc($)
  214. }
  215. async setCategory(tid, pg, filter, extend) {
  216. let extend_type = extend["type"] ?? tid
  217. let sort_by = extend["sort"] ?? "video_viewed"
  218. this.limit = 24
  219. let cateUrl;
  220. this.total = 0
  221. this.count = 0
  222. if (tid.indexOf("latest-updates") > 1) {
  223. cateUrl = `https://jable.tv/latest-updates/?mode=async&function=get_block&block_id=list_videos_latest_videos_list&sort_by=post_date&from=${pg}&_=1709730132217`
  224. } else {
  225. cateUrl = extend_type + `/${pg}/?mode=async&function=get_block&block_id=list_videos_common_videos_list&sort_by=${sort_by}&_=${new Date().getTime()}`
  226. }
  227. let $ = await this.getHtml(cateUrl);
  228. this.vodList = await this.parseVodShortListFromDoc($)
  229. let page = $($("[class=\"page-item\"]").slice(-1)[0]).text()
  230. if (page.indexOf("最後") > -1) {
  231. } else {
  232. if (parseInt(page) === this.page || _.isEmpty(page)) {
  233. await this.jadeLog.debug("分类页面到底了")
  234. this.total = this.page
  235. this.count = this.page
  236. }
  237. }
  238. }
  239. async setSearch(wd, quick) {
  240. let searchUrl = this.siteUrl + `/search/${wd}/`
  241. let $ = await this.getHtml(searchUrl)
  242. this.vodList = await this.parseVodShortListFromDocByCategory($)
  243. }
  244. }
  245. let spider = new JableTVSpider()
  246. async function init(cfg) {
  247. await spider.init(cfg)
  248. }
  249. async function home(filter) {
  250. return await spider.home(filter)
  251. }
  252. async function homeVod() {
  253. return await spider.homeVod()
  254. }
  255. async function category(tid, pg, filter, extend) {
  256. return await spider.category(tid, pg, filter, extend)
  257. }
  258. async function detail(id) {
  259. return await spider.detail(id)
  260. }
  261. async function play(flag, id, flags) {
  262. return await spider.play(flag, id, flags)
  263. }
  264. async function search(wd, quick) {
  265. return await spider.search(wd, quick)
  266. }
  267. async function proxy(segments, headers) {
  268. return await spider.proxy(segments, headers)
  269. }
  270. export function __jsEvalReturn() {
  271. return {
  272. init: init,
  273. home: home,
  274. homeVod: homeVod,
  275. category: category,
  276. detail: detail,
  277. play: play,
  278. search: search,
  279. proxy: proxy
  280. };
  281. }
  282. export {spider}