drpy.js 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742
  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 'http://192.168.10.103:5705/libs/drT.js';
  9. // import muban from 'https://gitcode.net/qq_32394351/dr_py/-/raw/master/js/模板.js';
  10. // import muban from 'http://192.168.10.103:5705/admin/view/模板.js';
  11. // const key = 'drpy_zbk';
  12. // eval(req('http://192.168.1.124:5705/libs/es6py.js').content);
  13. function init_test(){
  14. // console.log(typeof(CryptoJS));
  15. console.log("init_test_start");
  16. console.log(RKEY);
  17. console.log(JSON.stringify(rule));
  18. // console.log('123456的md5值是:'+md5('123456'));
  19. // let aa = base64Encode('编码测试一下')
  20. // log(aa);
  21. // let bb = base64Decode(aa);
  22. // log('bb:'+bb);
  23. // clearItem(RULE_CK);
  24. // console.log(JSON.stringify(rule));
  25. // console.log(request('https://www.baidu.com',{withHeaders:true}));
  26. // console.log(request('https://www.baidu.com/favicon.ico',{toBase64:true}));
  27. // require('http://192.168.10.99:5705/txt/pluto/drT.js');
  28. // console.log(typeof(drT));
  29. // console.log(drT.renderText('{{fl.cate}},hi, {{fl}}哈哈.{{fl}}',{sort: 1,cate:'movie'},'fl'));
  30. console.log("init_test_end");
  31. }
  32. let rule = {};
  33. /** 已知问题记录
  34. * 1.影魔的jinjia2引擎不支持 {{fl}}对象直接渲染 (有能力解决的话尽量解决下,支持对象直接渲染字符串转义,如果加了|safe就不转义)[影魔牛逼,最新的文件发现这问题已经解决了]
  35. * Array.prototype.append = Array.prototype.push; 这种js执行后有毛病,for in 循环列表会把属性给打印出来 (这个大毛病需要重点排除一下)
  36. * 2.import es6py.js但是里面的函数没有被装载进来.比如drpy规则报错setResult2 is undefiend(合并文件了可以不管了)
  37. * 3.无法重复导入cheerio(怎么解决drpy和parseTag里都需要导入cheerio的问题) 无法在副文件导入cheerio (现在是全部放在drpy一个文件里了,凑合解决?)
  38. * 4.有个错误不知道哪儿来的 executeScript: com.quickjs.JSObject$Undefined cannot be cast to java.lang.String 在 点击选集播放打印init_test_end后面打印(貌似不影响使用)
  39. * 5.需要实现 stringify 函数,比起JSON.stringify函数,它会原封不动保留中文不会编码unicode
  40. * 6.base64Encode,base64Decode,md5函数还没有实现 (抄影魔代码实现了)
  41. * 7.eval(getCryptoJS());还没有实现 (可以空实现了,以后遇到能忽略)
  42. * done: jsp:{pdfa,pdfh,pd},json:{pdfa,pdfh,pd},jq:{pdfa,pdfh,pd}
  43. * * 电脑看日志调试
  44. adb tcpip 5555
  45. adb connect 192.168.10.192
  46. adb devices -l
  47. adb logcat -c
  48. adb logcat | grep -i QuickJS
  49. * **/
  50. /*** 以下是内置变量和解析方法 **/
  51. 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';
  52. 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';
  53. const UA = 'Mozilla/5.0';
  54. 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';
  55. 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';
  56. const RULE_CK = 'cookie'; // 源cookie的key值
  57. // const KEY = typeof(key)!=='undefined'&&key?key:'drpy_' + (rule.title || rule.host); // 源的唯一标识
  58. const CATE_EXCLUDE = '首页|留言|APP|下载|资讯|新闻|动态';
  59. const TAB_EXCLUDE = '猜你|喜欢|APP|下载|剧情|热播';
  60. const OCR_RETRY = 3;//ocr验证重试次数
  61. // const OCR_API = 'http://dm.mudery.com:10000';//ocr在线识别接口
  62. // const OCR_API = 'http://192.168.3.239:5705/parse/ocr';//ocr在线识别接口
  63. const OCR_API = 'http://cms.nokia.press/parse/ocr';//ocr在线识别接口
  64. if(typeof(MY_URL)==='undefined'){
  65. var MY_URL; // 全局注入变量,pd函数需要
  66. }
  67. var RKEY; // 源的唯一标识
  68. var fetch;
  69. var print;
  70. var log;
  71. var rule_fetch_params;
  72. var fetch_params; // 每个位置单独的
  73. var oheaders;
  74. var _pdfh;
  75. var _pdfa;
  76. var _pd;
  77. // const DOM_CHECK_ATTR = ['url', 'src', 'href', 'data-original', 'data-src'];
  78. const DOM_CHECK_ATTR = /(url|src|href|data-original|data-src)$/;
  79. const SELECT_REGEX = /:eq|:lt|:gt|#/g;
  80. const SELECT_REGEX_A = /:eq|:lt|:gt/g;
  81. /**
  82. es6py扩展
  83. */
  84. if (typeof Object.assign != 'function') {
  85. Object.assign = function () {
  86. var target = arguments[0];
  87. for (var i = 1; i < arguments.length; i++) {
  88. var source = arguments[i];
  89. for (var key in source) {
  90. if (Object.prototype.hasOwnProperty.call(source, key)) {
  91. target[key] = source[key];
  92. }
  93. }
  94. }
  95. return target;
  96. };
  97. }
  98. if (!String.prototype.includes) {
  99. String.prototype.includes = function (search, start) {
  100. if (typeof start !== 'number') {
  101. start = 0;
  102. }
  103. if (start + search.length > this.length) {
  104. return false;
  105. } else {
  106. return this.indexOf(search, start) !== -1;
  107. }
  108. };
  109. }
  110. if (!Array.prototype.includes) {
  111. Object.defineProperty(Array.prototype, 'includes', {
  112. value: function (searchElement, fromIndex) {
  113. if (this == null) {//this是空或者未定义,抛出错误
  114. throw new TypeError('"this" is null or not defined');
  115. }
  116. var o = Object(this);//将this转变成对象
  117. var len = o.length >>> 0;//无符号右移0位,获取对象length属性,如果未定义就会变成0
  118. if (len === 0) {//length为0直接返回false未找到目标值
  119. return false;
  120. }
  121. var n = fromIndex | 0;//查找起始索引
  122. var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);//计算正确起始索引,因为有可能是负值
  123. while (k < len) {//从起始索引处开始循环
  124. if (o[k] === searchElement) {//如果某一位置与寻找目标相等,返回true,找到了
  125. return true;
  126. }
  127. k++;
  128. }
  129. return false;//未找到,返回false
  130. }
  131. });
  132. }
  133. if (typeof String.prototype.startsWith != 'function') {
  134. String.prototype.startsWith = function (prefix){
  135. return this.slice(0, prefix.length) === prefix;
  136. };
  137. }
  138. if (typeof String.prototype.endsWith != 'function') {
  139. String.prototype.endsWith = function(suffix) {
  140. return this.indexOf(suffix, this.length - suffix.length) !== -1;
  141. };
  142. }
  143. Object.prototype.myValues=function(obj){
  144. if(obj ==null) {
  145. throw new TypeError("Cannot convert undefined or null to object");
  146. }
  147. var res=[]
  148. for(var k in obj){
  149. if(obj.hasOwnProperty(k)){//需判断是否是本身的属性
  150. res.push(obj[k]);
  151. }
  152. }
  153. return res;
  154. }
  155. if (typeof Object.prototype.values != 'function') {
  156. Object.prototype.values=function(obj){
  157. if(obj ==null) {
  158. throw new TypeError("Cannot convert undefined or null to object");
  159. }
  160. var res=[]
  161. for(var k in obj){
  162. if(obj.hasOwnProperty(k)){//需判断是否是本身的属性
  163. res.push(obj[k]);
  164. }
  165. }
  166. return res;
  167. }
  168. }
  169. if (typeof Array.prototype.join != 'function') {
  170. Array.prototype.join = function (emoji) {
  171. // emoji = emoji||',';
  172. emoji = emoji||'';
  173. let self = this;
  174. let str = "";
  175. let i = 0;
  176. if (!Array.isArray(self)) {throw String(self)+'is not Array'}
  177. if(self.length===0){return ''}
  178. if (self.length === 1){return String(self[0])}
  179. i = 1;
  180. str = this[0];
  181. for (; i < self.length; i++) {
  182. str += String(emoji)+String(self[i]);
  183. }
  184. return str;
  185. };
  186. }
  187. String.prototype.rstrip = function (chars) {
  188. let regex = new RegExp(chars + "$");
  189. return this.replace(regex, "");
  190. };
  191. Array.prototype.append = Array.prototype.push;
  192. String.prototype.strip = String.prototype.trim;
  193. function 是否正版(vipUrl){
  194. 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');
  195. return flag.test(vipUrl);
  196. }
  197. function urlDeal(vipUrl){
  198. if(!vipUrl){
  199. return ''
  200. }
  201. if(!是否正版(vipUrl)){
  202. return vipUrl
  203. }
  204. if(!/miguvideo/.test(vipUrl)){
  205. vipUrl=vipUrl.split('#')[0].split('?')[0];
  206. }
  207. return vipUrl
  208. }
  209. function setResult(d){
  210. if(!Array.isArray(d)){
  211. return []
  212. }
  213. VODS = [];
  214. // print(d);
  215. d.forEach(function (it){
  216. let obj = {
  217. vod_id:it.url||'',
  218. vod_name: it.title||'',
  219. vod_remarks: it.desc||'',
  220. vod_content: it.content||'',
  221. vod_pic: it.pic_url||it.img||'',
  222. };
  223. let keys = Object.keys(it);
  224. if(keys.includes('tname')){
  225. obj.type_name = it.tname||'';
  226. }
  227. if(keys.includes('tid')){
  228. obj.type_id = it.tid||'';
  229. }
  230. if(keys.includes('year')){
  231. obj.vod_year = it.year||'';
  232. }
  233. if(keys.includes('actor')){
  234. obj.vod_actor = it.actor||'';
  235. }
  236. if(keys.includes('director')){
  237. obj.vod_director = it.director||'';
  238. }
  239. if(keys.includes('area')){
  240. obj.vod_area = it.area||'';
  241. }
  242. VODS.push(obj);
  243. });
  244. return VODS
  245. }
  246. function setResult2(res){
  247. VODS = res.list||[];
  248. return VODS
  249. }
  250. function setHomeResult(res){
  251. if(!res||typeof(res)!=='object'){
  252. return []
  253. }
  254. return setResult(res.list);
  255. }
  256. // 猫了个咪
  257. function rc(js) {
  258. if (js === 'maomi_aes.js') {
  259. var a = CryptoJS.enc.Utf8.parse("625222f9149e961d");
  260. var t = CryptoJS.enc.Utf8.parse("5efdtf6060e2o330");
  261. return {
  262. De: function (word) {
  263. word = CryptoJS.enc.Hex.parse(word)
  264. return CryptoJS.AES.decrypt(CryptoJS.enc.Base64.stringify(word), a, {
  265. iv: t,
  266. mode: CryptoJS.mode.CBC,
  267. padding: CryptoJS.pad.Pkcs7
  268. }).toString(CryptoJS.enc.Utf8)
  269. },
  270. En: function (word) {
  271. // print(a);
  272. // print(word);
  273. var Encrypted = CryptoJS.AES.encrypt(word, a, {
  274. iv: t,
  275. mode: CryptoJS.mode.CBC,
  276. padding: CryptoJS.pad.Pkcs7
  277. });
  278. return Encrypted.ciphertext.toString();
  279. }
  280. };
  281. }
  282. return {};
  283. }
  284. // 千万不要用for in 推荐 forEach (for in 会打乱顺序)
  285. //猫函数
  286. function maoss(jxurl, ref, key) {
  287. fetch_params = JSON.parse(JSON.stringify(rule_fetch_params));
  288. eval(getCryptoJS());
  289. try {
  290. var getVideoInfo = function (text) {
  291. return CryptoJS.AES.decrypt(text, key, {iv: iv, padding: CryptoJS.pad.Pkcs7}).toString(CryptoJS.enc.Utf8);
  292. };
  293. var token_key = key == undefined ? 'dvyYRQlnPRCMdQSe' : key;
  294. if (ref) {
  295. var html = request(jxurl, {
  296. headers: {
  297. 'Referer': ref
  298. }
  299. });
  300. } else {
  301. var html = request(jxurl);
  302. }
  303. // print(html);
  304. if (html.indexOf('&btwaf=') != -1) {
  305. html = request(jxurl + '&btwaf' + html.match(/&btwaf(.*?)"/)[1], {
  306. headers: {
  307. 'Referer': ref
  308. }
  309. })
  310. }
  311. var token_iv = html.split('_token = "')[1].split('"')[0];
  312. var key = CryptoJS.enc.Utf8.parse(token_key);
  313. var iv = CryptoJS.enc.Utf8.parse(token_iv);
  314. // log("iv:"+iv);
  315. // log(html);
  316. // print(key);
  317. // print(iv);
  318. eval(html.match(/var config = {[\s\S]*?}/)[0] + '');
  319. // config.url = config.url.replace(/,/g,'');
  320. // print(config.url);
  321. if (!config.url.startsWith('http')) {
  322. //config.url = decodeURIComponent(AES(config.url, key, iv));
  323. config.url = CryptoJS.AES.decrypt(config.url, key, {
  324. iv: iv,
  325. padding: CryptoJS.pad.Pkcs7
  326. }).toString(CryptoJS.enc.Utf8)
  327. }
  328. return config.url;
  329. } catch (e) {
  330. return '';
  331. }
  332. }
  333. function urlencode (str) {
  334. str = (str + '').toString();
  335. return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
  336. replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
  337. }
  338. function base64Encode(text){
  339. return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(text));
  340. // return text
  341. }
  342. function base64Decode(text){
  343. return CryptoJS.enc.Utf8.stringify(CryptoJS.enc.Base64.parse(text));
  344. // return text
  345. }
  346. function md5(text) {
  347. return CryptoJS.MD5(text).toString();
  348. }
  349. function getCryptoJS(){
  350. // return request('https://gitcode.net/qq_32394351/dr_py/-/raw/master/libs/crypto-hiker.js');
  351. return 'console.log("CryptoJS已装载");'
  352. }
  353. let VODS = [];// 一级或者搜索需要的数据列表
  354. let VOD = {};// 二级的单个数据
  355. globalThis.encodeUrl = urlencode;
  356. globalThis.urlencode = urlencode;
  357. /**
  358. * url拼接
  359. * @param fromPath 初始当前页面url
  360. * @param nowPath 相对当前页面url
  361. * @returns {*}
  362. */
  363. function urljoin(fromPath, nowPath) {
  364. fromPath = fromPath||'';
  365. nowPath = nowPath||'';
  366. return joinUrl(fromPath, nowPath);
  367. // try {
  368. // // import Uri from './uri.min.js';
  369. // // var Uri = require('./uri.min.js');
  370. // // eval(request('https://cdn.bootcdn.net/ajax/libs/URI.js/1.19.11/URI.min.js'));
  371. // // let new_uri = URI(nowPath, fromPath);
  372. // let new_uri = Uri(nowPath, fromPath);
  373. // new_uri = new_uri.toString();
  374. // // console.log(new_uri);
  375. // // return fromPath + nowPath
  376. // return new_uri
  377. // }
  378. // catch (e) {
  379. // console.log('urljoin发生错误:'+e.message);
  380. // if(nowPath.startsWith('http')){
  381. // return nowPath
  382. // }if(nowPath.startsWith('/')){
  383. // return getHome(fromPath)+nowPath
  384. // }
  385. // return fromPath+nowPath
  386. // }
  387. }
  388. var urljoin2 = urljoin;
  389. /**
  390. * 重写pd方法-增加自动urljoin(没法重写,改个名继续骗)
  391. * @param html
  392. * @param parse
  393. * @param uri
  394. * @returns {*}
  395. */
  396. function pD(html,parse,uri){
  397. let ret = pdfh(html,parse);
  398. if(typeof(uri)==='undefined'||!uri){
  399. uri = '';
  400. }
  401. if(DOM_CHECK_ATTR.test(parse)){
  402. if(/http/.test(ret)){
  403. ret = ret.substr(ret.indexOf('http'));
  404. }else{
  405. ret = urljoin(MY_URL,ret)
  406. }
  407. }
  408. // MY_URL = getItem('MY_URL',MY_URL);
  409. // console.log(`规则${RKEY}打印MY_URL:${MY_URL},uri:${uri}`);
  410. return ret
  411. }
  412. const parseTags = {
  413. jsp:{
  414. pdfh:pdfh,
  415. pdfa:pdfa,
  416. pd:pD,
  417. },
  418. json:{
  419. pdfh(html, parse) {
  420. if (!parse || !parse.trim()){
  421. return '';
  422. }
  423. if (typeof(html) === 'string'){
  424. // print('jsonpath:pdfh字符串转dict');
  425. html = JSON.parse(html);
  426. }
  427. parse = parse.trim();
  428. if (!parse.startsWith('$.')){
  429. parse = '$.' + parse;
  430. }
  431. parse = parse.split('||');
  432. for (let ps of parse) {
  433. let ret = cheerio.jp(ps, html);
  434. if (Array.isArray(ret)){
  435. ret = ret[0] || '';
  436. } else{
  437. ret = ret || ''
  438. }
  439. if (ret && typeof (ret) !== 'string'){
  440. ret = ret.toString();
  441. }
  442. if(ret){
  443. return ret
  444. }
  445. }
  446. return '';
  447. },
  448. pdfa(html, parse) {
  449. if (!parse || !parse.trim()){
  450. return '';
  451. }
  452. if (typeof(html) === 'string'){
  453. // print('jsonpath:pdfa字符串转dict');
  454. html = JSON.parse(html);
  455. }
  456. parse = parse.trim()
  457. if (!parse.startsWith('$.')){
  458. parse = '$.' + parse;
  459. }
  460. let ret = cheerio.jp(parse, html);
  461. if (Array.isArray(ret) && Array.isArray(ret[0]) && ret.length === 1){
  462. return ret[0] || []
  463. }
  464. return ret || []
  465. },
  466. pd(html,parse){
  467. let ret = parseTags.json.pdfh(html,parse);
  468. if(ret){
  469. return urljoin(MY_URL,ret);
  470. }
  471. return ret
  472. },
  473. },
  474. jq:{
  475. pdfh(html, parse, base_url) {
  476. if (!parse || !parse.trim()) {
  477. return ''
  478. }
  479. let eleFind = typeof html === 'object';
  480. let option = undefined;
  481. if (eleFind && parse.startsWith('body&&')) {
  482. parse = parse.substr(6);
  483. if (parse.indexOf('&&') < 0) {
  484. option = parse.trim();
  485. parse = '*=*';
  486. }
  487. }
  488. if (parse.indexOf('&&') > -1) {
  489. let sp = parse.split('&&');
  490. option = sp[sp.length - 1];
  491. sp.splice(sp.length - 1);
  492. if (sp.length > 1) {
  493. for (let i in sp) {
  494. if (!SELECT_REGEX.test(sp[i])) {
  495. sp[i] = sp[i] + ':eq(0)';
  496. }
  497. }
  498. } else {
  499. if (!SELECT_REGEX.test(sp[0])) {
  500. sp[0] = sp[0] + ':eq(0)';
  501. }
  502. }
  503. parse = sp.join(' ');
  504. }
  505. let result = '';
  506. const $ = eleFind ? html.rr : cheerio.load(html);
  507. let ret = eleFind ? ((parse === '*=*' || $(html.ele).is(parse)) ? html.ele : $(html.ele).find(parse)) : $(parse);
  508. if (option) {
  509. if (option === 'Text') {
  510. result = $(ret).text();
  511. }
  512. else if (option === 'Html') {
  513. result = $(ret).html();
  514. }
  515. else {
  516. result = $(ret).attr(option);
  517. }
  518. if (result && base_url && DOM_CHECK_ATTR.test(option)) {
  519. if (/http/.test(result)) {
  520. result = result.substr(result.indexOf('http'));
  521. } else {
  522. result = urljoin(base_url, result)
  523. }
  524. }
  525. } else {
  526. result = $(ret).toString();
  527. }
  528. return result;
  529. },
  530. pdfa(html, parse) {
  531. if (!parse || !parse.trim()) {
  532. return [];
  533. }
  534. let eleFind = typeof html === 'object';
  535. if (parse.indexOf('&&') > -1) {
  536. let sp = parse.split('&&');
  537. for (let i in sp) {
  538. if (!SELECT_REGEX_A.test(sp[i]) && i < sp.length - 1) {
  539. sp[i] = sp[i] + ':eq(0)';
  540. }
  541. }
  542. parse = sp.join(' ');
  543. }
  544. const $ = eleFind ? html.rr : cheerio.load(html);
  545. let ret = eleFind ? ($(html.ele).is(parse) ? html.ele : $(html.ele).find(parse)) : $(parse);
  546. let result = [];
  547. if (ret) {
  548. ret.each(function (idx, ele) {
  549. result.push({ rr: $, ele: ele });
  550. });
  551. }
  552. return result;
  553. },
  554. pd(html,parse,uri){
  555. return parseTags.jq.pdfh(html, parse, MY_URL);
  556. },
  557. },
  558. getParse(p0){//非js开头的情况自动获取解析标签
  559. if(p0.startsWith('jsp:')){
  560. return this.jsp
  561. }else if(p0.startsWith('json:')){
  562. return this.json
  563. }else if(p0.startsWith('jq:')){
  564. return this.jq
  565. }else {
  566. return this.jq
  567. }
  568. }
  569. };
  570. const stringify = JSON.stringify;
  571. const jsp = parseTags.jsp;
  572. const jq = parseTags.jq;
  573. /*** 后台需要实现的java方法并注入到js中 ***/
  574. /**
  575. * 读取本地文件->应用程序目录
  576. * @param filePath
  577. * @returns {string}
  578. */
  579. function readFile(filePath){
  580. filePath = filePath||'./uri.min.js';
  581. var fd = os.open(filePath);
  582. var buffer = new ArrayBuffer(1024);
  583. var len = os.read(fd, buffer, 0, 1024);
  584. console.log(len);
  585. let text = String.fromCharCode.apply(null, new Uint8Array(buffer));
  586. console.log(text);
  587. return text
  588. }
  589. /**
  590. * 处理返回的json数据
  591. * @param html
  592. * @returns {*}
  593. */
  594. function dealJson(html) {
  595. try {
  596. return html.match(/[\w|\W|\s|\S]*?(\{[\w|\W|\s|\S]*\})/).group[1];
  597. } catch (e) {
  598. }
  599. try {
  600. html = JSON.parse(html);
  601. }catch (e) {}
  602. // console.log(typeof(html));
  603. return html;
  604. }
  605. /**
  606. * 验证码识别逻辑,需要java实现(js没有bytes类型,无法调用后端的传递图片二进制获取验证码文本的接口)
  607. * @type {{api: string, classification: (function(*=): string)}}
  608. */
  609. var OcrApi={
  610. api:OCR_API,
  611. classification:function (img){ // img是byte类型,这里不方便搞啊
  612. let code = '';
  613. try {
  614. let html = request(this.api,{data:{img:img},headers:{'User-Agent':PC_UA},'method':'POST'});
  615. html = JSON.parse(html);
  616. code = html.url||'';
  617. }catch (e) {}
  618. return code
  619. }
  620. };
  621. /**
  622. * 验证码识别,暂未实现
  623. * @param url 验证码图片链接
  624. * @returns {string} 验证成功后的cookie
  625. */
  626. function verifyCode(url){
  627. let cnt = 0;
  628. let host = getHome(url);
  629. let cookie = '';
  630. while (cnt < OCR_RETRY){
  631. try{
  632. // let obj = {headers:headers,timeout:timeout};
  633. let yzm_url = `${host}/index.php/verify/index.html`;
  634. console.log(`验证码链接:${yzm_url}`);
  635. let hhtml = request(yzm_url,{withHeaders:true,toBase64:true});
  636. let json = JSON.parse(hhtml);
  637. if(!cookie){
  638. cookie = json['set-cookie']?json['set-cookie'].split(';')[0]:'';
  639. }
  640. // console.log(hhtml);
  641. console.log('cookie:'+cookie);
  642. let img = json.body;
  643. // console.log(img);
  644. let code = OcrApi.classification(img);
  645. console.log(`第${cnt+1}次验证码识别结果:${code}`);
  646. let submit_url = `${host}/index.php/ajax/verify_check?type=search&verify=${code}`;
  647. console.log(submit_url);
  648. let html = request(submit_url,{headers:{Cookie:cookie,'User-Agent':MOBILE_UA},'method':'POST'});
  649. // console.log(html);
  650. html = JSON.parse(html);
  651. if(html.msg === 'ok'){
  652. console.log(`第${cnt+1}次验证码提交成功`);
  653. return cookie // 需要返回cookie
  654. }else if(html.msg!=='ok'&&cnt+1>=OCR_RETRY){
  655. cookie = ''; // 需要清空返回cookie
  656. }
  657. }catch (e) {
  658. console.log(`第${cnt+1}次验证码提交失败:${e.message}`);
  659. if(cnt+1>=OCR_RETRY){
  660. cookie = '';
  661. }
  662. }
  663. cnt+=1
  664. }
  665. return cookie
  666. }
  667. /**
  668. * 存在数据库配置表里, key字段对应值value,没有就新增,有就更新,调用此方法会清除key对应的内存缓存
  669. * @param k 键
  670. * @param v 值
  671. */
  672. function setItem(k,v){
  673. local.set(RKEY,k,v);
  674. console.log(`规则${RKEY}设置${k} => ${v}`)
  675. }
  676. /**
  677. * 获取数据库配置表对应的key字段的value,没有这个key就返回value默认传参.需要有缓存,第一次获取后会存在内存里
  678. * @param k 键
  679. * @param v 值
  680. * @returns {*}
  681. */
  682. function getItem(k,v){
  683. return local.get(RKEY,k) || v;
  684. }
  685. /**
  686. * 删除数据库key对应的一条数据,并清除此key对应的内存缓存
  687. * @param k
  688. */
  689. function clearItem(k){
  690. local.delete(RKEY,k);
  691. }
  692. /*** js自封装的方法 ***/
  693. /**
  694. * 获取链接的host(带http协议的完整链接)
  695. * @param url 任意一个正常完整的Url,自动提取根
  696. * @returns {string}
  697. */
  698. function getHome(url){
  699. if(!url){
  700. return ''
  701. }
  702. let tmp = url.split('//');
  703. url = tmp[0] + '//' + tmp[1].split('/')[0];
  704. try {
  705. url = decodeURIComponent(url);
  706. }catch (e) {}
  707. return url
  708. }
  709. /**
  710. * get参数编译链接,类似python params字典自动拼接
  711. * @param url 访问链接
  712. * @param obj 参数字典
  713. * @returns {*}
  714. */
  715. function buildUrl(url,obj){
  716. obj = obj||{};
  717. if(url.indexOf('?')<0){
  718. url += '?'
  719. }
  720. let param_list = [];
  721. let keys = Object.keys(obj);
  722. keys.forEach(it=>{
  723. param_list.push(it+'='+obj[it])
  724. });
  725. let prs = param_list.join('&');
  726. if(keys.length > 0 && !url.endsWith('?')){
  727. url += '&'
  728. }
  729. url+=prs;
  730. return url
  731. }
  732. /**
  733. * 远程依赖执行函数
  734. * @param url 远程js地址
  735. */
  736. function require(url){
  737. eval(request(url));
  738. }
  739. /**
  740. * 海阔网页请求函数完整封装
  741. * @param url 请求链接
  742. * @param obj 请求对象 {headers:{},method:'',timeout:5000,body:'',withHeaders:false}
  743. * @returns {string|string|DocumentFragment|*}
  744. */
  745. function request(url,obj){
  746. if(typeof(obj)==='undefined'||!obj||obj==={}){
  747. if(!fetch_params||!fetch_params.headers){
  748. let headers = {
  749. 'User-Agent':MOBILE_UA,
  750. };
  751. if(rule.headers){
  752. Object.assign(headers,rule.headers);
  753. }
  754. fetch_params.headers = headers;
  755. }
  756. if(!fetch_params.headers.Referer){
  757. fetch_params.headers.Referer = getHome(url)
  758. }
  759. obj = fetch_params;
  760. }else{
  761. let headers = obj.headers||{};
  762. let keys = Object.keys(headers).map(it=>it.toLowerCase());
  763. if(!keys.includes('user-agent')){
  764. headers['User-Agent'] = MOBILE_UA;
  765. }if(!keys.includes('referer')){
  766. headers['Referer'] = getHome(url);
  767. }
  768. obj.headers = headers;
  769. }
  770. console.log(JSON.stringify(obj.headers));
  771. if(typeof(obj.headers.body)!='undefined'&&obj.headers.body&&typeof (obj.headers.body)==='string'){
  772. let data = {};
  773. obj.headers.body.split('&').forEach(it=>{
  774. data[it.split('=')[0]] = it.split('=')[1]
  775. });
  776. obj.data = data;
  777. delete obj.headers.body
  778. }
  779. if(!url){
  780. return obj.withHeaders?'{}':''
  781. }
  782. if(obj.toBase64){ // 返回base64,用于请求图片
  783. obj.buffer = 2;
  784. delete obj.toBase64
  785. }
  786. console.log('request:'+url);
  787. let res = req(url, obj);
  788. let html = res.content||'';
  789. // console.log(html);
  790. if(obj.withHeaders){
  791. let htmlWithHeaders = res.headers;
  792. htmlWithHeaders.body = html;
  793. return JSON.stringify(htmlWithHeaders);
  794. }else{
  795. return html
  796. }
  797. }
  798. fetch = request;
  799. print = function (data){
  800. data = data||'';
  801. if(typeof(data)!=='string'){
  802. try {
  803. data = JSON.stringify(data);
  804. }catch (e) {
  805. console.log('print:'+e.message)
  806. }
  807. }
  808. console.log(data);
  809. }
  810. log = print;
  811. /**
  812. * 检查宝塔验证并自动跳过获取正确源码
  813. * @param html 之前获取的html
  814. * @param url 之前的来源url
  815. * @param obj 来源obj
  816. * @returns {string|DocumentFragment|*}
  817. */
  818. function checkHtml(html,url,obj){
  819. if(/\?btwaf=/.test(html)){
  820. let btwaf = html.match(/btwaf(.*?)"/)[1];
  821. url = url.split('#')[0]+'?btwaf'+btwaf;
  822. html = request(url,obj);
  823. }
  824. return html
  825. }
  826. /**
  827. * 带一次宝塔验证的源码获取
  828. * @param url 请求链接
  829. * @param obj 请求参数
  830. * @returns {string|DocumentFragment}
  831. */
  832. function getCode(url,obj){
  833. let html = request(url,obj);
  834. html = checkHtml(html,url,obj);
  835. return html
  836. }
  837. /**
  838. * 源rule专用的请求方法,自动注入cookie
  839. * @param url 请求链接
  840. * @returns {string|DocumentFragment}
  841. */
  842. function getHtml(url){
  843. let obj = {};
  844. if(rule.headers){
  845. obj.headers = rule.headers;
  846. }
  847. let cookie = getItem(RULE_CK,'');
  848. if(cookie){
  849. if(obj.headers && ! Object.keys(obj.headers).map(it=>it.toLowerCase()).includes('cookie')){
  850. obj.headers['Cookie'] = cookie;
  851. }else if(!obj.headers){
  852. obj.headers = {Cookie:cookie};
  853. }
  854. }
  855. let html = getCode(url,obj);
  856. return html
  857. }
  858. /**
  859. * 首页分类解析,筛选暂未实现
  860. * @param homeObj 首页传参对象
  861. * @returns {string}
  862. */
  863. function homeParse(homeObj) {
  864. fetch_params = JSON.parse(JSON.stringify(rule_fetch_params));
  865. let classes = [];
  866. if (homeObj.class_name && homeObj.class_url) {
  867. let names = homeObj.class_name.split('&');
  868. let urls = homeObj.class_url.split('&');
  869. let cnt = Math.min(names.length, urls.length);
  870. for (let i = 0; i < cnt; i++) {
  871. classes.push({
  872. 'type_id': urls[i],
  873. 'type_name': names[i]
  874. });
  875. }
  876. }
  877. if (homeObj.class_parse) {
  878. let p = homeObj.class_parse.split(';');
  879. if (p.length >= 4) {
  880. try {
  881. let html = getHtml(homeObj.MY_URL);
  882. if (html) {
  883. homeHtmlCache = html;
  884. let list = pdfa(html, p[0]);
  885. if (list && list.length > 0) {
  886. list.forEach((it,idex) => {
  887. try {
  888. let name = pdfh(it, p[1]);
  889. if (homeObj.cate_exclude && (new RegExp(homeObj.cate_exclude).test(name))) {
  890. return;
  891. }
  892. let url = pdfh(it, p[2]);
  893. if (p[3]) {
  894. let exp = new RegExp(p[3]);
  895. url = url.match(exp)[1];
  896. }
  897. classes.push({
  898. 'type_id': url.trim(),
  899. 'type_name': name.trim()
  900. });
  901. } catch (e) {
  902. console.log(`分类列表定位第${idex}个元素正常报错:${e.message}`);
  903. }
  904. });
  905. }
  906. }
  907. } catch (e) {
  908. console.log(e.message);
  909. }
  910. }
  911. }
  912. // 排除分类
  913. classes = classes.filter(it=>!homeObj.cate_exclude || !(new RegExp(homeObj.cate_exclude).test(it.type_name)));
  914. let resp = {
  915. 'class': classes
  916. };
  917. if(homeObj.filter){
  918. resp.filters = homeObj.filter;
  919. }
  920. console.log(JSON.stringify(resp));
  921. return JSON.stringify(resp);
  922. }
  923. /**
  924. * 首页推荐列表解析
  925. * @param homeVodObj
  926. * @returns {string}
  927. */
  928. function homeVodParse(homeVodObj){
  929. fetch_params = JSON.parse(JSON.stringify(rule_fetch_params));
  930. let d = [];
  931. MY_URL = homeVodObj.homeUrl;
  932. // setItem('MY_URL',MY_URL);
  933. console.log(MY_URL);
  934. let p = homeVodObj.推荐;
  935. if(!p||typeof(p)!=='string'){
  936. return '{}'
  937. }
  938. p = p.trim();
  939. if(p.startsWith('js:')){
  940. const TYPE = 'home';
  941. var input = MY_URL;
  942. const HOST = rule.host;
  943. eval(p.replace('js:',''));
  944. d = VODS;
  945. }else {
  946. p = p.split(';');
  947. if (!homeVodObj.double && p.length < 5) {
  948. return '{}'
  949. } else if (homeVodObj.double && p.length < 6) {
  950. return '{}'
  951. }
  952. let _ps = parseTags.getParse(p[0]);
  953. _pdfa = _ps.pdfa;
  954. _pdfh = _ps.pdfh;
  955. _pd = _ps.pd;
  956. let is_json = p[0].startsWith('json:');
  957. p[0] = p[0].replace(/^(jsp:|json:|jq:)/,'');
  958. // print(p[0]);
  959. let html = homeHtmlCache || getHtml(MY_URL);
  960. homeHtmlCache = undefined;
  961. if(is_json){
  962. // print('是json,开始处理');
  963. html = dealJson(html);
  964. }
  965. try {
  966. console.log('double:' + homeVodObj.double);
  967. if (homeVodObj.double) {
  968. let items = _pdfa(html, p[0]);
  969. // console.log(items.length);
  970. for (let item of items) {
  971. // console.log(p[1]);
  972. let items2 = _pdfa(item, p[1]);
  973. // console.log(items2.length);
  974. for (let item2 of items2) {
  975. try {
  976. let title = _pdfh(item2, p[2]);
  977. let img = '';
  978. try {
  979. img = _pd(item2, p[3])
  980. } catch (e) {
  981. }
  982. let desc = _pdfh(item2, p[4]);
  983. let links = [];
  984. for (let p5 of p[5].split('+')) {
  985. let link = !homeVodObj.detailUrl ? _pd(item2, p5, MY_URL) : _pdfh(item2, p5);
  986. links.push(link);
  987. }
  988. let vod = {
  989. vod_name: title,
  990. vod_pic: img,
  991. vod_remarks: desc,
  992. vod_id: links.join('$')
  993. };
  994. // print(vod);
  995. d.push(vod);
  996. } catch (e) {
  997. console.log('首页列表处理发生错误:'+e.message);
  998. }
  999. }
  1000. }
  1001. } else {
  1002. let items = _pdfa(html, p[0]);
  1003. for (let item of items) {
  1004. try {
  1005. let title = _pdfh(item, p[1]);
  1006. let img = '';
  1007. try {
  1008. img = _pd(item, p[2], MY_URL);
  1009. } catch (e) {
  1010. }
  1011. let desc = _pdfh(item, p[3]);
  1012. let links = [];
  1013. for (let p5 of p[4].split('+')) {
  1014. let link = !homeVodObj.detailUrl ? _pd(item, p5, MY_URL) : _pdfh(item, p5);
  1015. links.push(link);
  1016. }
  1017. let vod = {
  1018. vod_name: title,
  1019. vod_pic: img,
  1020. vod_remarks: desc,
  1021. vod_id: links.join('$')
  1022. };
  1023. d.push(vod);
  1024. } catch (e) {
  1025. }
  1026. }
  1027. }
  1028. } catch (e) {
  1029. }
  1030. }
  1031. // console.log(JSON.stringify(d));
  1032. return JSON.stringify({
  1033. list:d
  1034. })
  1035. }
  1036. /**
  1037. * 一级分类页数据解析
  1038. * @param cateObj
  1039. * @returns {string}
  1040. */
  1041. function categoryParse(cateObj) {
  1042. fetch_params = JSON.parse(JSON.stringify(rule_fetch_params));
  1043. let p = cateObj.一级;
  1044. if(!p||typeof(p)!=='string'){
  1045. return '{}'
  1046. }
  1047. let d = [];
  1048. // let url = cateObj.url.replaceAll('fyclass', cateObj.tid).replaceAll('fypage', cateObj.pg);
  1049. let url = cateObj.url.replaceAll('fyclass', cateObj.tid);
  1050. if(rule.filter_url){
  1051. if(!/fyfilter/.test(url)){
  1052. if(!url.endsWith('&')&&!rule.filter_url.startsWith('&')){
  1053. url+='&'
  1054. }
  1055. url+=rule.filter_url;
  1056. }else{
  1057. url = url.replace('fyfilter', rule.filter_url);
  1058. }
  1059. // console.log('filter:'+cateObj.filter);
  1060. let fl = cateObj.filter?cateObj.extend:{};
  1061. let new_url;
  1062. new_url = cheerio.jinja2(url,{fl:fl});
  1063. // console.log('jinjia2执行后的new_url类型为:'+typeof(new_url));
  1064. if(/object Object/.test(new_url)){
  1065. new_url = drT.renderText(url,fl);
  1066. }
  1067. url = new_url;
  1068. }
  1069. if(/fypage/.test(url)){
  1070. if(url.includes('(')&&url.includes(')')){
  1071. let url_rep = url.match(/.*?\((.*)\)/)[1];
  1072. // console.log(url_rep);
  1073. let cnt_page = url_rep.replaceAll('fypage', cateObj.pg);
  1074. // console.log(cnt_page);
  1075. let cnt_pg = eval(cnt_page);
  1076. // console.log(cnt_pg);
  1077. url = url.replaceAll(url_rep,cnt_pg).replaceAll('(','').replaceAll(')','');
  1078. }else{
  1079. url = url.replaceAll('fypage',cateObj.pg);
  1080. }
  1081. }
  1082. if(cateObj.pg === 1 && url.includes('[')&&url.includes(']')){
  1083. url = url.split('[')[1].split(']')[0];
  1084. }
  1085. MY_URL = url;
  1086. // setItem('MY_URL',MY_URL);
  1087. console.log(MY_URL);
  1088. p = p.trim();
  1089. const MY_CATE = cateObj.tid;
  1090. if(p.startsWith('js:')){
  1091. var MY_FL = cateObj.extend;
  1092. const TYPE = 'cate';
  1093. var input = MY_URL;
  1094. const MY_PAGE = cateObj.pg;
  1095. var desc = '';
  1096. eval(p.trim().replace('js:',''));
  1097. d = VODS;
  1098. }else {
  1099. p = p.split(';');
  1100. if (p.length < 5) {
  1101. return '{}'
  1102. }
  1103. let _ps = parseTags.getParse(p[0]);
  1104. _pdfa = _ps.pdfa;
  1105. _pdfh = _ps.pdfh;
  1106. _pd = _ps.pd;
  1107. let is_json = p[0].startsWith('json:');
  1108. p[0] = p[0].replace(/^(jsp:|json:|jq:)/,'');
  1109. try {
  1110. let html = getHtml(MY_URL);
  1111. if (html) {
  1112. if(is_json){
  1113. html = dealJson(html);
  1114. }
  1115. let list = _pdfa(html, p[0]);
  1116. list.forEach(it => {
  1117. let links = p[4].split('+').map(p4=>{
  1118. return !rule.detailUrl?_pd(it, p4,MY_URL):_pdfh(it, p4);
  1119. });
  1120. let link = links.join('$');
  1121. let vod_id = rule.detailUrl?MY_CATE+'$'+link:link;
  1122. d.push({
  1123. 'vod_id': vod_id,
  1124. 'vod_name': _pdfh(it, p[1]).replace(/\n|\t/g,'').trim(),
  1125. 'vod_pic': _pd(it, p[2],MY_URL),
  1126. 'vod_remarks': _pdfh(it, p[3]).replace(/\n|\t/g,'').trim(),
  1127. });
  1128. });
  1129. }
  1130. } catch (e) {
  1131. console.log(e.message);
  1132. }
  1133. }
  1134. // print(d);
  1135. return d.length<1?'{}':JSON.stringify({
  1136. 'page': parseInt(cateObj.pg),
  1137. 'pagecount': 999,
  1138. 'limit': 20,
  1139. 'total': 999,
  1140. 'list': d,
  1141. });
  1142. }
  1143. /**
  1144. * 搜索列表数据解析
  1145. * @param searchObj
  1146. * @returns {string}
  1147. */
  1148. function searchParse(searchObj) {
  1149. fetch_params = JSON.parse(JSON.stringify(rule_fetch_params));
  1150. let d = [];
  1151. if(!searchObj.searchUrl){
  1152. return '{}'
  1153. }
  1154. let p = searchObj.搜索==='*'&&rule.一级 ? rule.一级 : searchObj.搜索;
  1155. if(!p||typeof(p)!=='string'){
  1156. return '{}'
  1157. }
  1158. p = p.trim();
  1159. let url = searchObj.searchUrl.replaceAll('**', searchObj.wd).replaceAll('fypage', searchObj.pg);
  1160. MY_URL = url;
  1161. console.log(MY_URL);
  1162. // setItem('MY_URL',MY_URL);
  1163. if(p.startsWith('js:')){
  1164. const TYPE = 'search';
  1165. const MY_PAGE = searchObj.pg;
  1166. const KEY = searchObj.wd;
  1167. var input = MY_URL;
  1168. var detailUrl = rule.detailUrl||'';
  1169. eval(p.trim().replace('js:',''));
  1170. d = VODS;
  1171. }else{
  1172. p = p.split(';');
  1173. if (p.length < 5) {
  1174. return '{}'
  1175. }
  1176. let _ps = parseTags.getParse(p[0]);
  1177. _pdfa = _ps.pdfa;
  1178. _pdfh = _ps.pdfh;
  1179. _pd = _ps.pd;
  1180. let is_json = p[0].startsWith('json:');
  1181. p[0] = p[0].replace(/^(jsp:|json:|jq:)/,'');
  1182. try {
  1183. let html = getHtml(MY_URL);
  1184. if (html) {
  1185. if(/系统安全验证|输入验证码/.test(html)){
  1186. let cookie = verifyCode(MY_URL);
  1187. if(cookie){
  1188. console.log(`本次成功过验证,cookie:${cookie}`);
  1189. setItem(RULE_CK,cookie);
  1190. }else{
  1191. console.log(`本次自动过搜索验证失败,cookie:${cookie}`);
  1192. }
  1193. // obj.headers['Cookie'] = cookie;
  1194. html = getHtml(MY_URL);
  1195. }
  1196. if(!html.includes(searchObj.wd)){
  1197. console.log('搜索结果源码未包含关键字,疑似搜索失败,正为您打印结果源码');
  1198. console.log(html);
  1199. }
  1200. if(is_json){
  1201. html = dealJson(html);
  1202. }
  1203. let list = _pdfa(html, p[0]);
  1204. list.forEach(it => {
  1205. let links = p[4].split('+').map(p4=>{
  1206. return !rule.detailUrl?_pd(it, p4,MY_URL):_pdfh(it, p4)
  1207. });
  1208. let link = links.join('$');
  1209. let ob = {
  1210. 'vod_id': link,
  1211. 'vod_name': _pdfh(it, p[1]).replace(/\n|\t/g,'').trim(),
  1212. 'vod_pic': _pd(it, p[2],MY_URL),
  1213. 'vod_remarks': _pdfh(it, p[3]).replace(/\n|\t/g,'').trim(),
  1214. };
  1215. if (p.length > 5 && p[5]) {
  1216. ob.vod_content = _pdfh(it, p[5]);
  1217. }
  1218. d.push(ob);
  1219. });
  1220. }
  1221. } catch (e) {
  1222. return '{}'
  1223. }
  1224. }
  1225. return JSON.stringify({
  1226. 'page': parseInt(searchObj.pg),
  1227. 'pagecount': 10,
  1228. 'limit': 20,
  1229. 'total': 100,
  1230. 'list': d,
  1231. });
  1232. }
  1233. /**
  1234. * 二级详情页数据解析
  1235. * @param detailObj
  1236. * @returns {string}
  1237. */
  1238. function detailParse(detailObj){
  1239. fetch_params = JSON.parse(JSON.stringify(rule_fetch_params));
  1240. let vod = {
  1241. vod_id: detailObj.orId, //"id",
  1242. vod_name: "片名",
  1243. vod_pic: "",
  1244. type_name: "剧情",
  1245. vod_year: "年份",
  1246. vod_area: "地区",
  1247. vod_remarks: "更新信息",
  1248. vod_actor: "主演",
  1249. vod_director: "导演",
  1250. vod_content: "简介"
  1251. };
  1252. let p = detailObj.二级;
  1253. let url = detailObj.url;
  1254. let detailUrl = detailObj.detailUrl;
  1255. let fyclass = detailObj.fyclass;
  1256. let tab_exclude = detailObj.tab_exclude;
  1257. let html = detailObj.html||'';
  1258. MY_URL = url;
  1259. console.log(MY_URL);
  1260. // setItem('MY_URL',MY_URL);
  1261. if(p==='*'){
  1262. vod.vod_play_from = '道长在线';
  1263. vod.vod_remarks = detailUrl;
  1264. vod.vod_actor = '没有二级,只有一级链接直接嗅探播放';
  1265. vod.vod_content = MY_URL;
  1266. vod.vod_play_url = '嗅探播放$' + MY_URL;
  1267. }else if(typeof(p)==='string'&&p.trim().startsWith('js:')){
  1268. const TYPE = 'detail';
  1269. var input = MY_URL;
  1270. eval(p.trim().replace('js:',''));
  1271. vod = VOD;
  1272. console.log(JSON.stringify(vod));
  1273. }else if(p&&typeof(p)==='object'){
  1274. if(!html){
  1275. html = getHtml(MY_URL);
  1276. }
  1277. let _impJQP = true;
  1278. let _ps;
  1279. if(p.is_json){
  1280. _ps = parseTags.json;
  1281. html = dealJson(html);
  1282. _impJQP = false;
  1283. }else if(p.is_jsp){
  1284. _ps = parseTags.jsp;
  1285. }else if(p.is_jq){
  1286. _ps = parseTags.jq;
  1287. }else{
  1288. _ps = parseTags.jq;
  1289. }
  1290. if (_impJQP) {
  1291. let c$ = cheerio.load(html);
  1292. html = { rr: c$, ele: c$('html')[0] }
  1293. }
  1294. _pdfa = _ps.pdfa;
  1295. _pdfh = _ps.pdfh;
  1296. _pd = _ps.pd;
  1297. if(p.title){
  1298. let p1 = p.title.split(';');
  1299. vod.vod_name = _pdfh(html, p1[0]).replace(/\n|\t/g,'').trim();
  1300. let type_name = p1.length > 1 ? _pdfh(html, p1[1]).replace(/\n|\t/g,'').replace(/ /g,'').trim():'';
  1301. vod.type_name = type_name||vod.type_name;
  1302. }
  1303. if(p.desc){
  1304. try{
  1305. let p1 = p.desc.split(';');
  1306. vod.vod_remarks = _pdfh(html, p1[0]).replace(/\n|\t/g,'').trim();
  1307. vod.vod_year = p1.length > 1 ? _pdfh(html, p1[1]).replace(/\n|\t/g,'').trim():'';
  1308. vod.vod_area = p1.length > 2 ? _pdfh(html, p1[2]).replace(/\n|\t/g,'').trim():'';
  1309. // vod.vod_actor = p1.length > 3 ? _pdfh(html, p1[3]).replaceAll('\n', ' ').trim():'';
  1310. vod.vod_actor = p1.length > 3 ? _pdfh(html, p1[3]).replace(/\n|\t/g,'').trim():'';
  1311. vod.vod_director = p1.length > 4 ? _pdfh(html, p1[4]).replace(/\n|\t/g,'').trim():'';
  1312. }
  1313. catch (e) {
  1314. }
  1315. }
  1316. if(p.content){
  1317. try{
  1318. let p1 = p.content.split(';');
  1319. vod.vod_content = _pdfh(html, p1[0]).replace(/\n|\t/g,'').trim();
  1320. }
  1321. catch (e) {}
  1322. }
  1323. if(p.img){
  1324. try{
  1325. let p1 = p.img.split(';');
  1326. vod.vod_pic = _pd(html, p1[0],MY_URL);
  1327. }
  1328. catch (e) {}
  1329. }
  1330. let vod_play_from = '$$$';
  1331. let playFrom = [];
  1332. if(p.重定向&&p.重定向.startsWith('js:')){
  1333. html = eval(p.重定向.replace('js:',''));
  1334. if (_impJQP) {
  1335. let c$ = cheerio.load(html);
  1336. html = { rr: c$, ele: c$('html')[0] }
  1337. }
  1338. }
  1339. // console.log(2);
  1340. if(p.tabs){
  1341. let p_tab = p.tabs.split(';')[0];
  1342. console.log(p_tab);
  1343. let vHeader = _pdfa(html, p_tab);
  1344. console.log(vHeader.length);
  1345. for(let v of vHeader){
  1346. let v_title = _pdfh(v,'body&&Text');
  1347. console.log(v_title);
  1348. if(tab_exclude&& (new RegExp(tab_exclude)).test(v_title)){
  1349. continue;
  1350. }
  1351. playFrom.push(v_title);
  1352. }
  1353. console.log(JSON.stringify(playFrom));
  1354. }else{
  1355. playFrom = ['道长在线']
  1356. }
  1357. vod.vod_play_from = playFrom.join(vod_play_from);
  1358. // console.log(3);
  1359. let vod_play_url = '$$$';
  1360. let vod_tab_list = [];
  1361. if(p.lists){
  1362. for(let i=0;i<playFrom.length;i++){
  1363. let tab_name = playFrom[i];
  1364. let tab_ext = p.tabs.split(';').length > 1 ? p.tabs.split(';')[1] : '';
  1365. let p1 = p.lists.replaceAll('#idv', tab_name).replaceAll('#id', i);
  1366. tab_ext = tab_ext.replaceAll('#idv', tab_name).replaceAll('#id', i);
  1367. console.log(p1);
  1368. // console.log(html);
  1369. let vodList = [];
  1370. try {
  1371. vodList = _pdfa(html, p1);
  1372. console.log('len(vodList):'+vodList.length);
  1373. }catch (e) {
  1374. // console.log(e.message);
  1375. }
  1376. let new_vod_list = [];
  1377. let tabName = tab_ext?_pdfh(html, tab_ext):tab_name;
  1378. console.log(tabName);
  1379. // console.log('cheerio解析Text');
  1380. vodList.forEach(it=>{
  1381. // 请注意,这里要固定pdfh解析body&&Text,不需要下划线,没写错
  1382. // new_vod_list.push(pdfh(it,'body&&Text')+'$'+_pd(it,'a&&href',MY_URL));
  1383. // new_vod_list.push(cheerio.load(it).text()+'$'+_pd(it,'a&&href',MY_URL));
  1384. new_vod_list.push(_pdfh(it, 'body&&Text') + '$' + _pd(it, 'a&&href', MY_URL));
  1385. });
  1386. let vlist = new_vod_list.join('#');
  1387. vod_tab_list.push(vlist);
  1388. }
  1389. }
  1390. vod.vod_play_url = vod_tab_list.join(vod_play_url);
  1391. }
  1392. // print(vod);
  1393. return JSON.stringify({
  1394. list: [vod]
  1395. })
  1396. }
  1397. /**
  1398. * 选集播放点击事件解析
  1399. * @param playObj
  1400. * @returns {string}
  1401. */
  1402. function playParse(playObj){
  1403. fetch_params = JSON.parse(JSON.stringify(rule_fetch_params));
  1404. MY_URL = playObj.url;
  1405. if(!/http/.test(MY_URL)){
  1406. try {
  1407. MY_URL = base64Decode(MY_URL);
  1408. }catch (e) {}
  1409. }
  1410. MY_URL = decodeURIComponent(MY_URL);
  1411. var input = MY_URL;//注入给免嗅js
  1412. let common_play = {
  1413. parse:1,
  1414. url:input
  1415. };
  1416. let lazy_play;
  1417. if(!rule.play_parse||!rule.lazy){
  1418. lazy_play = common_play;
  1419. }else if(rule.play_parse&&rule.lazy&&typeof(rule.lazy)==='string'){
  1420. try {
  1421. let lazy_code = rule.lazy.replace('js:','').trim();
  1422. print('开始执行js免嗅=>'+lazy_code);
  1423. eval(lazy_code);
  1424. lazy_play = typeof(input) === 'object'?input:{
  1425. parse:1,
  1426. jx:1,
  1427. url:input
  1428. };
  1429. }catch (e) {
  1430. print('js免嗅错误:'+e.message);
  1431. lazy_play = common_play;
  1432. }
  1433. }else{
  1434. lazy_play = common_play;
  1435. }
  1436. console.log(JSON.stringify(lazy_play));
  1437. return JSON.stringify(lazy_play);
  1438. }
  1439. /**
  1440. * js源预处理特定返回对象中的函数
  1441. * @param ext
  1442. */
  1443. function init(ext) {
  1444. console.log('init');
  1445. try {
  1446. // make shared jsContext happy muban不能import,不然会造成换源继承后变量被篡改
  1447. if (typeof (globalThis.mubanJs) === 'undefined') {
  1448. let mubanJs = request('https://gitcode.net/qq_32394351/dr_py/-/raw/master/js/模板.js', { 'User-Agent': MOBILE_UA });
  1449. mubanJs = mubanJs.replace('export default', '(function() {return muban;}()) // export default');
  1450. // console.log(mubanJs);
  1451. globalThis.mubanJs = mubanJs;
  1452. }
  1453. let muban = eval(globalThis.mubanJs);
  1454. if (typeof ext == 'object'){
  1455. rule = ext;
  1456. if (rule.template) {
  1457. rule = Object.assign(muban[rule.template], rule);
  1458. }
  1459. } else if (typeof ext == 'string') {
  1460. if (ext.startsWith('http')) {
  1461. let js = request(ext,{'method':'GET'});
  1462. if (js){
  1463. eval(js.replace('var rule', 'rule'));
  1464. }
  1465. }
  1466. } else {
  1467. eval(ext.replace('var rule', 'rule'));
  1468. }
  1469. /** 处理一下 rule规则关键字段没传递的情况 **/
  1470. let rule_cate_excludes = (rule.cate_exclude||'').split('|').filter(it=>it.trim());
  1471. let rule_tab_excludes = (rule.tab_exclude||'').split('|').filter(it=>it.trim());
  1472. rule_cate_excludes = rule_cate_excludes.concat(CATE_EXCLUDE.split('|').filter(it=>it.trim()));
  1473. rule_tab_excludes = rule_tab_excludes.concat(TAB_EXCLUDE.split('|').filter(it=>it.trim()));
  1474. rule.cate_exclude = rule_cate_excludes.join('|');
  1475. rule.tab_exclude = rule_tab_excludes.join('|');
  1476. rule.host = (rule.host||'').rstrip('/');
  1477. rule.url = rule.url||'';
  1478. rule.double = rule.double||false;
  1479. rule.homeUrl = rule.homeUrl||'';
  1480. rule.detailUrl = rule.detailUrl||'';
  1481. rule.searchUrl = rule.searchUrl||'';
  1482. rule.homeUrl = rule.host&&rule.homeUrl?urljoin(rule.host,rule.homeUrl):(rule.homeUrl||rule.host);
  1483. rule.detailUrl = rule.host&&rule.detailUrl?urljoin(rule.host,rule.detailUrl):rule.detailUrl;
  1484. if(rule.url.includes('[')&&rule.url.includes(']')){
  1485. let u1 = rule.url.split('[')[0]
  1486. let u2 = rule.url.split('[')[1].split(']')[0]
  1487. rule.url = rule.host && rule.url?urljoin(rule.host,u1)+'['+urljoin(rule.host,u2)+']':rule.url;
  1488. }else{
  1489. rule.url = rule.host && rule.url ? urljoin(rule.host,rule.url) : rule.url;
  1490. }
  1491. rule.searchUrl = rule.host && rule.searchUrl ? urljoin(rule.host,rule.searchUrl) : rule.searchUrl;
  1492. rule.timeout = rule.timeout||5000;
  1493. rule.encoding = rule.编码||rule.encoding||'utf-8';
  1494. if(rule.headers && typeof(rule.headers) === 'object'){
  1495. try {
  1496. let header_keys = Object.keys(rule.headers);
  1497. for(let k of header_keys){
  1498. if(k.toLowerCase() === 'user-agent'){
  1499. let v = rule.headers[k];
  1500. console.log(v);
  1501. if(['MOBILE_UA','PC_UA','UC_UA','IOS_UA','UA'].includes(v)){
  1502. rule.headers[k] = eval(v);
  1503. }
  1504. }
  1505. }
  1506. }catch (e) {
  1507. console.log('处理headers发生错误:'+e.message);
  1508. }
  1509. }
  1510. // print(rule.headers);
  1511. rule_fetch_params = {'headers': rule.headers||false, 'timeout': rule.timeout, 'encoding': rule.encoding};
  1512. oheaders = rule.headers||{};
  1513. RKEY = typeof(key)!=='undefined'&&key?key:'drpy_' + (rule.title || rule.host);
  1514. init_test();
  1515. }catch (e) {
  1516. console.log('init_test发生错误:'+e.message);
  1517. }
  1518. }
  1519. let homeHtmlCache = undefined;
  1520. /**
  1521. * js源获取首页分类和筛选特定返回对象中的函数
  1522. * @param filter 筛选条件字典对象
  1523. * @returns {string}
  1524. */
  1525. function home(filter) {
  1526. console.log("home");
  1527. let homeObj = {
  1528. filter:rule.filter||false,
  1529. MY_URL: rule.homeUrl,
  1530. class_name: rule.class_name || '',
  1531. class_url: rule.class_url || '',
  1532. class_parse: rule.class_parse || '',
  1533. cate_exclude: rule.cate_exclude,
  1534. };
  1535. return homeParse(homeObj);
  1536. }
  1537. /**
  1538. * js源获取首页推荐数据列表特定返回对象中的函数
  1539. * @param params
  1540. * @returns {string}
  1541. */
  1542. function homeVod(params) {
  1543. let homeVodObj = {
  1544. 推荐:rule.推荐,
  1545. double:rule.double,
  1546. homeUrl:rule.homeUrl,
  1547. detailUrl:rule.detailUrl
  1548. };
  1549. return homeVodParse(homeVodObj)
  1550. // return "{}";
  1551. }
  1552. /**
  1553. * js源获取分类页一级数据列表特定返回对象中的函数
  1554. * @param tid 分类id
  1555. * @param pg 页数
  1556. * @param filter 当前选中的筛选条件
  1557. * @param extend 扩展
  1558. * @returns {string}
  1559. */
  1560. function category(tid, pg, filter, extend) {
  1561. let cateObj = {
  1562. url: rule.url,
  1563. 一级: rule.一级,
  1564. tid: tid,
  1565. pg: parseInt(pg),
  1566. filter: filter,
  1567. extend: extend
  1568. };
  1569. // console.log(JSON.stringify(extend));
  1570. return categoryParse(cateObj)
  1571. }
  1572. /**
  1573. * js源获取二级详情页数据特定返回对象中的函数
  1574. * @param vod_url 一级列表中的vod_id或者是带分类的自拼接 vod_id 如 fyclass$vod_id
  1575. * @returns {string}
  1576. */
  1577. function detail(vod_url) {
  1578. let orId = vod_url;
  1579. let fyclass = '';
  1580. if(vod_url.indexOf('$')>-1){
  1581. let tmp = vod_url.split('$');
  1582. fyclass = tmp[0];
  1583. vod_url = tmp[1];
  1584. }
  1585. let detailUrl = vod_url;
  1586. let url;
  1587. if(!detailUrl.startsWith('http')&&!detailUrl.includes('/')){
  1588. url = rule.detailUrl.replaceAll('fyid', detailUrl).replaceAll('fyclass',fyclass);
  1589. }else if(detailUrl.includes('/')){
  1590. url = urljoin(rule.homeUrl,detailUrl);
  1591. }else{
  1592. url = detailUrl
  1593. }
  1594. let detailObj = {
  1595. orId: orId,
  1596. url:url,
  1597. 二级:rule.二级,
  1598. detailUrl:detailUrl,
  1599. fyclass:fyclass,
  1600. tab_exclude:rule.tab_exclude,
  1601. }
  1602. return detailParse(detailObj)
  1603. }
  1604. /**
  1605. * js源选集按钮播放点击事件特定返回对象中的函数
  1606. * @param flag 线路名
  1607. * @param id 播放按钮的链接
  1608. * @param flags 全局配置的flags是否需要解析的标识列表
  1609. * @returns {string}
  1610. */
  1611. function play(flag, id, flags) {
  1612. let playObj = {
  1613. url:id,
  1614. flag:flag,
  1615. flags:flags
  1616. }
  1617. return playParse(playObj);
  1618. }
  1619. /**
  1620. * js源搜索返回的数据列表特定返回对象中的函数
  1621. * @param wd 搜索关键字
  1622. * @param quick 是否来自快速搜索
  1623. * @returns {string}
  1624. */
  1625. function search(wd, quick) {
  1626. let searchObj = {
  1627. searchUrl: rule.searchUrl,
  1628. 搜索: rule.搜索,
  1629. wd: wd,
  1630. //pg: pg,
  1631. pg: 1,
  1632. quick: quick,
  1633. };
  1634. // console.log(JSON.stringify(searchObj));
  1635. return searchParse(searchObj)
  1636. }
  1637. function DRPY(){//导出函数
  1638. return {
  1639. init: init,
  1640. home: home,
  1641. homeVod: homeVod,
  1642. category: category,
  1643. detail: detail,
  1644. play: play,
  1645. search: search,
  1646. }
  1647. }
  1648. // 导出函数对象
  1649. export default {
  1650. init: init,
  1651. home: home,
  1652. homeVod: homeVod,
  1653. category: category,
  1654. detail: detail,
  1655. play: play,
  1656. search: search,
  1657. DRPY:DRPY
  1658. }