drpy.js 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292
  1. // import 'https://gitcode.net/qq_32394351/dr_py/-/raw/master/libs/es6py.js';
  2. // import {是否正版,urlDeal,setResult,setResult2,setHomeResult,maoss,urlencode} from 'http://192.168.10.103:5705/libs/es6py.js';
  3. // import 'http://192.168.1.124:5705/libs/es6py.js';
  4. import cheerio from 'https://gitcode.net/qq_32394351/dr_py/-/raw/master/libs/cheerio.min.js';
  5. // import cheerio from 'http://192.168.10.103:5705/libs/cheerio.min.js';
  6. import 'https://gitcode.net/qq_32394351/dr_py/-/raw/master/libs/crypto-js.js';
  7. import 'https://gitcode.net/qq_32394351/dr_py/-/raw/master/libs/drT.js';
  8. import 模板 from 'https://gitcode.net/qq_32394351/dr_py/-/raw/master/js/模板.js';
  9. // import 'http://192.168.10.103:5705/libs/drT.js';
  10. // import muban from 'https://gitcode.net/qq_32394351/dr_py/-/raw/master/js/模板.js';
  11. // import muban from 'http://192.168.10.103:5705/admin/view/模板.js';
  12. // const key = 'drpy_zbk';
  13. // eval(req('http://192.168.1.124:5705/libs/es6py.js').content);
  14. function init_test(){
  15. // console.log(typeof(CryptoJS));
  16. console.log("init_test_start");
  17. console.log("当前版本号:"+VERSION);
  18. console.log(RKEY);
  19. console.log(JSON.stringify(rule));
  20. // console.log('123456的md5值是:'+md5('123456'));
  21. // let aa = base64Encode('编码测试一下')
  22. // log(aa);
  23. // let bb = base64Decode(aa);
  24. // log('bb:'+bb);
  25. // clearItem(RULE_CK);
  26. // console.log(JSON.stringify(rule));
  27. // console.log(request('https://www.baidu.com',{withHeaders:true}));
  28. // console.log(request('https://www.baidu.com/favicon.ico',{toBase64:true}));
  29. // require('http://192.168.10.99:5705/txt/pluto/drT.js');
  30. // console.log(typeof(drT));
  31. // console.log(drT.renderText('{{fl.cate}},hi, {{fl}}哈哈.{{fl}}',{sort: 1,cate:'movie'},'fl'));
  32. console.log("init_test_end");
  33. }
  34. /**
  35. * 执行预处理代码
  36. */
  37. function pre(){
  38. if(typeof(rule.预处理) === 'string' && rule.预处理 && rule.预处理.trim()){
  39. let code = rule.预处理.trim();
  40. console.log("执行预处理代码:"+code);
  41. if(code.startsWith('js:')){
  42. code = code.replace('js:','');
  43. }
  44. try {
  45. // code里可以进行get 或者 post请求cookie并改变rule.headers 里的cookie
  46. // 直接操作 rule_fetch_params 这个变量 .headers.Cookie
  47. eval(code);
  48. }catch (e) {
  49. console.log('预处理执行失败:'+e.message);
  50. }
  51. }
  52. }
  53. let rule = {};
  54. const VERSION = 'drpy1 3.9.30 20221204';
  55. /** 已知问题记录
  56. * 1.影魔的jinjia2引擎不支持 {{fl}}对象直接渲染 (有能力解决的话尽量解决下,支持对象直接渲染字符串转义,如果加了|safe就不转义)[影魔牛逼,最新的文件发现这问题已经解决了]
  57. * Array.prototype.append = Array.prototype.push; 这种js执行后有毛病,for in 循环列表会把属性给打印出来 (这个大毛病需要重点排除一下)
  58. * 2.import es6py.js但是里面的函数没有被装载进来.比如drpy规则报错setResult2 is undefiend(合并文件了可以不管了)
  59. * 3.无法重复导入cheerio(怎么解决drpy和parseTag里都需要导入cheerio的问题) 无法在副文件导入cheerio (现在是全部放在drpy一个文件里了,凑合解决?)
  60. * 4.有个错误不知道哪儿来的 executeScript: com.quickjs.JSObject$Undefined cannot be cast to java.lang.String 在 点击选集播放打印init_test_end后面打印(貌似不影响使用)
  61. * 5.需要实现 stringify 函数,比起JSON.stringify函数,它会原封不动保留中文不会编码unicode
  62. * 6.base64Encode,base64Decode,md5函数还没有实现 (抄影魔代码实现了)
  63. * 7.eval(getCryptoJS());还没有实现 (可以空实现了,以后遇到能忽略)
  64. * done: jsp:{pdfa,pdfh,pd},json:{pdfa,pdfh,pd},jq:{pdfa,pdfh,pd}
  65. * * 电脑看日志调试
  66. adb tcpip 5555
  67. adb connect 192.168.10.192
  68. adb devices -l
  69. adb logcat -c
  70. adb logcat | grep -i QuickJS
  71. adb logcat -c -b events
  72. adb logcat -c -b main -b events -b radio -b system
  73. adb logcat > 2.log DRPY:E | grep -i QuickJS
  74. * **/
  75. /*** 以下是内置变量和解析方法 **/
  76. const MOBILE_UA = 'Mozilla/5.0 (Linux; Android 11; M2007J3SC Build/RKQ1.200826.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/77.0.3865.120 MQQBrowser/6.2 TBS/045714 Mobile Safari/537.36';
  77. const PC_UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36';
  78. const UA = 'Mozilla/5.0';
  79. const UC_UA = 'Mozilla/5.0 (Linux; U; Android 9; zh-CN; MI 9 Build/PKQ1.181121.001) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.108 UCBrowser/12.5.5.1035 Mobile Safari/537.36';
  80. const IOS_UA = 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1';
  81. const RULE_CK = 'cookie'; // 源cookie的key值
  82. // const KEY = typeof(key)!=='undefined'&&key?key:'drpy_' + (rule.title || rule.host); // 源的唯一标识
  83. const CATE_EXCLUDE = '首页|留言|APP|下载|资讯|新闻|动态';
  84. const TAB_EXCLUDE = '猜你|喜欢|APP|下载|剧情|热播';
  85. const OCR_RETRY = 3;//ocr验证重试次数
  86. // const OCR_API = 'http://dm.mudery.com:10000';//ocr在线识别接口
  87. // const OCR_API = 'http://192.168.3.239:5705/parse/ocr';//ocr在线识别接口
  88. // const OCR_API = 'http://cms.nokia.press/parse/ocr';//ocr在线识别接口
  89. const OCR_API = 'http://cms.nokia.press:5706/parse/ocr';//ocr在线识别接口
  90. if(typeof(MY_URL)==='undefined'){
  91. var MY_URL; // 全局注入变量,pd函数需要
  92. }
  93. var RKEY; // 源的唯一标识
  94. var fetch;
  95. var print;
  96. var log;
  97. var rule_fetch_params;
  98. var fetch_params; // 每个位置单独的
  99. var oheaders;
  100. // var play_url; // 二级详情页注入变量,为了适配js模式0 (不在这里定义了,直接二级里定义了个空字符串)
  101. var _pdfh;
  102. var _pdfa;
  103. var _pd;
  104. // const DOM_CHECK_ATTR = ['url', 'src', 'href', 'data-original', 'data-src'];
  105. const DOM_CHECK_ATTR = /(url|src|href|-original|-src|-play|-url)$/;
  106. const SELECT_REGEX = /:eq|:lt|:gt|#/g;
  107. const SELECT_REGEX_A = /:eq|:lt|:gt/g;
  108. /**
  109. es6py扩展
  110. */
  111. if (typeof Object.assign != 'function') {
  112. Object.assign = function () {
  113. var target = arguments[0];
  114. for (var i = 1; i < arguments.length; i++) {
  115. var source = arguments[i];
  116. for (var key in source) {
  117. if (Object.prototype.hasOwnProperty.call(source, key)) {
  118. target[key] = source[key];
  119. }
  120. }
  121. }
  122. return target;
  123. };
  124. }
  125. if (!String.prototype.includes) {
  126. String.prototype.includes = function (search, start) {
  127. if (typeof start !== 'number') {
  128. start = 0;
  129. }
  130. if (start + search.length > this.length) {
  131. return false;
  132. } else {
  133. return this.indexOf(search, start) !== -1;
  134. }
  135. };
  136. }
  137. if (!Array.prototype.includes) {
  138. Object.defineProperty(Array.prototype, 'includes', {
  139. value: function (searchElement, fromIndex) {
  140. if (this == null) {//this是空或者未定义,抛出错误
  141. throw new TypeError('"this" is null or not defined');
  142. }
  143. var o = Object(this);//将this转变成对象
  144. var len = o.length >>> 0;//无符号右移0位,获取对象length属性,如果未定义就会变成0
  145. if (len === 0) {//length为0直接返回false未找到目标值
  146. return false;
  147. }
  148. var n = fromIndex | 0;//查找起始索引
  149. var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);//计算正确起始索引,因为有可能是负值
  150. while (k < len) {//从起始索引处开始循环
  151. if (o[k] === searchElement) {//如果某一位置与寻找目标相等,返回true,找到了
  152. return true;
  153. }
  154. k++;
  155. }
  156. return false;//未找到,返回false
  157. }
  158. });
  159. }
  160. if (typeof String.prototype.startsWith != 'function') {
  161. String.prototype.startsWith = function (prefix){
  162. return this.slice(0, prefix.length) === prefix;
  163. };
  164. }
  165. if (typeof String.prototype.endsWith != 'function') {
  166. String.prototype.endsWith = function(suffix) {
  167. return this.indexOf(suffix, this.length - suffix.length) !== -1;
  168. };
  169. }
  170. Object.prototype.myValues=function(obj){
  171. if(obj ==null) {
  172. throw new TypeError("Cannot convert undefined or null to object");
  173. }
  174. var res=[]
  175. for(var k in obj){
  176. if(obj.hasOwnProperty(k)){//需判断是否是本身的属性
  177. res.push(obj[k]);
  178. }
  179. }
  180. return res;
  181. }
  182. if (typeof Object.prototype.values != 'function') {
  183. Object.prototype.values=function(obj){
  184. if(obj ==null) {
  185. throw new TypeError("Cannot convert undefined or null to object");
  186. }
  187. var res=[]
  188. for(var k in obj){
  189. if(obj.hasOwnProperty(k)){//需判断是否是本身的属性
  190. res.push(obj[k]);
  191. }
  192. }
  193. return res;
  194. }
  195. }
  196. if (typeof Array.prototype.join != 'function') {
  197. Array.prototype.join = function (emoji) {
  198. // emoji = emoji||',';
  199. emoji = emoji||'';
  200. let self = this;
  201. let str = "";
  202. let i = 0;
  203. if (!Array.isArray(self)) {throw String(self)+'is not Array'}
  204. if(self.length===0){return ''}
  205. if (self.length === 1){return String(self[0])}
  206. i = 1;
  207. str = this[0];
  208. for (; i < self.length; i++) {
  209. str += String(emoji)+String(self[i]);
  210. }
  211. return str;
  212. };
  213. }
  214. String.prototype.rstrip = function (chars) {
  215. let regex = new RegExp(chars + "$");
  216. return this.replace(regex, "");
  217. };
  218. Array.prototype.append = Array.prototype.push;
  219. String.prototype.strip = String.prototype.trim;
  220. function 是否正版(vipUrl){
  221. let flag = new RegExp('qq\.com|iqiyi\.com|youku\.com|mgtv\.com|bilibili\.com|sohu\.com|ixigua\.com|pptv\.com|miguvideo\.com|le\.com|1905\.com|fun\.tv');
  222. return flag.test(vipUrl);
  223. }
  224. function urlDeal(vipUrl){
  225. if(!vipUrl){
  226. return ''
  227. }
  228. if(!是否正版(vipUrl)){
  229. return vipUrl
  230. }
  231. if(!/miguvideo/.test(vipUrl)){
  232. vipUrl=vipUrl.split('#')[0].split('?')[0];
  233. }
  234. return vipUrl
  235. }
  236. function setResult(d){
  237. if(!Array.isArray(d)){
  238. return []
  239. }
  240. VODS = [];
  241. // print(d);
  242. d.forEach(function (it){
  243. let obj = {
  244. vod_id:it.url||'',
  245. vod_name: it.title||'',
  246. vod_remarks: it.desc||'',
  247. vod_content: it.content||'',
  248. vod_pic: it.pic_url||it.img||'',
  249. };
  250. let keys = Object.keys(it);
  251. if(keys.includes('tname')){
  252. obj.type_name = it.tname||'';
  253. }
  254. if(keys.includes('tid')){
  255. obj.type_id = it.tid||'';
  256. }
  257. if(keys.includes('year')){
  258. obj.vod_year = it.year||'';
  259. }
  260. if(keys.includes('actor')){
  261. obj.vod_actor = it.actor||'';
  262. }
  263. if(keys.includes('director')){
  264. obj.vod_director = it.director||'';
  265. }
  266. if(keys.includes('area')){
  267. obj.vod_area = it.area||'';
  268. }
  269. VODS.push(obj);
  270. });
  271. return VODS
  272. }
  273. function setResult2(res){
  274. VODS = res.list||[];
  275. return VODS
  276. }
  277. function setHomeResult(res){
  278. if(!res||typeof(res)!=='object'){
  279. return []
  280. }
  281. return setResult(res.list);
  282. }
  283. // 猫了个咪
  284. function rc(js) {
  285. if (js === 'maomi_aes.js') {
  286. var a = CryptoJS.enc.Utf8.parse("625222f9149e961d");
  287. var t = CryptoJS.enc.Utf8.parse("5efdtf6060e2o330");
  288. return {
  289. De: function (word) {
  290. word = CryptoJS.enc.Hex.parse(word)
  291. return CryptoJS.AES.decrypt(CryptoJS.enc.Base64.stringify(word), a, {
  292. iv: t,
  293. mode: CryptoJS.mode.CBC,
  294. padding: CryptoJS.pad.Pkcs7
  295. }).toString(CryptoJS.enc.Utf8)
  296. },
  297. En: function (word) {
  298. // print(a);
  299. // print(word);
  300. var Encrypted = CryptoJS.AES.encrypt(word, a, {
  301. iv: t,
  302. mode: CryptoJS.mode.CBC,
  303. padding: CryptoJS.pad.Pkcs7
  304. });
  305. return Encrypted.ciphertext.toString();
  306. }
  307. };
  308. }
  309. return {};
  310. }
  311. // 千万不要用for in 推荐 forEach (for in 会打乱顺序)
  312. //猫函数
  313. function maoss(jxurl, ref, key) {
  314. fetch_params = JSON.parse(JSON.stringify(rule_fetch_params));
  315. eval(getCryptoJS());
  316. try {
  317. var getVideoInfo = function (text) {
  318. return CryptoJS.AES.decrypt(text, key, {iv: iv, padding: CryptoJS.pad.Pkcs7}).toString(CryptoJS.enc.Utf8);
  319. };
  320. var token_key = key == undefined ? 'dvyYRQlnPRCMdQSe' : key;
  321. if (ref) {
  322. var html = request(jxurl, {
  323. headers: {
  324. 'Referer': ref
  325. }
  326. });
  327. } else {
  328. var html = request(jxurl);
  329. }
  330. // print(html);
  331. if (html.indexOf('&btwaf=') != -1) {
  332. html = request(jxurl + '&btwaf' + html.match(/&btwaf(.*?)"/)[1], {
  333. headers: {
  334. 'Referer': ref
  335. }
  336. })
  337. }
  338. var token_iv = html.split('_token = "')[1].split('"')[0];
  339. var key = CryptoJS.enc.Utf8.parse(token_key);
  340. var iv = CryptoJS.enc.Utf8.parse(token_iv);
  341. // log("iv:"+iv);
  342. // log(html);
  343. // print(key);
  344. // print(iv);
  345. eval(html.match(/var config = {[\s\S]*?}/)[0] + '');
  346. // config.url = config.url.replace(/,/g,'');
  347. // print(config.url);
  348. if (!config.url.startsWith('http')) {
  349. //config.url = decodeURIComponent(AES(config.url, key, iv));
  350. config.url = CryptoJS.AES.decrypt(config.url, key, {
  351. iv: iv,
  352. padding: CryptoJS.pad.Pkcs7
  353. }).toString(CryptoJS.enc.Utf8)
  354. }
  355. return config.url;
  356. } catch (e) {
  357. return '';
  358. }
  359. }
  360. function urlencode (str) {
  361. str = (str + '').toString();
  362. return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
  363. replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
  364. }
  365. function base64Encode(text){
  366. return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(text));
  367. // return text
  368. }
  369. function base64Decode(text){
  370. return CryptoJS.enc.Utf8.stringify(CryptoJS.enc.Base64.parse(text));
  371. // return text
  372. }
  373. function md5(text) {
  374. return CryptoJS.MD5(text).toString();
  375. }
  376. function getCryptoJS(){
  377. // return request('https://gitcode.net/qq_32394351/dr_py/-/raw/master/libs/crypto-hiker.js');
  378. return 'console.log("CryptoJS已装载");'
  379. }
  380. /**
  381. * 强制正序算法
  382. * @param lists 待正序列表
  383. * @param key 正序键
  384. * @param option 单个元素处理函数
  385. * @returns {*}
  386. */
  387. function forceOrder(lists,key,option){
  388. let start = Math.floor(lists.length/2);
  389. let end = Math.min(lists.length-1,start+1);
  390. if(start >= end){
  391. return lists;
  392. }
  393. let first = lists[start];
  394. let second = lists[end];
  395. if(key){
  396. try {
  397. first = first[key];
  398. second = second[key];
  399. }catch (e) {}
  400. }
  401. if(option && typeof(option)==='function'){
  402. try {
  403. first = option(first);
  404. second = option(second);
  405. }catch (e) {}
  406. }
  407. first+='';
  408. second+='';
  409. // console.log(first,second);
  410. if(first.match(/(\d+)/)&&second.match(/(\d+)/)){
  411. let num1 = Number(first.match(/(\d+)/)[1]);
  412. let num2 = Number(second.match(/(\d+)/)[1]);
  413. if (num1 > num2){
  414. lists.reverse();
  415. }
  416. }
  417. return lists
  418. }
  419. let VODS = [];// 一级或者搜索需要的数据列表
  420. let VOD = {};// 二级的单个数据
  421. let TABS = [];// 二级的自定义线路列表 如: TABS=['道长在线','道长在线2']
  422. let LISTS = [];// 二级的自定义选集播放列表 如: LISTS=[['第1集$http://1.mp4','第2集$http://2.mp4'],['第3集$http://1.mp4','第4集$http://2.mp4']]
  423. globalThis.encodeUrl = urlencode;
  424. globalThis.urlencode = urlencode;
  425. /**
  426. * url拼接
  427. * @param fromPath 初始当前页面url
  428. * @param nowPath 相对当前页面url
  429. * @returns {*}
  430. */
  431. function urljoin(fromPath, nowPath) {
  432. fromPath = fromPath||'';
  433. nowPath = nowPath||'';
  434. return joinUrl(fromPath, nowPath);
  435. // try {
  436. // // import Uri from './uri.min.js';
  437. // // var Uri = require('./uri.min.js');
  438. // // eval(request('https://cdn.bootcdn.net/ajax/libs/URI.js/1.19.11/URI.min.js'));
  439. // // let new_uri = URI(nowPath, fromPath);
  440. // let new_uri = Uri(nowPath, fromPath);
  441. // new_uri = new_uri.toString();
  442. // // console.log(new_uri);
  443. // // return fromPath + nowPath
  444. // return new_uri
  445. // }
  446. // catch (e) {
  447. // console.log('urljoin发生错误:'+e.message);
  448. // if(nowPath.startsWith('http')){
  449. // return nowPath
  450. // }if(nowPath.startsWith('/')){
  451. // return getHome(fromPath)+nowPath
  452. // }
  453. // return fromPath+nowPath
  454. // }
  455. }
  456. var urljoin2 = urljoin;
  457. // 内置 pdfh,pdfa,pd
  458. const defaultParser = {
  459. pdfh:pdfh,
  460. pdfa:pdfa,
  461. pd(html,parse,uri){
  462. let ret = this.pdfh(html,parse);
  463. if(typeof(uri)==='undefined'||!uri){
  464. uri = '';
  465. }
  466. if(DOM_CHECK_ATTR.test(parse)){
  467. if(/http/.test(ret)){
  468. ret = ret.substr(ret.indexOf('http'));
  469. }else{
  470. ret = urljoin(MY_URL,ret)
  471. }
  472. }
  473. return ret
  474. },
  475. };
  476. /**
  477. * pdfh原版优化,能取style属性里的图片链接
  478. * @param html 源码
  479. * @param parse 解析表达式
  480. * @returns {string|*}
  481. */
  482. function pdfh2(html,parse){
  483. let html2 = html;
  484. try {
  485. if(typeof(html)!=='string'){
  486. html2 = html.rr(html.ele).toString();
  487. }
  488. }catch (e) {
  489. print('html对象转文本发生了错误:'+e.message);
  490. }
  491. let result = defaultParser.pdfh(html2,parse);
  492. let option = parse.includes('&&')?parse.split('&&').slice(-1)[0]:parse.split(' ').slice(-1)[0];
  493. if(/style/.test(option.toLowerCase())&&/url\(/.test(result)){
  494. try {
  495. result = result.match(/url\((.*?)\)/)[1];
  496. }catch (e) {}
  497. }
  498. return result
  499. }
  500. /**
  501. * pdfa原版优化,可以转换jq的html对象
  502. * @param html
  503. * @param parse
  504. * @returns {*}
  505. */
  506. function pdfa2(html,parse){
  507. let html2 = html;
  508. try {
  509. if(typeof(html)!=='string'){
  510. html2 = html.rr(html.ele).toString();
  511. }
  512. }catch (e) {
  513. print('html对象转文本发生了错误:'+e.message);
  514. }
  515. return defaultParser.pdfa(html2,parse);
  516. }
  517. /**
  518. * pd原版方法重写-增加自动urljoin
  519. * @param html
  520. * @param parse
  521. * @param uri
  522. * @returns {*}
  523. */
  524. function pd2(html,parse,uri){
  525. let ret = pdfh2(html,parse);
  526. if(typeof(uri)==='undefined'||!uri){
  527. uri = '';
  528. }
  529. if(DOM_CHECK_ATTR.test(parse)){
  530. if(/http/.test(ret)){
  531. ret = ret.substr(ret.indexOf('http'));
  532. }else{
  533. ret = urljoin(MY_URL,ret)
  534. }
  535. }
  536. // MY_URL = getItem('MY_URL',MY_URL);
  537. // console.log(`规则${RKEY}打印MY_URL:${MY_URL},uri:${uri}`);
  538. return ret
  539. }
  540. const parseTags = {
  541. jsp:{
  542. pdfh:pdfh2,
  543. pdfa:pdfa2,
  544. pd:pd2,
  545. },
  546. json:{
  547. pdfh(html, parse) {
  548. if (!parse || !parse.trim()){
  549. return '';
  550. }
  551. if (typeof(html) === 'string'){
  552. // print('jsonpath:pdfh字符串转dict');
  553. html = JSON.parse(html);
  554. }
  555. parse = parse.trim();
  556. if (!parse.startsWith('$.')){
  557. parse = '$.' + parse;
  558. }
  559. parse = parse.split('||');
  560. for (let ps of parse) {
  561. let ret = cheerio.jp(ps, html);
  562. if (Array.isArray(ret)){
  563. ret = ret[0] || '';
  564. } else{
  565. ret = ret || ''
  566. }
  567. if (ret && typeof (ret) !== 'string'){
  568. ret = ret.toString();
  569. }
  570. if(ret){
  571. return ret
  572. }
  573. }
  574. return '';
  575. },
  576. pdfa(html, parse) {
  577. if (!parse || !parse.trim()){
  578. return '';
  579. }
  580. if (typeof(html) === 'string'){
  581. // print('jsonpath:pdfa字符串转dict');
  582. html = JSON.parse(html);
  583. }
  584. parse = parse.trim()
  585. if (!parse.startsWith('$.')){
  586. parse = '$.' + parse;
  587. }
  588. let ret = cheerio.jp(parse, html);
  589. if (Array.isArray(ret) && Array.isArray(ret[0]) && ret.length === 1){
  590. return ret[0] || []
  591. }
  592. return ret || []
  593. },
  594. pd(html,parse){
  595. let ret = parseTags.json.pdfh(html,parse);
  596. if(ret){
  597. return urljoin(MY_URL,ret);
  598. }
  599. return ret
  600. },
  601. },
  602. jq:{
  603. pdfh(html, parse, base_url) {
  604. if (!parse || !parse.trim()) {
  605. return ''
  606. }
  607. let eleFind = typeof html === 'object';
  608. let option = undefined;
  609. if (eleFind && parse.startsWith('body&&')) {
  610. parse = parse.substr(6);
  611. if (parse.indexOf('&&') < 0) {
  612. option = parse.trim();
  613. parse = '*=*';
  614. }
  615. }
  616. if (parse.indexOf('&&') > -1) {
  617. let sp = parse.split('&&');
  618. option = sp[sp.length - 1];
  619. sp.splice(sp.length - 1);
  620. if (sp.length > 1) {
  621. for (let i in sp) {
  622. //Javascript自定义Array.prototype干扰for-in循环
  623. if(sp.hasOwnProperty(i)){
  624. if (!SELECT_REGEX.test(sp[i])) {
  625. sp[i] = sp[i] + ':eq(0)';
  626. }
  627. }
  628. }
  629. } else {
  630. if (!SELECT_REGEX.test(sp[0])) {
  631. sp[0] = sp[0] + ':eq(0)';
  632. }
  633. }
  634. parse = sp.join(' ');
  635. }
  636. let result = '';
  637. const $ = eleFind ? html.rr : cheerio.load(html);
  638. let ret = eleFind ? ((parse === '*=*' || $(html.ele).is(parse)) ? html.ele : $(html.ele).find(parse)) : $(parse);
  639. if (option) {
  640. if (option === 'Text') {
  641. result = $(ret).text();
  642. }
  643. else if (option === 'Html') {
  644. result = $(ret).html();
  645. }
  646. else {
  647. result = $(ret).attr(option);
  648. if(/style/.test(option.toLowerCase())&&/url\(/.test(result)){
  649. try {
  650. result = result.match(/url\((.*?)\)/)[1];
  651. }catch (e) {}
  652. }
  653. }
  654. if (result && base_url && DOM_CHECK_ATTR.test(option)) {
  655. if (/http/.test(result)) {
  656. result = result.substr(result.indexOf('http'));
  657. } else {
  658. result = urljoin(base_url, result)
  659. }
  660. }
  661. } else {
  662. result = $(ret).toString();
  663. }
  664. return result;
  665. },
  666. pdfa(html, parse) {
  667. if (!parse || !parse.trim()) {
  668. print('!parse');
  669. return [];
  670. }
  671. let eleFind = typeof html === 'object';
  672. // print('parse前:'+parse);
  673. if (parse.indexOf('&&') > -1) {
  674. let sp = parse.split('&&');
  675. for (let i in sp) {
  676. if(sp.hasOwnProperty(i)){
  677. if (!SELECT_REGEX_A.test(sp[i]) && i < sp.length - 1) {
  678. if(sp[i]!=='body'){
  679. // sp[i] = sp[i] + ':eq(0)';
  680. sp[i] = sp[i] + ':first';
  681. }
  682. }
  683. }
  684. }
  685. parse = sp.join(' ');
  686. }
  687. // print('parse后:'+parse);
  688. const $ = eleFind ? html.rr : cheerio.load(html);
  689. let ret = eleFind ? ($(html.ele).is(parse) ? html.ele : $(html.ele).find(parse)) : $(parse);
  690. let result = [];
  691. // print('outerHTML:');
  692. // print($(ret[0]).prop("outerHTML"));
  693. if (ret) {
  694. ret.each(function (idx, ele) {
  695. result.push({ rr: $, ele: ele });
  696. // result.push({ rr: $, ele: $(ele).prop("outerHTML")}); // 性能贼差
  697. });
  698. }
  699. return result;
  700. },
  701. pd(html,parse,uri){
  702. return parseTags.jq.pdfh(html, parse, MY_URL);
  703. },
  704. },
  705. getParse(p0){//非js开头的情况自动获取解析标签
  706. if(p0.startsWith('jsp:')){
  707. return this.jsp
  708. }else if(p0.startsWith('json:')){
  709. return this.json
  710. }else if(p0.startsWith('jq:')){
  711. return this.jq
  712. }else {
  713. return this.jq
  714. }
  715. }
  716. };
  717. const stringify = JSON.stringify;
  718. const jsp = parseTags.jsp;
  719. const jq = parseTags.jq;
  720. /*** 后台需要实现的java方法并注入到js中 ***/
  721. /**
  722. * 读取本地文件->应用程序目录
  723. * @param filePath
  724. * @returns {string}
  725. */
  726. function readFile(filePath){
  727. filePath = filePath||'./uri.min.js';
  728. var fd = os.open(filePath);
  729. var buffer = new ArrayBuffer(1024);
  730. var len = os.read(fd, buffer, 0, 1024);
  731. console.log(len);
  732. let text = String.fromCharCode.apply(null, new Uint8Array(buffer));
  733. console.log(text);
  734. return text
  735. }
  736. /**
  737. * 处理返回的json数据
  738. * @param html
  739. * @returns {*}
  740. */
  741. function dealJson(html) {
  742. try {
  743. // html = html.match(/[\w|\W|\s|\S]*?(\{[\w|\W|\s|\S]*\})/).group[1];
  744. html = html.trim();
  745. if(!((html.startsWith('{') && html.endsWith('}'))||(html.startsWith('[') && html.endsWith(']')))){
  746. html = '{'+html.match(/.*?\{(.*)\}/m)[1]+'}';
  747. }
  748. } catch (e) {
  749. }
  750. try {
  751. html = JSON.parse(html);
  752. }catch (e) {}
  753. // console.log(typeof(html));
  754. return html;
  755. }
  756. /**
  757. * 验证码识别逻辑,需要java实现(js没有bytes类型,无法调用后端的传递图片二进制获取验证码文本的接口)
  758. * @type {{api: string, classification: (function(*=): string)}}
  759. */
  760. var OcrApi={
  761. api:OCR_API,
  762. classification:function (img){ // img是byte类型,这里不方便搞啊
  763. let code = '';
  764. try {
  765. let html = request(this.api,{data:{img:img},headers:{'User-Agent':PC_UA},'method':'POST'},true);
  766. html = JSON.parse(html);
  767. code = html.url||'';
  768. }catch (e) {}
  769. return code
  770. }
  771. };
  772. /**
  773. * 验证码识别,暂未实现
  774. * @param url 验证码图片链接
  775. * @returns {string} 验证成功后的cookie
  776. */
  777. function verifyCode(url){
  778. let cnt = 0;
  779. let host = getHome(url);
  780. let cookie = '';
  781. while (cnt < OCR_RETRY){
  782. try{
  783. // let obj = {headers:headers,timeout:timeout};
  784. let yzm_url = `${host}/index.php/verify/index.html`;
  785. console.log(`验证码链接:${yzm_url}`);
  786. let hhtml = request(yzm_url,{withHeaders:true,toBase64:true},true);
  787. let json = JSON.parse(hhtml);
  788. if(!cookie){
  789. // print(json);
  790. let setCk = Object.keys(json).find(it=>it.toLowerCase()==='set-cookie');
  791. // cookie = json['set-cookie']?json['set-cookie'].split(';')[0]:'';
  792. cookie = setCk?json[setCk].split(';')[0]:'';
  793. }
  794. // console.log(hhtml);
  795. console.log('cookie:'+cookie);
  796. let img = json.body;
  797. // console.log(img);
  798. let code = OcrApi.classification(img);
  799. console.log(`第${cnt+1}次验证码识别结果:${code}`);
  800. let submit_url = `${host}/index.php/ajax/verify_check?type=search&verify=${code}`;
  801. console.log(submit_url);
  802. let html = request(submit_url,{headers:{Cookie:cookie,'User-Agent':MOBILE_UA},'method':'POST'});
  803. // console.log(html);
  804. html = JSON.parse(html);
  805. if(html.msg === 'ok'){
  806. console.log(`第${cnt+1}次验证码提交成功`);
  807. return cookie // 需要返回cookie
  808. }else if(html.msg!=='ok'&&cnt+1>=OCR_RETRY){
  809. cookie = ''; // 需要清空返回cookie
  810. }
  811. }catch (e) {
  812. console.log(`第${cnt+1}次验证码提交失败:${e.message}`);
  813. if(cnt+1>=OCR_RETRY){
  814. cookie = '';
  815. }
  816. }
  817. cnt+=1
  818. }
  819. return cookie
  820. }
  821. /**
  822. * 存在数据库配置表里, key字段对应值value,没有就新增,有就更新,调用此方法会清除key对应的内存缓存
  823. * @param k 键
  824. * @param v 值
  825. */
  826. function setItem(k,v){
  827. local.set(RKEY,k,v);
  828. console.log(`规则${RKEY}设置${k} => ${v}`)
  829. }
  830. /**
  831. * 获取数据库配置表对应的key字段的value,没有这个key就返回value默认传参.需要有缓存,第一次获取后会存在内存里
  832. * @param k 键
  833. * @param v 值
  834. * @returns {*}
  835. */
  836. function getItem(k,v){
  837. return local.get(RKEY,k) || v;
  838. }
  839. /**
  840. * 删除数据库key对应的一条数据,并清除此key对应的内存缓存
  841. * @param k
  842. */
  843. function clearItem(k){
  844. local.delete(RKEY,k);
  845. }
  846. /*** js自封装的方法 ***/
  847. /**
  848. * 获取链接的host(带http协议的完整链接)
  849. * @param url 任意一个正常完整的Url,自动提取根
  850. * @returns {string}
  851. */
  852. function getHome(url){
  853. if(!url){
  854. return ''
  855. }
  856. let tmp = url.split('//');
  857. url = tmp[0] + '//' + tmp[1].split('/')[0];
  858. try {
  859. url = decodeURIComponent(url);
  860. }catch (e) {}
  861. return url
  862. }
  863. /**
  864. * get参数编译链接,类似python params字典自动拼接
  865. * @param url 访问链接
  866. * @param obj 参数字典
  867. * @returns {*}
  868. */
  869. function buildUrl(url,obj){
  870. obj = obj||{};
  871. if(url.indexOf('?')<0){
  872. url += '?'
  873. }
  874. let param_list = [];
  875. let keys = Object.keys(obj);
  876. keys.forEach(it=>{
  877. param_list.push(it+'='+obj[it])
  878. });
  879. let prs = param_list.join('&');
  880. if(keys.length > 0 && !url.endsWith('?')){
  881. url += '&'
  882. }
  883. url+=prs;
  884. return url
  885. }
  886. /**
  887. * 远程依赖执行函数
  888. * @param url 远程js地址
  889. */
  890. function require(url){
  891. eval(request(url));
  892. }
  893. /**
  894. * 海阔网页请求函数完整封装
  895. * @param url 请求链接
  896. * @param obj 请求对象 {headers:{},method:'',timeout:5000,body:'',withHeaders:false}
  897. * @param ocr_flag 标识此flag是用于请求ocr识别的,自动过滤content-type指定编码
  898. * @returns {string|string|DocumentFragment|*}
  899. */
  900. function request(url,obj,ocr_flag){
  901. ocr_flag = ocr_flag||false;
  902. if(typeof(obj)==='undefined'||!obj||obj==={}){
  903. if(!fetch_params||!fetch_params.headers){
  904. let headers = {
  905. 'User-Agent':MOBILE_UA,
  906. };
  907. if(rule.headers){
  908. Object.assign(headers,rule.headers);
  909. }
  910. if(!fetch_params){
  911. fetch_params = {};
  912. }
  913. fetch_params.headers = headers;
  914. }
  915. if(!fetch_params.headers.Referer){
  916. fetch_params.headers.Referer = getHome(url)
  917. }
  918. obj = fetch_params;
  919. }else{
  920. let headers = obj.headers||{};
  921. let keys = Object.keys(headers).map(it=>it.toLowerCase());
  922. if(!keys.includes('user-agent')){
  923. headers['User-Agent'] = MOBILE_UA;
  924. }if(!keys.includes('referer')){
  925. headers['Referer'] = getHome(url);
  926. }
  927. obj.headers = headers;
  928. }
  929. if(rule.encoding&&rule.encoding!=='utf-8'&&!ocr_flag){
  930. if(!obj.headers.hasOwnProperty('Content-Type')&&!obj.headers.hasOwnProperty('content-type')){ // 手动指定了就不管
  931. obj.headers["Content-Type"] = 'text/html; charset='+rule.encoding;
  932. }
  933. }
  934. if(typeof(obj.body)!='undefined'&&obj.body&&typeof (obj.body)==='string'){
  935. // let data = {};
  936. // obj.body.split('&').forEach(it=>{
  937. // data[it.split('=')[0]] = it.split('=')[1]
  938. // });
  939. // obj.data = data;
  940. // delete obj.body
  941. // 传body加 "Content-Type":"application/x-www-form-urlencoded;" 即可post form
  942. if(!obj.headers.hasOwnProperty('Content-Type')&&!obj.headers.hasOwnProperty('content-type')){ // 手动指定了就不管
  943. obj.headers["Content-Type"] = 'application/x-www-form-urlencoded; charset='+rule.encoding;
  944. }
  945. }else if(typeof(obj.body)!='undefined'&&obj.body&&typeof (obj.body)==='object'){
  946. obj.data = obj.body;
  947. delete obj.body
  948. }
  949. if(!url){
  950. return obj.withHeaders?'{}':''
  951. }
  952. if(obj.toBase64){ // 返回base64,用于请求图片
  953. obj.buffer = 2;
  954. delete obj.toBase64
  955. }
  956. console.log(JSON.stringify(obj.headers));
  957. // console.log('request:'+url+' obj:'+JSON.stringify(obj));
  958. console.log('request:'+url+`|method:${obj.method||'GET'}|body:${obj.body||''}`);
  959. let res = req(url, obj);
  960. let html = res.content||'';
  961. // console.log(html);
  962. if(obj.withHeaders){
  963. let htmlWithHeaders = res.headers;
  964. htmlWithHeaders.body = html;
  965. return JSON.stringify(htmlWithHeaders);
  966. }else{
  967. return html
  968. }
  969. }
  970. /**
  971. * 快捷post请求
  972. * @param url 地址
  973. * @param obj 对象
  974. * @returns {string|DocumentFragment|*}
  975. */
  976. function post(url,obj){
  977. obj.method = 'POST';
  978. return request(url,obj);
  979. }
  980. fetch = request;
  981. print = function (data){
  982. data = data||'';
  983. if(typeof(data)=='object'&&Object.keys(data).length>0){
  984. try {
  985. data = JSON.stringify(data);
  986. console.log(data);
  987. }catch (e) {
  988. // console.log('print:'+e.message);
  989. console.log(typeof(data)+':'+data.length);
  990. return
  991. }
  992. }else if(typeof(data)=='object'&&Object.keys(data).length<1){
  993. console.log('null object');
  994. }else{
  995. console.log(data);
  996. }
  997. }
  998. log = print;
  999. /**
  1000. * 检查宝塔验证并自动跳过获取正确源码
  1001. * @param html 之前获取的html
  1002. * @param url 之前的来源url
  1003. * @param obj 来源obj
  1004. * @returns {string|DocumentFragment|*}
  1005. */
  1006. function checkHtml(html,url,obj){
  1007. if(/\?btwaf=/.test(html)){
  1008. let btwaf = html.match(/btwaf(.*?)"/)[1];
  1009. url = url.split('#')[0]+'?btwaf'+btwaf;
  1010. print('宝塔验证访问链接:'+url);
  1011. html = request(url,obj);
  1012. }
  1013. return html
  1014. }
  1015. /**
  1016. * 带一次宝塔验证的源码获取
  1017. * @param url 请求链接
  1018. * @param obj 请求参数
  1019. * @returns {string|DocumentFragment}
  1020. */
  1021. function getCode(url,obj){
  1022. let html = request(url,obj);
  1023. html = checkHtml(html,url,obj);
  1024. return html
  1025. }
  1026. /**
  1027. * 源rule专用的请求方法,自动注入cookie
  1028. * @param url 请求链接
  1029. * @returns {string|DocumentFragment}
  1030. */
  1031. function getHtml(url){
  1032. let obj = {};
  1033. if(rule.headers){
  1034. obj.headers = rule.headers;
  1035. }
  1036. let cookie = getItem(RULE_CK,'');
  1037. if(cookie){
  1038. if(obj.headers && ! Object.keys(obj.headers).map(it=>it.toLowerCase()).includes('cookie')){
  1039. obj.headers['Cookie'] = cookie;
  1040. }else if(!obj.headers){
  1041. obj.headers = {Cookie:cookie};
  1042. }
  1043. }
  1044. let html = getCode(url,obj);
  1045. return html
  1046. }
  1047. /**
  1048. * 首页分类解析,筛选暂未实现
  1049. * @param homeObj 首页传参对象
  1050. * @returns {string}
  1051. */
  1052. function homeParse(homeObj) {
  1053. fetch_params = JSON.parse(JSON.stringify(rule_fetch_params));
  1054. let classes = [];
  1055. if (homeObj.class_name && homeObj.class_url) {
  1056. let names = homeObj.class_name.split('&');
  1057. let urls = homeObj.class_url.split('&');
  1058. let cnt = Math.min(names.length, urls.length);
  1059. for (let i = 0; i < cnt; i++) {
  1060. classes.push({
  1061. 'type_id': urls[i],
  1062. 'type_name': names[i]
  1063. });
  1064. }
  1065. }
  1066. if (homeObj.class_parse) {
  1067. let p = homeObj.class_parse.split(';');
  1068. let _ps = parseTags.getParse(p[0]);
  1069. _pdfa = _ps.pdfa;
  1070. _pdfh = _ps.pdfh;
  1071. _pd = _ps.pd;
  1072. MY_URL = rule.url;
  1073. if (p.length >= 3) { // 可以不写正则
  1074. try {
  1075. let html = getHtml(homeObj.MY_URL);
  1076. if (html) {
  1077. homeHtmlCache = html;
  1078. let list = _pdfa(html, p[0]);
  1079. if (list && list.length > 0) {
  1080. list.forEach((it,idex) => {
  1081. try {
  1082. let name = _pdfh(it, p[1]);
  1083. if (homeObj.cate_exclude && (new RegExp(homeObj.cate_exclude).test(name))) {
  1084. return;
  1085. }
  1086. // let url = pdfh(it, p[2]);
  1087. let url = _pd(it, p[2]);
  1088. if (p.length > 3 && p[3]) {
  1089. let exp = new RegExp(p[3]);
  1090. url = url.match(exp)[1];
  1091. }
  1092. classes.push({
  1093. 'type_id': url.trim(),
  1094. 'type_name': name.trim()
  1095. });
  1096. } catch (e) {
  1097. console.log(`分类列表定位第${idex}个元素正常报错:${e.message}`);
  1098. }
  1099. });
  1100. }
  1101. }
  1102. } catch (e) {
  1103. console.log(e.message);
  1104. }
  1105. }
  1106. }
  1107. // 排除分类
  1108. classes = classes.filter(it=>!homeObj.cate_exclude || !(new RegExp(homeObj.cate_exclude).test(it.type_name)));
  1109. let resp = {
  1110. 'class': classes
  1111. };
  1112. if(homeObj.filter){
  1113. resp.filters = homeObj.filter;
  1114. }
  1115. console.log(JSON.stringify(resp));
  1116. return JSON.stringify(resp);
  1117. }
  1118. /**
  1119. * 推荐和搜索单字段继承一级
  1120. * @param p 推荐或搜索的解析分割;列表
  1121. * @param pn 自身列表序号
  1122. * @param pp 一级解析分割;列表
  1123. * @param ppn 继承一级序号
  1124. * @returns {*}
  1125. */
  1126. function getPP(p, pn, pp, ppn){
  1127. try {
  1128. let ps = p[pn] === '*' && pp.length > ppn ?pp[ppn]:p[pn]
  1129. return ps
  1130. }catch (e) {
  1131. return ''
  1132. }
  1133. }
  1134. /**
  1135. * 首页推荐列表解析
  1136. * @param homeVodObj
  1137. * @returns {string}
  1138. */
  1139. function homeVodParse(homeVodObj){
  1140. fetch_params = JSON.parse(JSON.stringify(rule_fetch_params));
  1141. let d = [];
  1142. MY_URL = homeVodObj.homeUrl;
  1143. // setItem('MY_URL',MY_URL);
  1144. console.log(MY_URL);
  1145. let t1 = (new Date()).getTime();
  1146. let p = homeVodObj.推荐;
  1147. print('p:'+p);
  1148. if(p==='*' && rule.一级){
  1149. p = rule.一级;
  1150. homeVodObj.double = false;
  1151. }
  1152. if(!p||typeof(p)!=='string'){
  1153. return '{}'
  1154. }
  1155. p = p.trim();
  1156. let pp = rule.一级.split(';');
  1157. if(p.startsWith('js:')){
  1158. const TYPE = 'home';
  1159. var input = MY_URL;
  1160. const HOST = rule.host;
  1161. eval(p.replace('js:',''));
  1162. d = VODS;
  1163. }else {
  1164. p = p.split(';');
  1165. if (!homeVodObj.double && p.length < 5) {
  1166. return '{}'
  1167. } else if (homeVodObj.double && p.length < 6) {
  1168. return '{}'
  1169. }
  1170. let p0 = getPP(p,0,pp,0)
  1171. let _ps = parseTags.getParse(p0);
  1172. _pdfa = _ps.pdfa;
  1173. _pdfh = _ps.pdfh;
  1174. _pd = _ps.pd;
  1175. let is_json = p0.startsWith('json:');
  1176. p0 = p0.replace(/^(jsp:|json:|jq:)/,'');
  1177. // print(p[0]);
  1178. let html = homeHtmlCache || getHtml(MY_URL);
  1179. homeHtmlCache = undefined;
  1180. if(is_json){
  1181. // print('是json,开始处理');
  1182. html = dealJson(html);
  1183. }
  1184. try {
  1185. console.log('double:' + homeVodObj.double);
  1186. if (homeVodObj.double) {
  1187. let items = _pdfa(html, p0);
  1188. // console.log(items.length);
  1189. let p1 = getPP(p,1,pp,0);
  1190. let p2 = getPP(p,2,pp,1);
  1191. let p3 = getPP(p,3,pp,2);
  1192. let p4 = getPP(p,4,pp,3);
  1193. let p5 = getPP(p,5,pp,4);
  1194. let p6 = getPP(p,6,pp,5);
  1195. for (let item of items) {
  1196. // console.log(p[1]);
  1197. let items2 = _pdfa(item, p1);
  1198. // console.log(items2.length);
  1199. for (let item2 of items2) {
  1200. try {
  1201. let title = _pdfh(item2, p2);
  1202. let img = '';
  1203. try {
  1204. img = _pd(item2, p3);
  1205. } catch (e) {}
  1206. let desc = '';
  1207. try {
  1208. desc = _pdfh(item2, p4);
  1209. }catch (e) {}
  1210. let links = [];
  1211. for (let _p5 of p5.split('+')) {
  1212. let link = !homeVodObj.detailUrl ? _pd(item2, _p5, MY_URL) : _pdfh(item2, _p5);
  1213. links.push(link);
  1214. }
  1215. let content;
  1216. if(p.length > 6 && p[6]){
  1217. content = _pdfh(item2, p6);
  1218. } else{
  1219. content = '';
  1220. }
  1221. let vid = links.join('$');
  1222. if(rule.二级==='*'){
  1223. vid = vid+'@@'+title+'@@'+img;
  1224. }
  1225. let vod = {
  1226. vod_name: title,
  1227. vod_pic: img,
  1228. vod_remarks: desc,
  1229. vod_content: content,
  1230. vod_id: vid
  1231. };
  1232. // print(vod);
  1233. d.push(vod);
  1234. } catch (e) {
  1235. console.log('首页列表处理发生错误:'+e.message);
  1236. }
  1237. }
  1238. }
  1239. } else {
  1240. let items = _pdfa(html, p0);
  1241. let p1 = getPP(p,1,pp,1);
  1242. let p2 = getPP(p,2,pp,2);
  1243. let p3 = getPP(p,3,pp,3);
  1244. let p4 = getPP(p,4,pp,4);
  1245. let p5 = getPP(p,5,pp,5);
  1246. for (let item of items) {
  1247. try {
  1248. let title = _pdfh(item, p1);
  1249. let img = '';
  1250. try {
  1251. img = _pd(item, p2, MY_URL);
  1252. } catch (e) {}
  1253. let desc = '';
  1254. try {
  1255. desc = _pdfh(item, p3);
  1256. }catch (e) {}
  1257. let links = [];
  1258. for (let _p5 of p4.split('+')) {
  1259. let link = !homeVodObj.detailUrl ? _pd(item, _p5, MY_URL) : _pdfh(item, _p5);
  1260. links.push(link);
  1261. }
  1262. let content;
  1263. if(p.length > 5 && p[5]){
  1264. content = _pdfh(item, p5);
  1265. }else{
  1266. content = ''
  1267. }
  1268. let vid = links.join('$');
  1269. if(rule.二级==='*'){
  1270. vid = vid+'@@'+title+'@@'+img;
  1271. }
  1272. let vod = {
  1273. vod_name: title,
  1274. vod_pic: img,
  1275. vod_remarks: desc,
  1276. vod_content: content,
  1277. vod_id: vid
  1278. };
  1279. d.push(vod);
  1280. } catch (e) {
  1281. }
  1282. }
  1283. }
  1284. } catch (e) {
  1285. }
  1286. }
  1287. let t2 = (new Date()).getTime();
  1288. console.log('加载首页推荐耗时:'+(t2-t1)+'毫秒');
  1289. // console.log(JSON.stringify(d));
  1290. if(rule.图片来源){
  1291. d.forEach(it=>{
  1292. if(it.vod_pic&&it.vod_pic.startsWith('http')){
  1293. it.vod_pic = it.vod_pic + rule.图片来源;
  1294. }
  1295. });
  1296. }
  1297. if(d.length>0){
  1298. print(d.slice(0,2));
  1299. }
  1300. return JSON.stringify({
  1301. list:d
  1302. })
  1303. }
  1304. /**
  1305. * 一级分类页数据解析
  1306. * @param cateObj
  1307. * @returns {string}
  1308. */
  1309. function categoryParse(cateObj) {
  1310. fetch_params = JSON.parse(JSON.stringify(rule_fetch_params));
  1311. let p = cateObj.一级;
  1312. if(!p||typeof(p)!=='string'){
  1313. return '{}'
  1314. }
  1315. let d = [];
  1316. // let url = cateObj.url.replaceAll('fyclass', cateObj.tid).replaceAll('fypage', cateObj.pg);
  1317. let url = cateObj.url.replaceAll('fyclass', cateObj.tid);
  1318. if(rule.filter_url){
  1319. if(!/fyfilter/.test(url)){
  1320. if(!url.endsWith('&')&&!rule.filter_url.startsWith('&')){
  1321. url+='&'
  1322. }
  1323. url+=rule.filter_url;
  1324. }else{
  1325. url = url.replace('fyfilter', rule.filter_url);
  1326. }
  1327. // console.log('filter:'+cateObj.filter);
  1328. let fl = cateObj.filter?cateObj.extend:{};
  1329. // 自动合并 不同分类对应的默认筛选
  1330. if(rule.filter_def && typeof(rule.filter_def)==='object'){
  1331. try {
  1332. if(Object.keys(rule.filter_def).length>0 && rule.filter_def.hasOwnProperty(cateObj.tid)){
  1333. let self_fl_def = rule.filter_def[cateObj.tid];
  1334. if(self_fl_def && typeof(self_fl_def)==='object'){
  1335. // 引用传递转值传递,避免污染self变量
  1336. let fl_def = JSON.parse(JSON.stringify(self_fl_def));
  1337. fl = Object.assign(fl_def,fl);
  1338. }
  1339. }
  1340. }catch (e) {
  1341. print('合并不同分类对应的默认筛选出错:'+e.message);
  1342. }
  1343. }
  1344. let new_url;
  1345. new_url = cheerio.jinja2(url,{fl:fl});
  1346. // console.log('jinjia2执行后的new_url类型为:'+typeof(new_url));
  1347. if(/object Object/.test(new_url)){
  1348. new_url = drT.renderText(url,fl);
  1349. }
  1350. url = new_url;
  1351. }
  1352. if(/fypage/.test(url)){
  1353. if(url.includes('(')&&url.includes(')')){
  1354. let url_rep = url.match(/.*?\((.*)\)/)[1];
  1355. // console.log(url_rep);
  1356. let cnt_page = url_rep.replaceAll('fypage', cateObj.pg);
  1357. // console.log(cnt_page);
  1358. let cnt_pg = eval(cnt_page);
  1359. // console.log(cnt_pg);
  1360. url = url.replaceAll(url_rep,cnt_pg).replaceAll('(','').replaceAll(')','');
  1361. }else{
  1362. url = url.replaceAll('fypage',cateObj.pg);
  1363. }
  1364. }
  1365. if(cateObj.pg === 1 && url.includes('[')&&url.includes(']')){
  1366. url = url.split('[')[1].split(']')[0];
  1367. }else if(cateObj.pg > 1 && url.includes('[')&&url.includes(']')){
  1368. url = url.split('[')[0];
  1369. }
  1370. MY_URL = url;
  1371. // setItem('MY_URL',MY_URL);
  1372. console.log(MY_URL);
  1373. p = p.trim();
  1374. const MY_CATE = cateObj.tid;
  1375. if(p.startsWith('js:')){
  1376. var MY_FL = cateObj.extend;
  1377. const TYPE = 'cate';
  1378. var input = MY_URL;
  1379. const MY_PAGE = cateObj.pg;
  1380. var desc = '';
  1381. eval(p.trim().replace('js:',''));
  1382. d = VODS;
  1383. }else {
  1384. p = p.split(';');
  1385. if (p.length < 5) {
  1386. return '{}'
  1387. }
  1388. let _ps = parseTags.getParse(p[0]);
  1389. _pdfa = _ps.pdfa;
  1390. _pdfh = _ps.pdfh;
  1391. _pd = _ps.pd;
  1392. let is_json = p[0].startsWith('json:');
  1393. p[0] = p[0].replace(/^(jsp:|json:|jq:)/,'');
  1394. try {
  1395. let html = getHtml(MY_URL);
  1396. if (html) {
  1397. if(is_json){
  1398. html = dealJson(html);
  1399. }
  1400. let list = _pdfa(html, p[0]);
  1401. list.forEach(it => {
  1402. let links = p[4].split('+').map(p4=>{
  1403. return !rule.detailUrl?_pd(it, p4,MY_URL):_pdfh(it, p4);
  1404. });
  1405. let link = links.join('$');
  1406. let vod_id = rule.detailUrl?MY_CATE+'$'+link:link;
  1407. let vod_name = _pdfh(it, p[1]).replace(/\n|\t/g,'').trim();
  1408. let vod_pic = _pd(it, p[2],MY_URL);
  1409. if(rule.二级==='*'){
  1410. vod_id = vod_id+'@@'+vod_name+'@@'+vod_pic;
  1411. }
  1412. d.push({
  1413. 'vod_id': vod_id,
  1414. 'vod_name': vod_name,
  1415. 'vod_pic': vod_pic,
  1416. 'vod_remarks': _pdfh(it, p[3]).replace(/\n|\t/g,'').trim(),
  1417. });
  1418. });
  1419. }
  1420. } catch (e) {
  1421. console.log(e.message);
  1422. }
  1423. }
  1424. if(rule.图片来源){
  1425. d.forEach(it=>{
  1426. if(it.vod_pic&&it.vod_pic.startsWith('http')){
  1427. it.vod_pic = it.vod_pic + rule.图片来源;
  1428. }
  1429. });
  1430. }
  1431. // print(d);
  1432. if(d.length>0){
  1433. print(d.slice(0,2));
  1434. }
  1435. let pagecount = 0;
  1436. if(rule.pagecount && typeof(rule.pagecount) === 'object' && rule.pagecount.hasOwnProperty(MY_CATE)){
  1437. print(`MY_CATE:${MY_CATE},pagecount:${JSON.stringify(rule.pagecount)}`);
  1438. pagecount = parseInt(rule.pagecount[MY_CATE]);
  1439. }
  1440. let nodata = {
  1441. list:[{vod_name:'无数据,防无限请求',vod_id:'no_data',vod_remarks:'不要点,会崩的',vod_pic:'https://ghproxy.com/https://raw.githubusercontent.com/hjdhnx/dr_py/main/404.jpg'}],
  1442. total:1,pagecount:1,page:1,limit:1
  1443. };
  1444. let vod = d.length<1?JSON.stringify(nodata):JSON.stringify({
  1445. 'page': parseInt(cateObj.pg),
  1446. 'pagecount': pagecount||999,
  1447. 'limit': 20,
  1448. 'total': 999,
  1449. 'list': d,
  1450. });
  1451. // print(vod);
  1452. return vod
  1453. }
  1454. /**
  1455. * 搜索列表数据解析
  1456. * @param searchObj
  1457. * @returns {string}
  1458. */
  1459. function searchParse(searchObj) {
  1460. fetch_params = JSON.parse(JSON.stringify(rule_fetch_params));
  1461. let d = [];
  1462. if(!searchObj.searchUrl){
  1463. return '{}'
  1464. }
  1465. let p = searchObj.搜索==='*'&&rule.一级 ? rule.一级 : searchObj.搜索;
  1466. if(!p||typeof(p)!=='string'){
  1467. return '{}'
  1468. }
  1469. p = p.trim();
  1470. let pp = rule.一级.split(';');
  1471. let url = searchObj.searchUrl.replaceAll('**', searchObj.wd).replaceAll('fypage', searchObj.pg);
  1472. MY_URL = url;
  1473. console.log(MY_URL);
  1474. // log(searchObj.搜索);
  1475. // setItem('MY_URL',MY_URL);
  1476. if(p.startsWith('js:')){
  1477. const TYPE = 'search';
  1478. const MY_PAGE = searchObj.pg;
  1479. const KEY = searchObj.wd;
  1480. var input = MY_URL;
  1481. var detailUrl = rule.detailUrl||'';
  1482. eval(p.trim().replace('js:',''));
  1483. d = VODS;
  1484. }else{
  1485. p = p.split(';');
  1486. if (p.length < 5) {
  1487. return '{}'
  1488. }
  1489. let p0 = getPP(p,0,pp,0);
  1490. let _ps = parseTags.getParse(p0);
  1491. _pdfa = _ps.pdfa;
  1492. _pdfh = _ps.pdfh;
  1493. _pd = _ps.pd;
  1494. let is_json = p0.startsWith('json:');
  1495. p0 = p0.replace(/^(jsp:|json:|jq:)/,'');
  1496. // print('1381 p0:'+p0);
  1497. try {
  1498. let req_method = MY_URL.split(';').length>1?MY_URL.split(';')[1].toLowerCase():'get';
  1499. let html;
  1500. if(req_method==='post'){
  1501. let rurls = MY_URL.split(';')[0].split('#')
  1502. let rurl = rurls[0]
  1503. let params = rurls.length > 1 ?rurls[1]:'';
  1504. print(`post=》rurl:${rurl},params:${params}`);
  1505. // let new_dict = {};
  1506. // let new_tmp = params.split('&');
  1507. // new_tmp.forEach(i=>{
  1508. // new_dict[i.split('=')[0]] = i.split('=')[1];
  1509. // });
  1510. // html = post(rurl,{body:new_dict});
  1511. let _fetch_params = JSON.parse(JSON.stringify(rule_fetch_params));
  1512. let postData = {body:params};
  1513. Object.assign(_fetch_params,postData);
  1514. html = post(rurl,_fetch_params);
  1515. }else if(req_method==='postjson'){
  1516. let rurls = MY_URL.split(';')[0].split('#')
  1517. let rurl = rurls[0]
  1518. let params = rurls.length > 1 ?rurls[1]:'';
  1519. print(`postjson-》rurl:${rurl},params:${params}`);
  1520. try{
  1521. params = JSON.parse(params);
  1522. }catch (e) {
  1523. params = '{}'
  1524. }
  1525. let _fetch_params = JSON.parse(JSON.stringify(rule_fetch_params));
  1526. let postData = {body:params};
  1527. Object.assign(_fetch_params,postData);
  1528. html = post(rurl,_fetch_params);
  1529. }else{
  1530. html = getHtml(MY_URL);
  1531. }
  1532. if (html) {
  1533. if(/系统安全验证|输入验证码/.test(html)){
  1534. let cookie = verifyCode(MY_URL);
  1535. if(cookie){
  1536. console.log(`本次成功过验证,cookie:${cookie}`);
  1537. setItem(RULE_CK,cookie);
  1538. }else{
  1539. console.log(`本次自动过搜索验证失败,cookie:${cookie}`);
  1540. }
  1541. // obj.headers['Cookie'] = cookie;
  1542. html = getHtml(MY_URL);
  1543. }
  1544. if(!html.includes(searchObj.wd)){
  1545. console.log('搜索结果源码未包含关键字,疑似搜索失败,正为您打印结果源码');
  1546. console.log(html);
  1547. }
  1548. if(is_json){
  1549. // console.log(html);
  1550. html = dealJson(html);
  1551. // console.log(JSON.stringify(html));
  1552. }
  1553. // console.log(html);
  1554. let list = _pdfa(html, p0);
  1555. // print(list.length);
  1556. // print(list);
  1557. let p1 = getPP(p, 1, pp, 1);
  1558. let p2 = getPP(p, 2, pp, 2);
  1559. let p3 = getPP(p, 3, pp, 3);
  1560. let p4 = getPP(p, 4, pp, 4);
  1561. let p5 = getPP(p,5,pp,5);
  1562. list.forEach(it => {
  1563. let links = p4.split('+').map(_p4=>{
  1564. return !rule.detailUrl?_pd(it, _p4,MY_URL):_pdfh(it, _p4)
  1565. });
  1566. let link = links.join('$');
  1567. let content;
  1568. if(p.length > 5 && p[5]){
  1569. content = _pdfh(it, p5);
  1570. }else{
  1571. content = '';
  1572. }
  1573. let vod_id = link;
  1574. let vod_name = _pdfh(it, p1).replace(/\n|\t/g,'').trim();
  1575. let vod_pic = _pd(it, p2,MY_URL);
  1576. if(rule.二级==='*'){
  1577. vod_id = vod_id+'@@'+vod_name+'@@'+vod_pic;
  1578. }
  1579. let ob = {
  1580. 'vod_id': vod_id,
  1581. 'vod_name': vod_name,
  1582. 'vod_pic': vod_pic,
  1583. 'vod_remarks': _pdfh(it, p3).replace(/\n|\t/g,'').trim(),
  1584. 'vod_content': content.replace(/\n|\t/g,'').trim(),
  1585. };
  1586. d.push(ob);
  1587. });
  1588. }
  1589. } catch (e) {
  1590. print('搜索发生错误:'+e.message);
  1591. return '{}'
  1592. }
  1593. }
  1594. if(rule.图片来源){
  1595. d.forEach(it=>{
  1596. if(it.vod_pic&&it.vod_pic.startsWith('http')){
  1597. it.vod_pic = it.vod_pic + rule.图片来源;
  1598. }
  1599. });
  1600. }
  1601. // print(d);
  1602. return JSON.stringify({
  1603. 'page': parseInt(searchObj.pg),
  1604. 'pagecount': 10,
  1605. 'limit': 20,
  1606. 'total': 100,
  1607. 'list': d,
  1608. });
  1609. }
  1610. /**
  1611. * 二级详情页数据解析
  1612. * @param detailObj
  1613. * @returns {string}
  1614. */
  1615. function detailParse(detailObj){
  1616. let t1 = (new Date()).getTime();
  1617. fetch_params = JSON.parse(JSON.stringify(rule_fetch_params));
  1618. let orId = detailObj.orId;
  1619. let vod_name = '片名';
  1620. let vod_pic = '';
  1621. let vod_id = orId;
  1622. if(rule.二级==='*'){
  1623. // vod_id = orId.split('@@')[0]; // 千万不能分割
  1624. let extra = orId.split('@@');
  1625. vod_name = extra.length>1?extra[1]:vod_name;
  1626. vod_pic = extra.length>2?extra[2]:vod_pic;
  1627. }
  1628. // print(vod_pic);
  1629. let vod = {
  1630. vod_id: vod_id, //"id",
  1631. vod_name: vod_name,
  1632. vod_pic: vod_pic,
  1633. type_name: "类型",
  1634. vod_year: "年份",
  1635. vod_area: "地区",
  1636. vod_remarks: "更新信息",
  1637. vod_actor: "主演",
  1638. vod_director: "导演",
  1639. vod_content: "简介"
  1640. };
  1641. let p = detailObj.二级;
  1642. let url = detailObj.url;
  1643. let detailUrl = detailObj.detailUrl;
  1644. let fyclass = detailObj.fyclass;
  1645. let tab_exclude = detailObj.tab_exclude;
  1646. let html = detailObj.html||'';
  1647. MY_URL = url;
  1648. // console.log(MY_URL);
  1649. // setItem('MY_URL',MY_URL);
  1650. if(p==='*'){
  1651. vod.vod_play_from = '道长在线';
  1652. vod.vod_remarks = detailUrl;
  1653. vod.vod_actor = '没有二级,只有一级链接直接嗅探播放';
  1654. vod.vod_content = MY_URL;
  1655. vod.vod_play_url = '嗅探播放$' + MY_URL.split('@@')[0];
  1656. }else if(typeof(p)==='string'&&p.trim().startsWith('js:')){
  1657. const TYPE = 'detail';
  1658. var input = MY_URL;
  1659. var play_url = '';
  1660. eval(p.trim().replace('js:',''));
  1661. vod = VOD;
  1662. console.log(JSON.stringify(vod));
  1663. }else if(p&&typeof(p)==='object'){
  1664. let tt1 = (new Date()).getTime();
  1665. if(!html){
  1666. html = getHtml(MY_URL);
  1667. }
  1668. print(`二级${MY_URL}仅获取源码耗时:${(new Date()).getTime()-tt1}毫秒`);
  1669. let _impJQP = false;
  1670. let _ps;
  1671. if(p.is_json){
  1672. print('二级是json');
  1673. _ps = parseTags.json;
  1674. html = dealJson(html);
  1675. }else if(p.is_jsp){
  1676. print('二级是jsp');
  1677. _ps = parseTags.jsp;
  1678. }else if(p.is_jq){
  1679. print('二级是jq');
  1680. _ps = parseTags.jq;
  1681. }else{
  1682. print('二级默认jq');
  1683. _ps = parseTags.jq;
  1684. // print('二级默认jsp');
  1685. // _ps = parseTags.jsp;
  1686. }
  1687. if(_ps === parseTags.jq){ // jquery解析提前load(html)
  1688. _impJQP = true;
  1689. }
  1690. if (_impJQP) {
  1691. let ttt1 = (new Date()).getTime();
  1692. let c$ = cheerio.load(html);
  1693. // print(`二级${MY_URL}仅c$源码耗时:${(new Date()).getTime()-ttt1}毫秒`);
  1694. html = { rr: c$, ele: c$('html')[0] };
  1695. print(`二级${MY_URL}仅cheerio.load源码耗时:${(new Date()).getTime()-ttt1}毫秒`);
  1696. }
  1697. let tt2 = (new Date()).getTime();
  1698. print(`二级${MY_URL}获取并装载源码耗时:${tt2-tt1}毫秒`);
  1699. _pdfa = _ps.pdfa;
  1700. _pdfh = _ps.pdfh;
  1701. _pd = _ps.pd;
  1702. if(p.title){
  1703. let p1 = p.title.split(';');
  1704. vod.vod_name = _pdfh(html, p1[0]).replace(/\n|\t/g,'').trim();
  1705. let type_name = p1.length > 1 ? _pdfh(html, p1[1]).replace(/\n|\t/g,'').replace(/ /g,'').trim():'';
  1706. vod.type_name = type_name||vod.type_name;
  1707. }
  1708. if(p.desc){
  1709. try{
  1710. let p1 = p.desc.split(';');
  1711. vod.vod_remarks = _pdfh(html, p1[0]).replace(/\n|\t/g,'').trim();
  1712. vod.vod_year = p1.length > 1 ? _pdfh(html, p1[1]).replace(/\n|\t/g,'').trim():'';
  1713. vod.vod_area = p1.length > 2 ? _pdfh(html, p1[2]).replace(/\n|\t/g,'').trim():'';
  1714. // vod.vod_actor = p1.length > 3 ? _pdfh(html, p1[3]).replaceAll('\n', ' ').trim():'';
  1715. vod.vod_actor = p1.length > 3 ? _pdfh(html, p1[3]).replace(/\n|\t/g,'').trim():'';
  1716. vod.vod_director = p1.length > 4 ? _pdfh(html, p1[4]).replace(/\n|\t/g,'').trim():'';
  1717. }
  1718. catch (e) {
  1719. }
  1720. }
  1721. if(p.content){
  1722. try{
  1723. let p1 = p.content.split(';');
  1724. vod.vod_content = _pdfh(html, p1[0]).replace(/\n|\t/g,'').trim();
  1725. }
  1726. catch (e) {}
  1727. }
  1728. if(p.img){
  1729. try{
  1730. let p1 = p.img.split(';');
  1731. vod.vod_pic = _pd(html, p1[0],MY_URL);
  1732. }
  1733. catch (e) {}
  1734. }
  1735. let vod_play_from = '$$$';
  1736. let playFrom = [];
  1737. if(p.重定向&&p.重定向.startsWith('js:')){
  1738. print('开始执行重定向代码:'+p.重定向);
  1739. html = eval(p.重定向.replace('js:',''));
  1740. if (_impJQP) {
  1741. let c$ = cheerio.load(html);
  1742. html = { rr: c$, ele: c$('html')[0] }
  1743. }
  1744. }
  1745. // console.log(2);
  1746. if(p.tabs){
  1747. if(p.tabs.startsWith('js:')){
  1748. print('开始执行tabs代码:'+p.tabs);
  1749. if(html&&_impJQP&&typeof (html)!=='string'){
  1750. try { // 假装是jq的对象拿来转换一下字符串,try为了防止json的情况报错
  1751. html = html.rr(html.ele).toString();
  1752. }catch (e) {}
  1753. }
  1754. var input = MY_URL;
  1755. eval(p.tabs.replace('js:',''));
  1756. playFrom = TABS;
  1757. }else{
  1758. let p_tab = p.tabs.split(';')[0];
  1759. // console.log(p_tab);
  1760. let vHeader = _pdfa(html, p_tab);
  1761. console.log(vHeader.length);
  1762. let tab_text = p.tab_text||'body&&Text';
  1763. // print('tab_text:'+tab_text);
  1764. let new_map = {};
  1765. for(let v of vHeader){
  1766. let v_title = _pdfh(v,tab_text).trim();
  1767. console.log(v_title);
  1768. if(tab_exclude&& (new RegExp(tab_exclude)).test(v_title)){
  1769. continue;
  1770. }
  1771. if(!new_map.hasOwnProperty(v_title)){
  1772. new_map[v_title] = 1;
  1773. }else{
  1774. new_map[v_title] += 1;
  1775. }
  1776. if(new_map[v_title]>1){
  1777. v_title+=Number(new_map[v_title]-1);
  1778. }
  1779. playFrom.push(v_title);
  1780. }
  1781. }
  1782. console.log(JSON.stringify(playFrom));
  1783. }else{
  1784. playFrom = ['道长在线']
  1785. }
  1786. vod.vod_play_from = playFrom.join(vod_play_from);
  1787. // console.log(3);
  1788. let vod_play_url = '$$$';
  1789. let vod_tab_list = [];
  1790. if(p.lists){
  1791. if(p.lists.startsWith('js:')){
  1792. print('开始执行lists代码:'+p.lists);
  1793. try {
  1794. if(html&&_impJQP&&typeof (html)!=='string'){
  1795. // 假装是jq的对象拿来转换一下字符串,try为了防止json的情况报错
  1796. try {
  1797. html = html.rr(html.ele).toString();
  1798. }catch (e) {}
  1799. }
  1800. var input = MY_URL;
  1801. var play_url = '';
  1802. eval(p.lists.replace('js:',''));
  1803. for(let i in LISTS){
  1804. if(LISTS.hasOwnProperty(i)){
  1805. // print(i);
  1806. try {
  1807. LISTS[i] = LISTS[i].map(it=>it.split('$').slice(0,2).join('$'));
  1808. }catch (e) {
  1809. print('格式化LISTS发生错误:'+e.message);
  1810. }
  1811. }
  1812. }
  1813. vod_play_url = LISTS.map(it=>it.join('#')).join(vod_play_url);
  1814. }catch (e) {
  1815. print('js执行lists: 发生错误:'+e.message);
  1816. }
  1817. }else{
  1818. let list_text = p.list_text||'body&&Text';
  1819. let list_url = p.list_url||'a&&href';
  1820. // print('list_text:'+list_text);
  1821. // print('list_url:'+list_url);
  1822. // print('list_parse:'+p.lists);
  1823. let is_tab_js = p.tabs.trim().startsWith('js:');
  1824. for(let i=0;i<playFrom.length;i++){
  1825. let tab_name = playFrom[i];
  1826. let tab_ext = p.tabs.split(';').length > 1 && !is_tab_js ? p.tabs.split(';')[1] : '';
  1827. let p1 = p.lists.replaceAll('#idv', tab_name).replaceAll('#id', i);
  1828. tab_ext = tab_ext.replaceAll('#idv', tab_name).replaceAll('#id', i);
  1829. // 测试jsp提速
  1830. // console.log(p1);
  1831. // p1 = p1.replace(':eq(0)',',0').replace(' ','&&');
  1832. // console.log(p1);
  1833. // console.log(html);
  1834. let vodList = [];
  1835. try {
  1836. vodList = _pdfa(html, p1);
  1837. console.log('len(vodList):'+vodList.length);
  1838. }catch (e) {
  1839. // console.log(e.message);
  1840. }
  1841. let new_vod_list = [];
  1842. // print('tab_ext:'+tab_ext);
  1843. let tabName = tab_ext?_pdfh(html, tab_ext):tab_name;
  1844. console.log(tabName);
  1845. // console.log('cheerio解析Text');
  1846. // 此处存在性能问题: pt版2000集需要650毫秒,俊版1300毫秒 特么的优化不动 主要后面定位url的我拿他没法
  1847. // 主要性能问题在于 _pd(it, list_url, MY_URL)
  1848. let tt1 = (new Date()).getTime();
  1849. // vodList.forEach((it,idex)=>{
  1850. // // 请注意,这里要固定pdfh解析body&&Text,不需要下划线,没写错
  1851. // // new_vod_list.push(pdfh(it,'body&&Text')+'$'+_pd(it,'a&&href',MY_URL));
  1852. // // new_vod_list.push(cheerio.load(it).text()+'$'+_pd(it,'a&&href',MY_URL));
  1853. // // new_vod_list.push(_pdfh(it, list_text).trim() + '$' + _pd(it, list_url, MY_URL));
  1854. // // new_vod_list.push(_pdfh(it, list_text).trim() + '$' +idex);
  1855. // // new_vod_list.push(idex + '$' +_pdfh(it, list_url));
  1856. // new_vod_list.push(_pdfh(it, list_text).trim() + '$' +_pd(it, list_url,MY_URL));
  1857. // });
  1858. if(vodList.length>0){
  1859. for(let i=0;i<vodList.length;i++){
  1860. let it = vodList[i];
  1861. new_vod_list.push(_pdfh(it, list_text).trim() + '$' +_pd(it, list_url,MY_URL));
  1862. }
  1863. new_vod_list = forceOrder(new_vod_list,'',x=>x.split('$')[0]);
  1864. console.log(`drpy影响性能代码共计列表数循环次数:${vodList.length},耗时:${(new Date()).getTime()-tt1}毫秒`);
  1865. }
  1866. let vlist = new_vod_list.join('#');
  1867. vod_tab_list.push(vlist);
  1868. }
  1869. vod_play_url = vod_tab_list.join(vod_play_url);
  1870. }
  1871. }
  1872. vod.vod_play_url = vod_play_url;
  1873. }
  1874. if(rule.图片来源 && vod.vod_pic && vod.vod_pic.startsWith('http')){
  1875. vod.vod_pic = vod.vod_pic + rule.图片来源;
  1876. }
  1877. if(!vod.vod_id){
  1878. vod.vod_id = vod_id;
  1879. }
  1880. let t2 = (new Date()).getTime();
  1881. console.log(`加载二级界面${MY_URL}耗时:${t2-t1}毫秒`);
  1882. // print(vod);
  1883. return JSON.stringify({
  1884. list: [vod]
  1885. })
  1886. }
  1887. /**
  1888. * 判断是否需要解析
  1889. * @param url
  1890. * @returns {number|number}
  1891. */
  1892. function tellIsJx(url){
  1893. try {
  1894. let is_vip = !/\.(m3u8|mp4|m4a)$/.test(url.split('?')[0]) && 是否正版(url);
  1895. return is_vip?1:0
  1896. }catch (e) {
  1897. return 1
  1898. }
  1899. }
  1900. /**
  1901. * 选集播放点击事件解析
  1902. * @param playObj
  1903. * @returns {string}
  1904. */
  1905. function playParse(playObj){
  1906. fetch_params = JSON.parse(JSON.stringify(rule_fetch_params));
  1907. MY_URL = playObj.url;
  1908. if(!/http/.test(MY_URL)){
  1909. try {
  1910. MY_URL = base64Decode(MY_URL);
  1911. }catch (e) {}
  1912. }
  1913. MY_URL = decodeURIComponent(MY_URL);
  1914. var input = MY_URL;//注入给免嗅js
  1915. let common_play = {
  1916. parse:1,
  1917. url:input,
  1918. // url:urlencode(input),
  1919. jx:tellIsJx(input)
  1920. };
  1921. let lazy_play;
  1922. if(!rule.play_parse||!rule.lazy){
  1923. lazy_play = common_play;
  1924. }else if(rule.play_parse&&rule.lazy&&typeof(rule.lazy)==='string'){
  1925. try {
  1926. let lazy_code = rule.lazy.replace('js:','').trim();
  1927. print('开始执行js免嗅=>'+lazy_code);
  1928. eval(lazy_code);
  1929. lazy_play = typeof(input) === 'object'?input:{
  1930. parse:1,
  1931. jx:tellIsJx(input),
  1932. url:input
  1933. };
  1934. }catch (e) {
  1935. print('js免嗅错误:'+e.message);
  1936. lazy_play = common_play;
  1937. }
  1938. }else{
  1939. lazy_play = common_play;
  1940. }
  1941. // print('play_json:'+typeof(rule.play_json));
  1942. // console.log(Array.isArray(rule.play_json));
  1943. if(Array.isArray(rule.play_json) && rule.play_json.length >0){ // 数组情况判断长度大于0
  1944. let web_url = lazy_play.url;
  1945. for(let pjson of rule.play_json){
  1946. if(pjson.re && (pjson.re==='*'||web_url.match(new RegExp(pjson.re)))){
  1947. if(pjson.json && typeof(pjson.json)==='object'){
  1948. let base_json = pjson.json;
  1949. // print('开始合并:');
  1950. // print(base_json);
  1951. lazy_play = Object.assign(lazy_play,base_json);
  1952. break;
  1953. }
  1954. }
  1955. }
  1956. }else if(rule.play_json && !Array.isArray(rule.play_json)){ // 其他情况 非[] 判断true/false
  1957. let base_json = {
  1958. jx:1,
  1959. parse:1,
  1960. };
  1961. lazy_play = Object.assign(lazy_play,base_json);
  1962. }else if(!rule.play_json){ // 不解析传0
  1963. let base_json = {
  1964. jx:0,
  1965. parse:1,
  1966. };
  1967. lazy_play = Object.assign(lazy_play,base_json);
  1968. }
  1969. console.log(JSON.stringify(lazy_play));
  1970. return JSON.stringify(lazy_play);
  1971. }
  1972. /**
  1973. * js源预处理特定返回对象中的函数
  1974. * @param ext
  1975. */
  1976. function init(ext) {
  1977. console.log('init');
  1978. try {
  1979. // make shared jsContext happy muban不能import,不然会造成换源继承后变量被篡改
  1980. // if (typeof (globalThis.mubanJs) === 'undefined') {
  1981. // let mubanJs = request('https://gitcode.net/qq_32394351/dr_py/-/raw/master/js/模板.js', { 'User-Agent': MOBILE_UA });
  1982. // mubanJs = mubanJs.replace('export default', '(function() {return muban;}()) // export default');
  1983. // // console.log(mubanJs);
  1984. // globalThis.mubanJs = mubanJs;
  1985. // }
  1986. // let muban = eval(globalThis.mubanJs);
  1987. let muban = 模板.getMubans();
  1988. if (typeof ext == 'object'){
  1989. rule = ext;
  1990. } else if (typeof ext == 'string') {
  1991. if (ext.startsWith('http')) {
  1992. let js = request(ext,{'method':'GET'});
  1993. if (js){
  1994. eval(js.replace('var rule', 'rule'));
  1995. }
  1996. } else {
  1997. eval(ext.replace('var rule', 'rule'));
  1998. }
  1999. }
  2000. if (rule.模板 && muban.hasOwnProperty(rule.模板)) {
  2001. print('继承模板:'+rule.模板);
  2002. rule = Object.assign(muban[rule.模板], rule);
  2003. }
  2004. /** 处理一下 rule规则关键字段没传递的情况 **/
  2005. let rule_cate_excludes = (rule.cate_exclude||'').split('|').filter(it=>it.trim());
  2006. let rule_tab_excludes = (rule.tab_exclude||'').split('|').filter(it=>it.trim());
  2007. rule_cate_excludes = rule_cate_excludes.concat(CATE_EXCLUDE.split('|').filter(it=>it.trim()));
  2008. rule_tab_excludes = rule_tab_excludes.concat(TAB_EXCLUDE.split('|').filter(it=>it.trim()));
  2009. rule.cate_exclude = rule_cate_excludes.join('|');
  2010. rule.tab_exclude = rule_tab_excludes.join('|');
  2011. rule.host = (rule.host||'').rstrip('/');
  2012. rule.url = rule.url||'';
  2013. rule.double = rule.double||false;
  2014. rule.homeUrl = rule.homeUrl||'';
  2015. rule.detailUrl = rule.detailUrl||'';
  2016. rule.searchUrl = rule.searchUrl||'';
  2017. rule.homeUrl = rule.host&&rule.homeUrl?urljoin(rule.host,rule.homeUrl):(rule.homeUrl||rule.host);
  2018. rule.detailUrl = rule.host&&rule.detailUrl?urljoin(rule.host,rule.detailUrl):rule.detailUrl;
  2019. if(rule.url.includes('[')&&rule.url.includes(']')){
  2020. let u1 = rule.url.split('[')[0]
  2021. let u2 = rule.url.split('[')[1].split(']')[0]
  2022. rule.url = rule.host && rule.url?urljoin(rule.host,u1)+'['+urljoin(rule.host,u2)+']':rule.url;
  2023. }else{
  2024. rule.url = rule.host && rule.url ? urljoin(rule.host,rule.url) : rule.url;
  2025. }
  2026. rule.searchUrl = rule.host && rule.searchUrl ? urljoin(rule.host,rule.searchUrl) : rule.searchUrl;
  2027. rule.timeout = rule.timeout||5000;
  2028. rule.encoding = rule.编码||rule.encoding||'utf-8';
  2029. rule.图片来源 = rule.图片来源||'';
  2030. rule.play_json = rule.hasOwnProperty('play_json')?rule.play_json:[];
  2031. rule.pagecount = rule.hasOwnProperty('pagecount')?rule.pagecount:{};
  2032. if(rule.headers && typeof(rule.headers) === 'object'){
  2033. try {
  2034. let header_keys = Object.keys(rule.headers);
  2035. for(let k of header_keys){
  2036. if(k.toLowerCase() === 'user-agent'){
  2037. let v = rule.headers[k];
  2038. console.log(v);
  2039. if(['MOBILE_UA','PC_UA','UC_UA','IOS_UA','UA'].includes(v)){
  2040. rule.headers[k] = eval(v);
  2041. }
  2042. }
  2043. }
  2044. }catch (e) {
  2045. console.log('处理headers发生错误:'+e.message);
  2046. }
  2047. }
  2048. // print(rule.headers);
  2049. rule_fetch_params = {'headers': rule.headers||false, 'timeout': rule.timeout, 'encoding': rule.encoding};
  2050. oheaders = rule.headers||{};
  2051. RKEY = typeof(key)!=='undefined'&&key?key:'drpy_' + (rule.title || rule.host);
  2052. pre(); // 预处理
  2053. init_test();
  2054. }catch (e) {
  2055. console.log('init_test发生错误:'+e.message);
  2056. }
  2057. }
  2058. let homeHtmlCache = undefined;
  2059. /**
  2060. * js源获取首页分类和筛选特定返回对象中的函数
  2061. * @param filter 筛选条件字典对象
  2062. * @returns {string}
  2063. */
  2064. function home(filter) {
  2065. console.log("home");
  2066. let homeObj = {
  2067. filter:rule.filter||false,
  2068. MY_URL: rule.homeUrl,
  2069. class_name: rule.class_name || '',
  2070. class_url: rule.class_url || '',
  2071. class_parse: rule.class_parse || '',
  2072. cate_exclude: rule.cate_exclude,
  2073. };
  2074. return homeParse(homeObj);
  2075. }
  2076. /**
  2077. * js源获取首页推荐数据列表特定返回对象中的函数
  2078. * @param params
  2079. * @returns {string}
  2080. */
  2081. function homeVod(params) {
  2082. console.log("homeVod");
  2083. let homeVodObj = {
  2084. 推荐:rule.推荐,
  2085. double:rule.double,
  2086. homeUrl:rule.homeUrl,
  2087. detailUrl:rule.detailUrl
  2088. };
  2089. return homeVodParse(homeVodObj)
  2090. // return "{}";
  2091. }
  2092. /**
  2093. * js源获取分类页一级数据列表特定返回对象中的函数
  2094. * @param tid 分类id
  2095. * @param pg 页数
  2096. * @param filter 当前选中的筛选条件
  2097. * @param extend 扩展
  2098. * @returns {string}
  2099. */
  2100. function category(tid, pg, filter, extend) {
  2101. let cateObj = {
  2102. url: rule.url,
  2103. 一级: rule.一级,
  2104. tid: tid,
  2105. pg: parseInt(pg),
  2106. filter: filter,
  2107. extend: extend
  2108. };
  2109. // console.log(JSON.stringify(extend));
  2110. return categoryParse(cateObj)
  2111. }
  2112. /**
  2113. * js源获取二级详情页数据特定返回对象中的函数
  2114. * @param vod_url 一级列表中的vod_id或者是带分类的自拼接 vod_id 如 fyclass$vod_id
  2115. * @returns {string}
  2116. */
  2117. function detail(vod_url) {
  2118. let orId = vod_url;
  2119. let fyclass = '';
  2120. if(vod_url.indexOf('$')>-1){
  2121. let tmp = vod_url.split('$');
  2122. fyclass = tmp[0];
  2123. vod_url = tmp[1];
  2124. }
  2125. let detailUrl = vod_url.split('@@')[0];
  2126. let url;
  2127. if(!detailUrl.startsWith('http')&&!detailUrl.includes('/')){
  2128. url = rule.detailUrl.replaceAll('fyid', detailUrl).replaceAll('fyclass',fyclass);
  2129. }else if(detailUrl.includes('/')){
  2130. url = urljoin(rule.homeUrl,detailUrl);
  2131. }else{
  2132. url = detailUrl
  2133. }
  2134. let detailObj = {
  2135. orId: orId,
  2136. url:url,
  2137. 二级:rule.二级,
  2138. detailUrl:detailUrl,
  2139. fyclass:fyclass,
  2140. tab_exclude:rule.tab_exclude,
  2141. }
  2142. return detailParse(detailObj)
  2143. }
  2144. /**
  2145. * js源选集按钮播放点击事件特定返回对象中的函数
  2146. * @param flag 线路名
  2147. * @param id 播放按钮的链接
  2148. * @param flags 全局配置的flags是否需要解析的标识列表
  2149. * @returns {string}
  2150. */
  2151. function play(flag, id, flags) {
  2152. let playObj = {
  2153. url:id,
  2154. flag:flag,
  2155. flags:flags
  2156. }
  2157. return playParse(playObj);
  2158. }
  2159. /**
  2160. * js源搜索返回的数据列表特定返回对象中的函数
  2161. * @param wd 搜索关键字
  2162. * @param quick 是否来自快速搜索
  2163. * @returns {string}
  2164. */
  2165. function search(wd, quick) {
  2166. let searchObj = {
  2167. searchUrl: rule.searchUrl,
  2168. 搜索: rule.搜索,
  2169. wd: wd,
  2170. //pg: pg,
  2171. pg: 1,
  2172. quick: quick,
  2173. };
  2174. // console.log(JSON.stringify(searchObj));
  2175. return searchParse(searchObj)
  2176. }
  2177. function DRPY(){//导出函数
  2178. return {
  2179. init: init,
  2180. home: home,
  2181. homeVod: homeVod,
  2182. category: category,
  2183. detail: detail,
  2184. play: play,
  2185. search: search,
  2186. }
  2187. }
  2188. // 导出函数对象
  2189. export default {
  2190. init: init,
  2191. home: home,
  2192. homeVod: homeVod,
  2193. category: category,
  2194. detail: detail,
  2195. play: play,
  2196. search: search,
  2197. DRPY:DRPY
  2198. }