1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| import requests import os import json,time
lessonID = "4003249"
header = { 'cookie':"_ga=GA1.2.1597694283.1582103010; _did=web_2408385821B4C392; _gid=GA1.2.237363874.1585559892; csrftoken=UFomwya8SZP3HFR5gv9GnTQ6ChyipTSI; sessionid=o3lwkaqztkxg629ahwfnf3037jvwl9kl; django_language=zh-cn" }
url_lesson = "https://changjiang.yuketang.cn/v/lesson/get_lesson_replay_timeline/?lesson_id={}".format(lessonID)
resp = requests.get(url_lesson, headers=header) dic_info = json.loads(resp.text)
m3u8_list = dic_info['data']['live_timeline']
num = 1 for i in m3u8_list: url_m3u8 = i["replay_url"] print(url_m3u8) if not os.path.exists('./{}/'.format(lessonID)): os.mkdir('./{}/'.format(lessonID)) if '.m3u8' in url_m3u8: filename_m3u8 = "./{}/{}".format(lessonID,url_m3u8.split('/')[-1]) resp = requests.get(url_m3u8, headers=header)
with open(filename_m3u8, 'w', encoding='utf8') as f: f.write(resp.text)
dataTS = [] with open(filename_m3u8, 'r', encoding='utf8') as f: data = f.readlines(); for i in data: if i.endswith(".ts\n"): dataTS.append(i[0:-1])
url_ts_prefix = os.path.dirname(url_m3u8)+"/" with open("./{}/{}.ts".format(lessonID,url_m3u8.split('/')[-1].split('.')[0]), 'wb') as f: for ts in dataTS: nurl = url_ts_prefix + ts print(nurl) resp = requests.get(nurl) f.write(resp.content) elif '.mp3' in url_m3u8: resp = requests.get(url_m3u8) filename = "together_"+url_m3u8.split('?')[0].split('/')[-1] with open("./{}/{}".format(lessonID, filename), 'ab+') as f: f.write(resp.content)
filename = str(num)+"_"+url_m3u8.split('?')[0].split('/')[-1] with open("./{}/{}".format(lessonID, filename), 'wb') as f: f.write(resp.content) num += 1 else: print("未知URL {}".format(url_m3u8))
url_lesson_info = "https://changjiang.yuketang.cn/v/lesson/get_lesson_replay_content/?lesson_id={}".format(lessonID) resp = requests.get(url_lesson_info, headers=header) title = json.loads(resp.text)["data"]['lesson']['title'] if not os.path.exists('./{}'.format(lessonID+"_"+title)): os.rename('./{}'.format(lessonID), './{}'.format(lessonID+"_"+title)) else: os.rename('./{}'.format(lessonID+"_"+title),'./{}_{}'.format(lessonID+"_"+title, int(time.time()))) os.rename('./{}'.format(lessonID), './{}'.format(lessonID+"_"+title))
|