bqg_open.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. if (id !== undefined) {
  77. $ = await this.getHtml(this.siteUrl + id + `list.html`);
  78. let urls = [];
  79. const links = $('dl>dd>a[href*="/html/"]');
  80. for (const l of links) {
  81. const name = $(l).text().trim();
  82. const link = l.attribs.href;
  83. urls.push(name + '$' + link);
  84. }
  85. bookDetail.volumes = '全卷';
  86. bookDetail.urls = urls.join('#');
  87. }
  88. return bookDetail
  89. }
  90. async setClasses() {
  91. let $ = await this.getHtml()
  92. for (const a of $('div.nav > ul > li > a[href!="/"]')) {
  93. this.classes.push({
  94. type_id: a.attribs.href.replace(/\//g, ''), type_name: a.children[0].data.trim(), tline: 2,
  95. });
  96. }
  97. }
  98. async setHomeVod() {
  99. let $ = await this.getHtml()
  100. this.homeVodList = await this.parseVodShortListFromDoc($)
  101. }
  102. async setDetail(id) {
  103. let $ = await this.getHtml(this.siteUrl + id)
  104. this.vodDetail = await this.parseVodDetailFromDoc($, id)
  105. }
  106. async setCategory(tid, pg, filter, extend) {
  107. let $ = await this.getHtml(this.siteUrl + `/${tid}/${pg}.html`);
  108. this.vodList = await this.parseVodShortListFromDocByCategory($)
  109. }
  110. async setPlay(flag, id, flags) {
  111. try {
  112. let content = '';
  113. while (true) {
  114. let $ = await this.getHtml(this.siteUrl + id)
  115. content += $('#chaptercontent')
  116. .html()
  117. .replace(/<br>|请收藏.*?<\/p>/g, '\n')
  118. .trim();
  119. id = $('a.Readpage_down')[0].attribs.href;
  120. if (id.indexOf('_') < 0) break;
  121. }
  122. this.playUrl = {"content":content + '\n\n'}
  123. } catch (e) {
  124. this.playUrl = {"content":""}
  125. }
  126. }
  127. async search(wd, quick) {
  128. const cook = await req(`${this.siteUrl}/user/hm.html?q=${encodeURIComponent(wd)}`, {
  129. headers: {
  130. accept: 'application/json',
  131. 'User-Agent': Utils.MOBILEUA,
  132. Referer: `${this.siteUrl}/s?q=${encodeURIComponent(wd)}`,
  133. },
  134. });
  135. const set_cookie = _.isArray(cook.headers['set-cookie']) ? cook.headers['set-cookie'].join(';;;') : cook.headers['set-cookie'];
  136. const cks = set_cookie.split(';;;');
  137. const cookie = {};
  138. for (const c of cks) {
  139. const tmp = c.trim();
  140. const idx = tmp.indexOf('=');
  141. const k = tmp.substr(0, idx);
  142. cookie[k] = tmp.substr(idx + 1, tmp.indexOf(';') - idx - 1);
  143. }
  144. const resp = await req(`${this.siteUrl}/user/search.html?q=${encodeURIComponent(wd)}&so=undefined`, {
  145. headers: {
  146. accept: 'application/json',
  147. 'User-Agent': Utils.MOBILEUA,
  148. cookie: 'hm=' + cookie['hm'],
  149. Referer: `${this.siteUrl}/s?q=${encodeURIComponent(wd)}`,
  150. },
  151. });
  152. let data = JSON.parse(resp.content);
  153. let books = [];
  154. for (const book of data) {
  155. books.push({
  156. book_id: book["url_list"],
  157. book_name: book["articlename"],
  158. book_pic: book["url_img"],
  159. book_remarks: book["author"],
  160. });
  161. }
  162. return {
  163. tline: 2, list: books,
  164. };
  165. }
  166. async proxy(segments, headers) {
  167. await this.jadeLog.debug(`正在设置反向代理 segments = ${segments.join(",")},headers = ${JSON.stringify(headers)}`)
  168. let what = segments[0];
  169. let url = Utils.base64Decode(segments[1]);
  170. if (what === 'img') {
  171. await this.jadeLog.debug(`反向代理ID为:${url}`)
  172. let $ = await this.getHtml(this.siteUrl + url)
  173. let bookDetail = await this.parseVodDetailFromDoc($)
  174. let resp;
  175. if (!_.isEmpty(headers)) {
  176. resp = await req(bookDetail.book_pic, {
  177. buffer: 2, headers: headers
  178. });
  179. } else {
  180. resp = await req(bookDetail.book_pic, {
  181. buffer: 2, headers: {
  182. Referer: url, 'User-Agent': Utils.CHROME,
  183. },
  184. });
  185. }
  186. return JSON.stringify({
  187. code: resp.code, buffer: 2, content: resp.content, headers: resp.headers,
  188. });
  189. }
  190. return JSON.stringify({
  191. code: 500, content: '',
  192. });
  193. }
  194. }
  195. let spider = new BQQSpider()
  196. async function init(cfg) {
  197. await spider.init(cfg)
  198. }
  199. async function home(filter) {
  200. return await spider.home(filter)
  201. }
  202. async function homeVod() {
  203. return await spider.homeVod()
  204. }
  205. async function category(tid, pg, filter, extend) {
  206. return await spider.category(tid, pg, filter, extend)
  207. }
  208. async function detail(id) {
  209. return await spider.detail(id)
  210. }
  211. async function play(flag, id, flags) {
  212. return await spider.play(flag, id, flags)
  213. }
  214. async function search(wd, quick) {
  215. return await spider.search(wd, quick)
  216. }
  217. async function proxy(segments, headers) {
  218. return await spider.proxy(segments, headers)
  219. }
  220. export function __jsEvalReturn() {
  221. return {
  222. init: init,
  223. home: home,
  224. homeVod: homeVod,
  225. category: category,
  226. detail: detail,
  227. play: play,
  228. search: search,
  229. proxy: proxy
  230. };
  231. }
  232. export {spider}