|
| 1 | +import re, os |
| 2 | +import requests |
| 3 | +from parsel import Selector |
| 4 | +from tmoockey import download_key, download_ts, aes_key_and_ts, save_mp4 |
| 5 | + |
| 6 | + |
| 7 | +headers = { |
| 8 | + 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3', |
| 9 | + 'Accept-Encoding': 'gzip, deflate', |
| 10 | + 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8', |
| 11 | + 'Cache-Control': 'max-age=0', |
| 12 | + 'Connection': 'keep-alive', |
| 13 | + 'Cookie': 'xxxxxxxxxxxxxxxxxxx', |
| 14 | + 'Host': 'tts.tmooc.cn', |
| 15 | + 'Referer': 'http://tts.tmooc.cn/studentCenter/toMyttsPage?versionCode=CGB_A_V02', |
| 16 | + 'Upgrade-Insecure-Requests': '1', |
| 17 | + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36', |
| 18 | +} |
| 19 | + |
| 20 | +headers2 = { |
| 21 | + 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3', |
| 22 | + 'Accept-Encoding': 'gzip, deflate', |
| 23 | + 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8', |
| 24 | + 'Cache-Control': 'max-age=0', |
| 25 | + 'Connection': 'keep-alive', |
| 26 | + 'Cookie': 'xxxxxxxxxxxx', |
| 27 | + 'Host': 'tts.tmooc.cn', |
| 28 | + 'Upgrade-Insecure-Requests': '1', |
| 29 | + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36', |
| 30 | + |
| 31 | +} |
| 32 | + |
| 33 | +headers3 = { |
| 34 | + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36', |
| 35 | +} |
| 36 | + |
| 37 | +def get_menuId(url): |
| 38 | + |
| 39 | + response = requests.get(url, headers=headers) |
| 40 | + selectors = Selector(text=response.text).xpath('//div[@class="course-menu"]/ul/li[@class="opened"]') |
| 41 | + for selector in selectors: |
| 42 | + video_name = selector.xpath('./p/text()').get().strip() |
| 43 | + video_href = selector.xpath('./ul/li[@class="sp"]/a/@href').get() |
| 44 | + video_name = ''.join(video_name.split()) |
| 45 | + video_href = video_href |
| 46 | + yield video_name, video_href |
| 47 | + |
| 48 | + |
| 49 | +def get_m3u8(m3u8_url): |
| 50 | + # m3u8_url = 'https://hls.videocc.net/01b768ec8c/0/01b768ec8c4739f79cec9964836b5800_2.m3u8?pid=1580626301758X1133587&device=desktop' |
| 51 | + # m3u8_url = 'https://hls.videocc.net/01b768ec8c/e/01b768ec8c3d4e068ad411ccc0b9342e_2.m3u8?pid=1580632138310X1619848&device=desktop' |
| 52 | + response_m3u8 = requests.get(m3u8_url, headers=headers3) |
| 53 | + print(response_m3u8.text) |
| 54 | + # 提取key的url |
| 55 | + url_key = re.search('URI="(.*?)"', response_m3u8.text).group(1) |
| 56 | + #'http://c.it211.com.cn/cgb19020226pm/static.key' |
| 57 | + key = download_key(url_key) |
| 58 | + url_ts = re.findall(r'https://.*?\.ts\?pid=.*?&device=desktop', response_m3u8.text) |
| 59 | + print(url_ts) |
| 60 | + for ts_url in url_ts: |
| 61 | + print(ts_url) |
| 62 | + ts = download_ts(ts_url) |
| 63 | + data = aes_key_and_ts(key, ts) |
| 64 | + video_path = '第6讲 【作】写人篇·我的好朋友' |
| 65 | + save_mp4(video_path, data) |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == '__main__': |
| 69 | + m3u8_url = 'https://hls.videocc.net/01b768ec8c/7/01b768ec8c751c7b09232998c15b6837_2.m3u8?pid=1580646548553X1389651&device=desktop' |
| 70 | + get_m3u8(m3u8_url) |
0 commit comments