drpy.js 90 KB

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