web.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # File : vod.py
  4. # Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
  5. # Date : 2022/9/6
  6. import functools
  7. import json
  8. import os
  9. from urllib.parse import urljoin, unquote,quote
  10. import requests
  11. from flask import Blueprint, abort, request, render_template, send_from_directory, render_template_string, jsonify, \
  12. make_response, redirect, \
  13. current_app, url_for
  14. from time import time
  15. from utils.web import getParmas, get_interval
  16. from utils.cfg import cfg
  17. from utils.env import get_env
  18. from js.rules import getRuleLists, getJxs
  19. from base.R import R
  20. from utils.log import logger
  21. from utils import parser
  22. from controllers.cms import CMS
  23. from base.database import db
  24. from models.ruleclass import RuleClass
  25. from models.playparse import PlayParse
  26. from js.rules import getRules
  27. from controllers.service import storage_service, rules_service
  28. from concurrent.futures import ThreadPoolExecutor, as_completed, thread # 引入线程池
  29. from quickjs import Function, Context
  30. import ujson
  31. # web = Blueprint("web", __name__, template_folder='templates/cmsV10/mxpro/html/index/')
  32. web = Blueprint("web", __name__)
  33. @web.route('/cms/<path:filename>')
  34. def custom_static_cms(filename):
  35. # 自定义静态目录 {{ url_for('custom_static',filename='help.txt')}}
  36. # print(filename)
  37. return send_from_directory('templates/cms', filename)
  38. @web.route('/player/<path:filename>')
  39. def custom_static_player(filename):
  40. # 自定义静态目录 {{ url_for('custom_static',filename='help.txt')}}
  41. # print(filename)
  42. return send_from_directory('templates/player', filename)
  43. @web.route('/player1')
  44. def custom_player1():
  45. ctx = getParmas()
  46. return render_template('player/mui/index.html', ctx=ctx)
  47. @web.route('/player2')
  48. def custom_player2():
  49. ctx = getParmas()
  50. return render_template('player/p2p-media-loader/p2pm3u8.html', ctx=ctx)
  51. @web.route('/player3')
  52. def custom_player3():
  53. ctx = getParmas()
  54. return render_template('player/p2pplayer/index.htm', ctx=ctx)
  55. @web.route('/player4')
  56. def custom_player4():
  57. ctx = getParmas()
  58. return render_template('player/p2phls/p2phls.html', ctx=ctx)
  59. @web.route('/<web_name>/<theme>')
  60. def web_index(web_name, theme):
  61. ctx = {'web_name': web_name, 'key': '关键词', 'description': '描述'}
  62. lsg = storage_service()
  63. js0_password = lsg.getItem('JS0_PASSWORD')
  64. ctx['pwd'] = js0_password
  65. ctx['path'] = request.path
  66. ctx['url'] = request.url
  67. vod_id = getParmas('vod_id')
  68. vod_name = getParmas('vod_name')
  69. wd = getParmas('wd')
  70. pg = getParmas('pg') or '1'
  71. tid = getParmas('tid')
  72. tname = getParmas('tname')
  73. url = getParmas('url')
  74. fl = getParmas('f')
  75. player = getParmas('player') or 'mui'
  76. ctx['vod_id'] = vod_id
  77. ctx['vod_name'] = vod_name
  78. ctx['wd'] = wd
  79. ctx['pg'] = pg
  80. ctx['tid'] = tid
  81. ctx['tname'] = tname
  82. ctx['url'] = url
  83. ctx['fl'] = quote(fl)
  84. print('tid:', tid,'fl:',fl)
  85. # print('f:', fl)
  86. file_path = os.path.abspath(f'js/{web_name}.js')
  87. print(file_path)
  88. if not os.path.exists(file_path):
  89. return render_template('404.html', ctx=ctx, error=f'发生错误的原因可能是下面路径未找到:{file_path}')
  90. try:
  91. if url:
  92. return render_template(f'player/{player}/index.html', ctx=ctx)
  93. elif vod_id and vod_name:
  94. return render_template(f'cms/{theme}/detailContent.html', ctx=ctx)
  95. elif wd:
  96. return render_template(f'cms/{theme}/searchContent.html', ctx=ctx)
  97. elif tid:
  98. return render_template(f'cms/{theme}/categoryContent.html', ctx=ctx)
  99. else:
  100. return render_template(f'cms/{theme}/homeContent.html', ctx=ctx)
  101. except Exception as e:
  102. return render_template('404.html', ctx=ctx, error=f'发生错误的原因可能是下面路径未找到:{e}')
  103. @web.route('/302redirect')
  104. def get302UrlResponse():
  105. url = getParmas('url')
  106. if not url:
  107. abort(403)
  108. params = {}
  109. if not url.startswith('http'):
  110. url = urljoin(request.root_url, url)
  111. # url = urljoin('http://localhost:5705/',url)
  112. print(url)
  113. items = url.split('vod?')[1].split('&')
  114. for item in items:
  115. params[item.split('=')[0]] = item.split('=')[1]
  116. print(params)
  117. # abort(403)
  118. timeout = getParmas('timeout') or 5000
  119. rurl = url
  120. try:
  121. timeout = int(timeout)
  122. headers = {
  123. # 'referer': url,
  124. 'user-agent': 'Mozilla/5.0'
  125. }
  126. logger.info(f'开始调用接口:{url}')
  127. r = requests.get(url, headers=headers, timeout=timeout, verify=False)
  128. rurl = r.url
  129. res_data = r.text
  130. try:
  131. res_data = r.json()
  132. except:
  133. pass
  134. # rurl = url_for('vod.vod_home', **params)
  135. # print(rurl)
  136. logger.info(f'结束调用接口:{rurl}')
  137. is_redirect = unquote(rurl) != unquote(url)
  138. return jsonify({
  139. 'url': rurl,
  140. 'redirect': is_redirect,
  141. 'data': res_data,
  142. })
  143. except Exception as e:
  144. logger.info(f'发生了错误:{e}')
  145. return jsonify({
  146. 'url': rurl,
  147. 'redirect': False,
  148. 'data': None,
  149. 'error': f'{e}',
  150. })