bqg_open.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * @File : bqg_open.js.js
  3. * @Author : jade
  4. * @Date : 2024/1/30 15:38
  5. * @Email : jadehh@1ive.com
  6. * @Software : Samples
  7. * @Desc :
  8. */
  9. import {_} 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 BQQSpider extends Spider {
  14. constructor() {
  15. super();
  16. this.siteUrl = "https://m.13bqg.com"
  17. }
  18. getAppName() {
  19. return "笔趣阁"
  20. }
  21. getJSName() {
  22. return "bqg_open"
  23. }
  24. getType() {
  25. return 10
  26. }
  27. getName() {
  28. return "📚︎┃笔趣阁┃📚︎"
  29. }
  30. async spiderInit(inReq = null) {
  31. if (inReq !== null) {
  32. this.jsBase = await js2Proxy(inReq, "img", this.getHeader());
  33. } else {
  34. this.jsBase = await js2Proxy(true, this.siteType, this.siteKey, 'img/', this.getHeader());
  35. }
  36. }
  37. async init(cfg) {
  38. await super.init(cfg);
  39. await this.spiderInit(null)
  40. }
  41. async parseVodShortListFromDoc($) {
  42. let books = []
  43. let bookElements = $("[class=\"block\"]")
  44. for (const bookElement of $(bookElements[0]).find("li")) {
  45. let bookShort = new BookShort()
  46. let bookShortElements = $(bookElement).find("span")
  47. bookShort.book_remarks = $(bookShortElements[0]).text()
  48. bookShort.book_name = $(bookShortElements[1]).text()
  49. bookShort.book_id = $(bookShortElements[1]).find("a")[0].attribs.href
  50. bookShort.book_pic = this.jsBase + Utils.base64Encode(bookShort.book_id)
  51. books.push(bookShort)
  52. }
  53. return books
  54. }
  55. async parseVodShortListFromDocByCategory($) {
  56. let books = [];
  57. for (const item of $('div.item')) {
  58. let bookShort = new BookShort()
  59. bookShort.book_id = $(item).find('a:first')[0].attribs.href;
  60. const img = $(item).find('img:first')[0];
  61. bookShort.book_name = img.attribs.alt
  62. bookShort.book_pic = img.attribs.src
  63. bookShort.book_remarks = $(item).find('span:first')[0].children[0].data.trim();
  64. books.push(bookShort)
  65. }
  66. return books
  67. }
  68. async parseVodDetailFromDoc($, id) {
  69. let bookDetail = new BookDetail()
  70. bookDetail.book_name = $('[property$=book_name]')[0].attribs.content
  71. bookDetail.book_year = $('[property$=update_time]')[0].attribs.content
  72. bookDetail.book_director = $('[property$=author]')[0].attribs.content
  73. bookDetail.book_content = $('[property$=description]')[0].attribs.content
  74. bookDetail.book_pic = $($("[class=\"cover\"]")).find("img")[0].attribs.src
  75. bookDetail.book_id = id
  76. let playBook = {}
  77. if (id !== undefined) {
  78. $ = await this.getHtml(this.siteUrl + id + `list.html`);
  79. let urls = [];
  80. const links = $('dl>dd>a[href*="/html/"]');
  81. for (const l of links) {
  82. const name = $(l).text().trim();
  83. const link = l.attribs.href;
  84. urls.push(name + '$' + link);
  85. }
  86. playBook["最新章节"] = urls.slice(-10).join('#');
  87. playBook["目录"] = urls.join('#');
  88. }
  89. bookDetail.volumes = _.keys(playBook).join('$$$');
  90. bookDetail.urls = _.values(playBook).join('$$$');
  91. return bookDetail
  92. }
  93. async setClasses() {
  94. let $ = await this.getHtml()
  95. for (const a of $('div.nav > ul > li > a[href!="/"]')) {
  96. this.classes.push({
  97. type_id: a.attribs.href.replace(/\//g, ''), type_name: a.children[0].data.trim(), tline: 2,
  98. });
  99. }
  100. }
  101. async setHomeVod() {
  102. let $ = await this.getHtml()
  103. this.homeVodList = await this.parseVodShortListFromDoc($)
  104. }
  105. async setDetail(id) {
  106. let $ = await this.getHtml(this.siteUrl + id)
  107. this.vodDetail = await this.parseVodDetailFromDoc($, id)
  108. }
  109. async setCategory(tid, pg, filter, extend) {
  110. let $ = await this.getHtml(this.siteUrl + `/${tid}/${pg}.html`);
  111. this.vodList = await this.parseVodShortListFromDocByCategory($)
  112. }
  113. async setPlay(flag, id, flags) {
  114. try {
  115. let content = '';
  116. while (true) {
  117. let $ = await this.getHtml(this.siteUrl + id)
  118. content += $('#chaptercontent')
  119. .html()
  120. .replace(/<br>|请收藏.*?<\/p>/g, '\n')
  121. .trim();
  122. id = $('a.Readpage_down')[0].attribs.href;
  123. if (id.indexOf('_') < 0) break;
  124. }
  125. this.playUrl = {"content":content + '\n\n'}
  126. } catch (e) {
  127. this.playUrl = {"content":""}
  128. }
  129. }
  130. async search(wd, quick) {
  131. const cook = await req(`${this.siteUrl}/user/hm.html?q=${encodeURIComponent(wd)}`, {
  132. headers: {
  133. accept: 'application/json',
  134. 'User-Agent': Utils.MOBILEUA,
  135. Referer: `${this.siteUrl}/s?q=${encodeURIComponent(wd)}`,
  136. },
  137. });
  138. const set_cookie = _.isArray(cook.headers['set-cookie']) ? cook.headers['set-cookie'].join(';;;') : cook.headers['set-cookie'];
  139. const cks = set_cookie.split(';;;');
  140. const cookie = {};
  141. for (const c of cks) {
  142. const tmp = c.trim();
  143. const idx = tmp.indexOf('=');
  144. const k = tmp.substr(0, idx);
  145. cookie[k] = tmp.substr(idx + 1, tmp.indexOf(';') - idx - 1);
  146. }
  147. const resp = await req(`${this.siteUrl}/user/search.html?q=${encodeURIComponent(wd)}&so=undefined`, {
  148. headers: {
  149. accept: 'application/json',
  150. 'User-Agent': Utils.MOBILEUA,
  151. cookie: 'hm=' + cookie['hm'],
  152. Referer: `${this.siteUrl}/s?q=${encodeURIComponent(wd)}`,
  153. },
  154. });
  155. let data = JSON.parse(resp.content);
  156. let books = [];
  157. for (const book of data) {
  158. books.push({
  159. book_id: book["url_list"],
  160. book_name: book["articlename"],
  161. book_pic: book["url_img"],
  162. book_remarks: book["author"],
  163. });
  164. }
  165. return {
  166. tline: 2, list: books,
  167. };
  168. }
  169. async proxy(segments, headers) {
  170. await this.jadeLog.debug(`正在设置反向代理 segments = ${segments.join(",")},headers = ${JSON.stringify(headers)}`)
  171. let what = segments[0];
  172. let url = Utils.base64Decode(segments[1]);
  173. if (what === 'img') {
  174. await this.jadeLog.debug(`反向代理ID为:${url}`)
  175. let $ = await this.getHtml(this.siteUrl + url)
  176. let bookDetail = await this.parseVodDetailFromDoc($)
  177. let resp;
  178. if (!_.isEmpty(headers)) {
  179. resp = await req(bookDetail.book_pic, {
  180. buffer: 2, headers: headers
  181. });
  182. } else {
  183. resp = await req(bookDetail.book_pic, {
  184. buffer: 2, headers: {
  185. Referer: url, 'User-Agent': Utils.CHROME,
  186. },
  187. });
  188. }
  189. return JSON.stringify({
  190. code: resp.code, buffer: 2, content: resp.content, headers: resp.headers,
  191. });
  192. }
  193. return JSON.stringify({
  194. code: 500, content: '',
  195. });
  196. }
  197. }
  198. let spider = new BQQSpider()
  199. async function init(cfg) {
  200. await spider.init(cfg)
  201. }
  202. async function home(filter) {
  203. return await spider.home(filter)
  204. }
  205. async function homeVod() {
  206. return await spider.homeVod()
  207. }
  208. async function category(tid, pg, filter, extend) {
  209. return await spider.category(tid, pg, filter, extend)
  210. }
  211. async function detail(id) {
  212. return await spider.detail(id)
  213. }
  214. async function play(flag, id, flags) {
  215. return await spider.play(flag, id, flags)
  216. }
  217. async function search(wd, quick) {
  218. return await spider.search(wd, quick)
  219. }
  220. async function proxy(segments, headers) {
  221. return await spider.proxy(segments, headers)
  222. }
  223. export function __jsEvalReturn() {
  224. return {
  225. init: init,
  226. home: home,
  227. homeVod: homeVod,
  228. category: category,
  229. detail: detail,
  230. play: play,
  231. search: search,
  232. proxy: proxy
  233. };
  234. }
  235. export {spider}