|
本帖最后由 doubebly 于 2024-4-17 15:25 编辑
- # -*- coding: utf-8 -*-
- # @Author : Doubebly
- # @Time : 2024/4/17 14:16
- # @Function:
- """
- 深圳卫视,http://127.0.0.1:5000/iptv/1.m3u8
- 都市频道,http://127.0.0.1:5000/iptv/2.m3u8
- 电视剧频道,http://127.0.0.1:5000/iptv/3.m3u8
- 公共频道,http://127.0.0.1:5000/iptv/4.m3u8
- 财*频道,http://127.0.0.1:5000/iptv/5.m3u8
- 娱乐生活频道,http://127.0.0.1:5000/iptv/6.m3u8
- 少儿频道,http://127.0.0.1:5000/iptv/7.m3u8
- 移动电视,http://127.0.0.1:5000/iptv/8.m3u8
- 宜和购物频道,http://127.0.0.1:5000/iptv/9.m3u8
- 国际频道,http://127.0.0.1:5000/iptv/10.m3u8
- """
- import re
- import time
- import hashlib
- import requests
- import urllib3
- import base64
- from flask import Flask, Response
- from flask_cors import CORS
- urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
- app = Flask(__name__)
- CORS(app)
- tv_list = {
- '1': ['AxeFRth', '7867', '深圳卫视'],
- '2': ['ZwxzUXr', '7868', '都市频道'],
- '3': ['4azbkoY', '7880', '电视剧频道'],
- '4': ['2q76Sw2', '7874', '公共频道'],
- '5': ['3vlcoxP', '7871', '财*频道'],
- '6': ['1q4iPng', '7872', '娱乐生活频道'],
- '7': ['1SIQj6s', '7881', '少儿频道'],
- '8': ['wDF6KJ3', '7869', '移动电视'],
- '9': ['BJ5u5k2', '7878', '宜和购物频道'],
- '10': ['sztvgjpd', '7944', '国际频道'],
- }
- def get_header():
- headers = {
- "Accept": "application/json, text/javascript, */*; q=0.01",
- "Accept-Language": "zh-CN,zh;q=0.9",
- "Cache-Control": "no-cache",
- "Connection": "keep-alive",
- "Origin": "https://www.sztv.com.cn",
- "Pragma": "no-cache",
- "Referer": "https://www.sztv.com.cn/",
- "Sec-Fetch-Dest": "empty",
- "Sec-Fetch-Mode": "cors",
- "Sec-Fetch-Site": "cross-site",
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
- "sec-ch-ua-mobile": "?0"
- }
- return headers
- def md5(a):
- return hashlib.md5(a.encode()).hexdigest()
- def deb64(e):
- return base64.b64decode(e.encode()).decode()
- def ba(a):
- b = int(len(a) - len(a) / 2)
- a = a[b:] + a[0:b]
- b = list(a)
- b.reverse()
- return deb64(''.join(b))
- def get_live_key(live_id):
- url = 'https://cls2.cutv.com/getCutvHlsLiveKey'
- t = str(int(time.time()))
- params = {
- "t": t,
- "id": live_id,
- "token": md5(t + live_id + 'cutvLiveStream|Dream2017'),
- "at": "1"
- }
- response = requests.get(url, headers=get_header(), params=params)
- # print(response.json())
- return response.json()
- def get_cdn_key(e):
- url = 'https://sttv2-api.cutv.com/api/getCDNkey.php'
- t = str(int(time.time() * 1000))
- params = {
- "domain": "sztv-live.cutv.com",
- "page": "https://www.sztv.com.cn/pindao/index.html?id=" + e,
- "token": md5('iYKkRHlmUanQGaNMIJziWOkNsztv-live.cutv.com' + t),
- "t": t
- }
- response = requests.get(url, headers=get_header(), params=params, verify=False)
- return response.json()['key']
- def get_m3u8_text(e):
- live_id = tv_list[e][0]
- live_key = ba(get_live_key(live_id))
- t = hex(int(time.time()))[2:]
- key = get_cdn_key(tv_list[e][1])
- sign = md5(f'{key}/{live_id}/500/{live_key}.m3u8{t}')
- url = f'https://sztv-live.cutv.com/{live_id}/500/{live_key}.m3u8?sign={sign}&t={t}'
- response = requests.get(url, headers=get_header())
- m3u8_text = re.sub('(.*.ts.*)', f'/iptv/{live_id}/500/\\1', response.text)
- return m3u8_text
- def get_ts(url):
- try:
- res = requests.get('https://sztv-live.cutv.com/' + url, headers=get_header(), timeout=5)
- if res.status_code == 200:
- return res.content
- else:
- return get_ts(url)
- except requests.exceptions.RequestException:
- return get_ts(url)
- @app.route('/')
- def home():
- return {'StatusCode': 200}
- @app.route('/iptv/<path:path>')
- def iptv(path):
- if 'm3u8' in path:
- ids = path.split('.')[0]
- filtered_content = get_m3u8_text(ids)
- headers = {
- 'Content-Type': 'application/vnd.apple.mpegurl',
- 'Access-Control-Allow-Origin': '*',
- }
- # print(filtered_content)
- return Response(filtered_content, headers=headers)
- elif 'ts' in path:
- ts_content = get_ts(path)
- return Response(ts_content, mimetype='video/mp2t')
- else:
- return {'code': 400}
- if __name__ == '__main__':
- app.run()
- pass
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|