ttkan.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * @File : ttkan.js
  3. * @Author : jade
  4. * @Date : 2024/5/10 9:59
  5. * @Email : jadehh@1ive.com
  6. * @Software : Samples
  7. * @Desc :
  8. */
  9. import {_, load} from '../lib/cat.js';
  10. import * as Utils from "../lib/utils.js";
  11. import {Spider} from "./spider.js";
  12. import {BookDetail, BookShort} from "../lib/book.js";
  13. class TTKanSpider extends Spider {
  14. constructor() {
  15. super();
  16. this.siteUrl = "https://cn.ttkan.co"
  17. this.apiUrl = "https://cn.ttkan.co/api"
  18. }
  19. getAppName() {
  20. return "天天看小说"
  21. }
  22. getJSName() {
  23. return "ttkan"
  24. }
  25. getType() {
  26. return 10
  27. }
  28. getName() {
  29. return "📚︎┃天天看小说┃📚︎"
  30. }
  31. async spiderInit(inReq = null) {
  32. if (inReq !== null) {
  33. this.jsBase = await js2Proxy(inReq, "img", this.getHeader());
  34. } else {
  35. this.jsBase = await js2Proxy(true, this.siteType, this.siteKey, 'img/', this.getHeader());
  36. }
  37. }
  38. async init(cfg) {
  39. await super.init(cfg);
  40. await this.spiderInit(null)
  41. }
  42. parseVodShortFromElement($, element) {
  43. let bookShort = new BookShort()
  44. let bookShortElements = $(element).find("a")
  45. bookShort.book_name = bookShortElements[0].attribs["aria-label"]
  46. bookShort.book_id = bookShortElements[0].attribs.href
  47. if ($(element).find("amp-img").length > 0) {
  48. bookShort.book_pic = $(element).find("amp-img")[0].attribs["src"].split("?")[0]
  49. }
  50. bookShort.book_remarks = $($(element).find("p")[0]).text()
  51. return bookShort
  52. }
  53. async parseVodShortListFromDoc($) {
  54. let books = []
  55. let bookElements = $("[class=\"frame_body\"]").find("[class=\"pure-g\"]").slice(-1)[0]
  56. for (const bookElement of bookElements.children) {
  57. let bookShort = this.parseVodShortFromElement($, $(bookElement).find("li")[0])
  58. books.push(bookShort)
  59. }
  60. return books
  61. }
  62. async parseVodShortListFromDocByCategory($) {
  63. let bookImgElements = $("[class=\"pure-u-xl-1-5 pure-u-lg-1-4 pure-u-md-1-3 pure-u-sm-1-3 pure-u-13-24\"]")
  64. let bookMsgElements = $("[class=\"pure-u-xl-4-5 pure-u-lg-3-4 pure-u-md-2-3 pure-u-sm-2-3 pure-u-11-24\"]")
  65. let books = [];
  66. for (let i = 0; i < bookImgElements.length; i++) {
  67. let bookShort = new BookShort()
  68. let imgElement = bookImgElements[i]
  69. let msgElement = bookMsgElements[i]
  70. let element = $(imgElement).find('a')[0]
  71. bookShort.book_id = element.attribs.href;
  72. const img = $(imgElement).find('amp-img')[0];
  73. bookShort.book_name = $(element).text()
  74. bookShort.book_pic = img.attribs["src"].split("?")[0]
  75. bookShort.book_name = img.attribs["alt"]
  76. bookShort.book_remarks = $($(msgElement).find('li').slice(-1)).text().replaceAll("状态:","");
  77. books.push(bookShort)
  78. }
  79. return books
  80. }
  81. async parseVodShortListFromJson(obj) {
  82. let books = [];
  83. for (const data of obj) {
  84. let bookShort = new BookShort()
  85. bookShort.book_id = "/novel/chapters/" + data["novel_id"]
  86. bookShort.book_name = data["name"]
  87. bookShort.book_remarks = "作者:" + data.author
  88. bookShort.book_pic = "https://static.ttkan.co/cover/" + data["topic_img"]
  89. books.push(bookShort)
  90. }
  91. return books
  92. }
  93. async parseVodShortListFromDocBySearch($) {
  94. let books = []
  95. let bookElements = $("[class=\"frame_body\"]").find("[class=\"pure-g\"]").slice(-1)[0]
  96. for (const bookElement of bookElements.children) {
  97. let bookShort = new BookShort()
  98. bookShort.book_id = $(bookElement).find("a")[0].attribs.href
  99. bookShort.book_name = $($(bookElement).find("li")[0]).text()
  100. bookShort.book_remarks = $($(bookElement).find("li")[1]).text()
  101. bookShort.book_pic = $(bookElement).find("amp-img")[0].attribs.src.split("?")[0]
  102. books.push(bookShort)
  103. }
  104. return books
  105. }
  106. async parseVodDetailFromDoc($, id) {
  107. let html = $.html()
  108. let bookDetail = new BookDetail()
  109. let infoElement = $("[class=\"pure-g novel_info\"]")
  110. bookDetail.book_pic = $(infoElement).find("amp-img")[0].attribs.src.split("?")[0]
  111. let elements = $(infoElement).find("[class=\"pure-u-xl-5-6 pure-u-lg-5-6 pure-u-md-2-3 pure-u-1-2\"]").find("li")
  112. bookDetail.book_name = $(elements[0]).text()
  113. bookDetail.book_director = $(elements[1]).text().replaceAll("作者:","")
  114. bookDetail.book_remarks = $(elements[3]).text().replaceAll("状态:","")
  115. bookDetail.book_year= $("[class=\"near_chapter\"]").find("time")[0].attribs.datetime.replaceAll("T"," ").split(".")[0]
  116. bookDetail.book_content = $($('[class="description"]')).text().trim()
  117. bookDetail.book_id = id
  118. const playBook = {};
  119. const nearElement = $('[class="near_chapter"]').find("a")[0]
  120. let nearVodItems = []
  121. const epName = $(nearElement).text();
  122. const page = nearElement.attribs.href.split("&page=").slice(-1)[0]
  123. const playUrl = epName + "+" + `${id.replaceAll("/novel/chapters/","")}_${page}.html` ;
  124. nearVodItems.push(epName + '$' + playUrl)
  125. const lastestElements = $('[class="chapters_frame"]').find("a")
  126. for (const lastestElement of lastestElements){
  127. const epName = $(lastestElement).text();
  128. const page = lastestElement.attribs.href.split("&page=").slice(-1)[0]
  129. const playUrl = epName + "+" + `${id.replaceAll("/novel/chapters/","")}_${page}.html` ;
  130. nearVodItems.push(epName + '$' + playUrl)
  131. }
  132. playBook["最近章节"] = nearVodItems.reverse().join("#")
  133. let params = {"language":"cn","novel_id":id.replaceAll("/novel/chapters/",""),"__amp_source_origin":encodeURIComponent(this.siteUrl)}
  134. let resJSon = JSON.parse(await this.fetch(this.apiUrl + "/nq/amp_novel_chapters" ,params,this.getHeader()))
  135. let allVodItems = []
  136. for (const data of resJSon["items"]){
  137. const epName = data.chapter_name;
  138. const playUrl = epName + "+" + `${id.replaceAll("/novel/chapters/","")}_${data.chapter_id}.html` ;
  139. allVodItems.push(epName + '$' + playUrl)
  140. }
  141. playBook["目录"] = allVodItems.join("#")
  142. bookDetail.volumes = _.keys(playBook).join('$$$');
  143. bookDetail.urls = _.values(playBook).join('$$$');
  144. return bookDetail
  145. }
  146. async setClasses() {
  147. let $ = await this.getHtml()
  148. let navElements = $('div.novel_class_nav > a')
  149. for (const element of navElements) {
  150. let type_id = element.attribs.href
  151. let type_name = $(element).text()
  152. this.classes.push(this.getTypeDic(type_name, type_id));
  153. }
  154. }
  155. async getFilter($) {
  156. let extend_list = []
  157. let extend_dic = {"name": "排序", "key": "sort", "value": []}
  158. let elements = $('div.rank_nav > a')
  159. let sortList = []
  160. let isNewSort = false
  161. if (elements.length === 0) {
  162. elements = $('div.nav_filter_inner > a')
  163. isNewSort = true
  164. }
  165. for (const element of elements) {
  166. let type_name = $(element).text()
  167. let type_id = element.attribs.href
  168. sortList.push(this.getFliterDic(type_name, type_id))
  169. }
  170. if (isNewSort) {
  171. const lastItem = sortList.splice(-1, 1);
  172. sortList = lastItem.concat(sortList);
  173. }
  174. extend_dic["value"] = sortList
  175. extend_list.push(extend_dic)
  176. return extend_list
  177. }
  178. async setFilterObj() {
  179. for (const type_dic of this.classes) {
  180. let type_id = type_dic["type_id"]
  181. if (type_id !== "最近更新") {
  182. let $ = await this.getHtml(this.siteUrl + type_id)
  183. this.filterObj[type_id] = await this.getFilter($)
  184. }
  185. }
  186. }
  187. async setHomeVod() {
  188. let $ = await this.getHtml()
  189. this.homeVodList = await this.parseVodShortListFromDoc($)
  190. }
  191. async setDetail(id) {
  192. let $ = await this.getHtml(this.siteUrl + id)
  193. this.vodDetail = await this.parseVodDetailFromDoc($, id)
  194. }
  195. getExtend(extend) {
  196. if (extend["sort"] === undefined) {
  197. return "*"
  198. }
  199. if (extend["sort"] === "全部") {
  200. return "*"
  201. } else {
  202. let value = extend["sort"].replaceAll("/novel/", "").replaceAll("class/", "")
  203. if (value.indexOf("_") > -1){
  204. return value.split("_").slice(-1)[0]
  205. }else{
  206. return value
  207. }
  208. }
  209. }
  210. async setCategory(tid, pg, filter, extend) {
  211. if (tid === "/novel/rank") {
  212. tid = extend["sort"] ?? tid
  213. let $ = await this.getHtml(this.siteUrl + tid)
  214. this.vodList = await this.parseVodShortListFromDocByCategory($)
  215. let x = 0
  216. } else {
  217. let extendFilter = this.getExtend(extend)
  218. let limit = 18
  219. let params = {
  220. "language": "cn",
  221. "limit": limit,
  222. "type": tid.replaceAll("/novel/", "").replaceAll("class/", ""),
  223. "filter": extendFilter,
  224. "page": parseInt(pg),
  225. "__amp_source_origin": encodeURIComponent(this.siteUrl)
  226. }
  227. let resJson = JSON.parse(await this.fetch(this.apiUrl + "/nq/amp_novel_list", params, this.getHeader()))
  228. this.vodList = await this.parseVodShortListFromJson(resJson["items"])
  229. }
  230. }
  231. async setPlay(flag, id, flags) {
  232. let id_list = id.split("-")
  233. id = id_list[1]
  234. let content = id_list[0] + "\n\n"
  235. while (true) {
  236. let $ = await this.getHtml(this.siteUrl + id)
  237. content += Utils.formatContent($("[class=\"content\"]").html().trim().replaceAll("<p>", " ").replaceAll("</p>", "\n"));
  238. id = $("[id=\"next_url\"]")[0].attribs.href;
  239. if (id.indexOf('_') < 0) break;
  240. }
  241. this.playUrl = {"content": content}
  242. }
  243. async setSearch(wd, quick) {
  244. let params = {"q":encodeURIComponent(wd)}
  245. let content = await this.fetch(this.siteUrl + "/novel/search", params, this.getHeader())
  246. let $ = load(content)
  247. this.vodList = await this.parseVodShortListFromDocBySearch($)
  248. let x = 0
  249. }
  250. async setPlay(flag, id, flags) {
  251. let id_list = id.split("+")
  252. id = id_list[1]
  253. let content = id_list[0] + "\n\n"
  254. let $ = await this.getHtml(this.siteUrl + "/novel/pagea/" + id)
  255. let bookContentList = $('[class="content"]').text().trim().replaceAll("章节报错 分享给朋友:","").replaceAll(" ","").split("\n")
  256. let newBookContentList = []
  257. for (const bookContent of bookContentList){
  258. if (!_.isEmpty(bookContent.replaceAll(" ",""))){
  259. newBookContentList.push(bookContent.replaceAll(" "," "))
  260. }
  261. }
  262. content = content + " " + newBookContentList.join("\n\n")
  263. this.playUrl = {"content": content}
  264. }
  265. }
  266. let spider = new TTKanSpider()
  267. async function init(cfg) {
  268. await spider.init(cfg)
  269. }
  270. async function home(filter) {
  271. return await spider.home(filter)
  272. }
  273. async function homeVod() {
  274. return await spider.homeVod()
  275. }
  276. async function category(tid, pg, filter, extend) {
  277. return await spider.category(tid, pg, filter, extend)
  278. }
  279. async function detail(id) {
  280. return await spider.detail(id)
  281. }
  282. async function play(flag, id, flags) {
  283. return await spider.play(flag, id, flags)
  284. }
  285. async function search(wd, quick) {
  286. return await spider.search(wd, quick)
  287. }
  288. async function proxy(segments, headers) {
  289. return await spider.proxy(segments, headers)
  290. }
  291. export function __jsEvalReturn() {
  292. return {
  293. init: init,
  294. home: home,
  295. homeVod: homeVod,
  296. category: category,
  297. detail: detail,
  298. play: play,
  299. search: search,
  300. proxy: proxy
  301. };
  302. }
  303. export {spider}