在线直播转点播.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /**
  2. * live2cms.js
  3. * 配置设置 {"key":"Live2CMS","name":"直播转点播V2","type":3,"api":"{{host}}/libs/live2cms.js","searchable":2,"quickSearch":0,"filterable":0,"ext":"{{host}}/txt/json/live2mv_data.json"}
  4. * live2mv_data.json
  5. [
  6. {"name": "甜蜜", "url": "./40136.txt"},
  7. {"name": "巧计", "url": "./40144.txt"},
  8. {"name": "星辰", "url": "./40152.txt"},
  9. {"name": "俊于", "url": "./40160.txt"},
  10. {"name": "多多", "url": "./40168.txt"},
  11. {"name": "菜妮丝", "url": "./40176.txt"},
  12. {"name": "乱世", "url": "./40184.txt"},
  13. {"name": "吾爱", "url": "./40192.txt"},
  14. {"name": "饭太硬", "url": "./40200.txt"}
  15. ]
  16. * 提示 ext文件格式为json列表,name,url参数
  17. * 取消加密,减少性能问题
  18. */
  19. String.prototype.rstrip = function (chars) {
  20. let regex = new RegExp(chars + "$");
  21. return this.replace(regex, "");
  22. };
  23. const request_timeout = 5000;
  24. const VERSION = 'live2cms 20230616';
  25. const UA = 'Mozilla/5.0'; //默认请求ua
  26. const __ext = {data_dict:{}};
  27. const tips = `\n道长直播转点播js-当前版本${VERSION}`;
  28. const def_pic = 'https://5b0988e595225.cdn.sohucs.com/q_70,c_zoom,w_640/images/20180504/82b2eff6f5bf489e9b4a90fbe9fc7e84.jpeg';
  29. /*https://avatars.githubusercontent.com/u/97389433?s=120&v=4';
  30. /**
  31. * 打印日志
  32. * @param any 任意变量
  33. */
  34. function print(any){
  35. any = any||'';
  36. if(typeof(any)=='object'&&Object.keys(any).length>0){
  37. try {
  38. any = JSON.stringify(any);
  39. console.log(any);
  40. }catch (e) {
  41. // console.log('print:'+e.message);
  42. console.log(typeof(any)+':'+any.length);
  43. }
  44. }else if(typeof(any)=='object'&&Object.keys(any).length<1){
  45. console.log('null object');
  46. }else{
  47. console.log(any);
  48. }
  49. }
  50. /*** js自封装的方法 ***/
  51. /**
  52. * 获取链接的host(带http协议的完整链接)
  53. * @param url 任意一个正常完整的Url,自动提取根
  54. * @returns {string}
  55. */
  56. function getHome(url){
  57. if(!url){
  58. return ''
  59. }
  60. let tmp = url.split('//');
  61. url = tmp[0] + '//' + tmp[1].split('/')[0];
  62. try {
  63. url = decodeURIComponent(url);
  64. }catch (e) {}
  65. return url
  66. }
  67. const http = function (url, options = {}) {
  68. if(options.method ==='POST' && options.data){
  69. options.body = JSON.stringify(options.data);
  70. options.headers = Object.assign({'content-type':'application/json'}, options.headers);
  71. }
  72. options.timeout = request_timeout;
  73. if(!options.headers){
  74. options.headers = {};
  75. }
  76. let keys = Object.keys(options.headers).map(it=>it.toLowerCase());
  77. if(!keys.includes('referer')){
  78. options.headers['Referer'] = getHome(url);
  79. }
  80. if(!keys.includes('user-agent')){
  81. options.headers['User-Agent'] = UA;
  82. }
  83. console.log(JSON.stringify(options.headers));
  84. try {
  85. const res = req(url, options);
  86. // if(options.headers['Authorization']){
  87. // console.log(res.content);
  88. // }
  89. res.json = () => res&&res.content ? JSON.parse(res.content) : null;
  90. res.text = () => res&&res.content ? res.content:'';
  91. return res
  92. }catch (e) {
  93. return {
  94. json() {
  95. return null
  96. }, text() {
  97. return ''
  98. }
  99. }
  100. }
  101. };
  102. ["get", "post"].forEach(method => {
  103. http[method] = function (url, options = {}) {
  104. return http(url, Object.assign(options, {method: method.toUpperCase()}));
  105. }
  106. });
  107. function init(ext) {
  108. console.log("当前版本号:"+VERSION);
  109. let data;
  110. if (typeof ext == 'object'){
  111. data = ext;
  112. print('live ext:object');
  113. } else if (typeof ext == 'string') {
  114. if (ext.startsWith('http')) {
  115. let ext_paramas = ext.split(';');
  116. let data_url = ext_paramas[0];
  117. print(data_url);
  118. data = http.get(data_url).json();
  119. }
  120. }
  121. print(data);
  122. __ext.data = data;
  123. print('init执行完毕');
  124. }
  125. function home(filter) {
  126. let classes = __ext.data.map(it => ({
  127. type_id: it.url,
  128. type_name: it.name,
  129. }));
  130. print("----home----");
  131. print(classes);
  132. return JSON.stringify({ 'class': classes});
  133. }
  134. function homeVod(params) {
  135. let _get_url = __ext.data[0].url;
  136. let html = http.get(_get_url).text();
  137. let arr = html.match(/.*?,#[\s\S].*?#/g);
  138. let _list = [];
  139. try {
  140. arr.forEach(it=>{
  141. _list.push({
  142. vod_name:it.split(',')[0],
  143. vod_id:_get_url+'$'+it.split(',')[0],
  144. vod_pic:def_pic,
  145. vod_remarks:it.split(',')[1],
  146. });
  147. });
  148. }catch (e) {
  149. print('Live2cms获取首页推荐发送错误:'+e.message);
  150. }
  151. return JSON.stringify({ 'list': _list });
  152. }
  153. function category(tid, pg, filter, extend) {
  154. let _get_url = tid;
  155. let html;
  156. if(__ext.data_dict[_get_url]){
  157. html = __ext.data_dict[_get_url];
  158. }else{
  159. html = http.get(_get_url).text();
  160. __ext.data_dict[_get_url] = html;
  161. }
  162. let arr = html.match(/.*?,#[\s\S].*?#/g);
  163. let _list = [];
  164. try {
  165. arr.forEach(it=>{
  166. _list.push({
  167. vod_name:it.split(',')[0],
  168. vod_id:_get_url+'$'+it.split(',')[0],
  169. vod_pic:def_pic,
  170. vod_remarks:it.split(',')[1],
  171. });
  172. });
  173. }catch (e) {
  174. print('Live2cms获取一级分类页发生错误:'+e.message);
  175. }
  176. return JSON.stringify({
  177. 'page': 1,
  178. 'pagecount': 1,
  179. 'limit': _list.length,
  180. 'total': _list.length,
  181. 'list': _list,
  182. });
  183. }
  184. function detail(tid) { // ⛵ 港•澳•台
  185. let _get_url = tid.split('$')[0];
  186. let _tab = tid.split('$')[1];
  187. if(tid.includes('#search#')){
  188. let vod_play_url = _tab.replace('#search#','')+'$'+_get_url;
  189. print(vod_play_url);
  190. return JSON.stringify({
  191. list: [{
  192. vod_id: tid,
  193. vod_name: '搜索:'+_tab.replace('#search#',''),
  194. type_name: "直播列表",
  195. vod_pic: def_pic,
  196. vod_content: tid,
  197. vod_play_from: '来自搜索',
  198. vod_play_url: vod_play_url,
  199. vod_director: tips,
  200. vod_remarks: `道长直播转点播js-当前版本${VERSION}`,
  201. }]
  202. });
  203. }
  204. let html;
  205. if(__ext.data_dict[_get_url]){
  206. html = __ext.data_dict[_get_url];
  207. }else{
  208. html = http.get(_get_url).text();
  209. __ext.data_dict[_get_url] = html;
  210. }
  211. let a = new RegExp(`.*?${_tab},#[\\s\\S].*?#`);
  212. let b = html.match(a)[0];
  213. let c = html.split(b)[1];
  214. if(c.match(/.*?,#[\s\S].*?#/)){
  215. let d = c.match(/.*?,#[\s\S].*?#/)[0];
  216. c = c.split(d)[0];
  217. }
  218. let arr = c.trim().split('\n');
  219. let _list = [];
  220. arr.forEach((it)=>{
  221. if(it.trim()){
  222. let t = it.trim().split(',')[0];
  223. let u = it.trim().split(',')[1];
  224. _list.push(t+'$'+u);
  225. }
  226. });
  227. let vod_name = __ext.data.find(x=>x.url===_get_url).name;
  228. let vod_play_url = _list.join('#');
  229. let vod = {
  230. vod_id: tid,
  231. vod_name: vod_name+'|'+_tab,
  232. type_name: "直播列表",
  233. vod_pic: def_pic,
  234. vod_content: tid,
  235. vod_play_from: vod_name,
  236. vod_play_url: vod_play_url,
  237. vod_director: tips,
  238. vod_remarks: `道长直播转点播js-当前版本${VERSION}`,
  239. };
  240. return JSON.stringify({
  241. list: [vod]
  242. });
  243. }
  244. function play(flag, id, flags) {
  245. let vod = {
  246. 'parse': /m3u8/.test(id)?0:1,
  247. 'playUrl': '',
  248. 'url': id
  249. };
  250. print(vod);
  251. return JSON.stringify(vod);
  252. }
  253. function search(wd, quick) {
  254. let _get_url = __ext.data[0].url;
  255. let html;
  256. if(__ext.data_dict[_get_url]){
  257. html = __ext.data_dict[_get_url];
  258. }else{
  259. html = http.get(_get_url).text();
  260. __ext.data_dict[_get_url] = html;
  261. }
  262. let str='';
  263. Object.keys(__ext.data_dict).forEach(()=>{
  264. str+=__ext.data_dict[_get_url];
  265. });
  266. let links = str.split('\n').filter(it=>it.trim() && it.includes(','));
  267. links = links.map(it=>it.trim());
  268. let plays = Array.from(new Set(links));
  269. print('搜索关键词:'+wd);
  270. print('过滤前:'+plays.length);
  271. plays = plays.filter(it=>it.includes(wd));
  272. print('过滤后:'+plays.length);
  273. print(plays);
  274. let _list = [];
  275. plays.forEach((it)=>{
  276. _list.push({
  277. 'vod_name':it.split(',')[0],
  278. 'vod_id':it.split(',')[1].trim()+'$'+it.split(',')[0].trim()+'#search#',
  279. 'vod_pic':def_pic,
  280. })
  281. });
  282. return JSON.stringify({
  283. 'list': _list
  284. });
  285. }
  286. // 导出函数对象
  287. export default {
  288. init: init,
  289. home: home,
  290. homeVod: homeVod,
  291. category: category,
  292. detail: detail,
  293. play: play,
  294. search: search
  295. }