forked from hcjohn463/JableTVDownload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
129 lines (86 loc) · 2.2 KB
/
main.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# author: hcjohn463
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import requests
import os
import re
import urllib.request
import m3u8
from Crypto.Cipher import AES
from config import headers
from crawler import prepareCrawl
from merge import mergeMp4
from delete import deleteM3u8, deleteMp4
from cover import get_cover
import time
import cloudscraper
from args import *
# In[2]:
parser = get_parser()
args = parser.parse_args()
if(len(args.url) != 0):
url = args.url
elif(args.random == True):
url = av_recommand()
else:
# 使用者輸入Jable網址
url = input('輸入jable網址:')
# In[3]:
# 建立番號資料夾
urlSplit = url.split('/')
dirName = urlSplit[-2]
if not os.path.exists(dirName):
os.makedirs(dirName)
folderPath = os.path.join(os.getcwd(), dirName)
# In[4]:
# 得到 m3u8 網址
htmlfile = cloudscraper.create_scraper(browser='firefox', delay=10).get(url)
result = re.search("https://.+m3u8", htmlfile.text)
m3u8url = result[0]
m3u8urlList = m3u8url.split('/')
m3u8urlList.pop(-1)
downloadurl = '/'.join(m3u8urlList)
# 儲存 m3u8 file 至資料夾
m3u8file = os.path.join(folderPath, dirName + '.m3u8')
urllib.request.urlretrieve(m3u8url, m3u8file)
# In[5]:
# 得到 m3u8 file裡的 URI和 IV
m3u8obj = m3u8.load(m3u8file)
m3u8uri = ''
m3u8iv = ''
for key in m3u8obj.keys:
if key:
m3u8uri = key.uri
m3u8iv = key.iv
# 儲存 ts網址 in tsList
tsList = []
for seg in m3u8obj.segments:
tsUrl = downloadurl + '/' + seg.uri
tsList.append(tsUrl)
# In[6]:
# 有加密
if m3u8uri:
m3u8keyurl = downloadurl + '/' + m3u8uri # 得到 key 的網址
# 得到 key的內容
response = requests.get(m3u8keyurl, headers=headers, timeout=10)
contentKey = response.content
vt = m3u8iv.replace("0x", "")[:16].encode() # IV取前16位
ci = AES.new(contentKey, AES.MODE_CBC, vt) # 建構解碼器
else:
ci = ''
# In[7]:
# 刪除m3u8 file
deleteM3u8(folderPath)
# In[8]:
# 開始爬蟲並下載mp4片段至資料夾
prepareCrawl(ci, folderPath, tsList)
# In[9]:
# 合成mp4
mergeMp4(folderPath, tsList)
# In[10]:
# 刪除子mp4
deleteMp4(folderPath)
# In[11]:
# get cover
get_cover(html_file=htmlfile, folder_path=folderPath)