-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvodDownloader.py
167 lines (155 loc) · 4.91 KB
/
vodDownloader.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import os
import sys
from io import BytesIO
import simplejson as json
import pycurl
from termcolor import colored
import argparse
def getIndexUrl(base_url):
c = pycurl.Curl()
b = BytesIO()
c.setopt(c.URL, base_url)
c.setopt(pycurl.SSL_VERIFYPEER, 0)
c.setopt(c.WRITEFUNCTION, b.write)
c.setopt(c.WRITEDATA, b)
c.perform()
vodId = json.loads(b.getvalue())['contentInfo']['contentId']
base_url = 'https://playback.dacast.com/content/access?contentId=' + vodId + '&provider=universe'
finalUrl = ""
try:
b = BytesIO()
c.setopt(c.URL, base_url)
c.setopt(pycurl.SSL_VERIFYPEER, 0)
c.setopt(c.WRITEFUNCTION, b.write)
c.setopt(c.WRITEDATA, b)
c.perform()
tempUrl = json.loads(b.getvalue())['hls']
indexUrl = tempUrl.replace('manifest.m3u8', 'index_0_av.m3u8')
b = BytesIO()
c.setopt(c.URL, indexUrl)
c.setopt(pycurl.SSL_VERIFYPEER, 0)
c.setopt(c.WRITEFUNCTION, b.write)
c.setopt(c.WRITEDATA, b)
c.perform()
data = b.getvalue().decode("utf-8").split('\n')
partialUrl = ""
for i in range(len(data)):
temp = data[i].replace('\'', '').replace('"', '').strip()
if(temp.startswith("stream-audio")):
partialUrl = data[i]
break
dacast = tempUrl.split('stream.ismd')[0] + "stream.ismd/"
finalUrl = dacast + partialUrl
except:
sys.stdout.write("\033[F")
sys.stdout.write("\033[K")
print(colored("[!] Error Downloading: " + fileName, 'red'))
failedVods.append(line.strip())
c.close()
b.close()
return finalUrl
def fetchSegments(base_url):
# Fetch video segments then save it to the m3u8 file
print(colored("[.] Fetching segments...", 'red'))
index_file = open("index_0_av.m3u8", 'wb')
# Build request
curl = pycurl.Curl()
curl.setopt(curl.URL, base_url)
curl.setopt(pycurl.SSL_VERIFYPEER, 0)
curl.setopt(curl.WRITEDATA, index_file)
curl.perform()
# Close everything and print confirmation
curl.close()
index_file.close()
sys.stdout.write("\033[F")
sys.stdout.write("\033[K")
print(colored("[+] Completed fetching segments", 'green'))
def filterSegments():
# To remove the comments from the m3u8 file and save the segments' link to segments.txt
index_file = open("index_0_av.m3u8", 'r')
segments_file = open(fileName + "_segments.txt", 'w')
number_of_lines = 0
for line in index_file:
if '#' not in line and line != '':
segments_file.write(line)
number_of_lines = number_of_lines + 1
index_file.close()
segments_file.close()
os.remove("index_0_av.m3u8")
return number_of_lines
def downloadSegments():
number_of_lines = filterSegments()
# Build progress bar
progress_bar = "[" + " " * 100 + "]"
progress_bar_counter = 1
print(colored("Downloading ==> ", 'red') + colored("".join(progress_bar), 'green'))
segments_file = open(fileName + "_segments.txt", 'r')
video = open(fileName + ".ts", 'wb')
# Start fetching
for url in segments_file:
# Build request
curl = pycurl.Curl()
curl.setopt(curl.WRITEDATA, video)
curl.setopt(curl.URL, dacastUrl + url.strip())
downloaded = True
try:
curl.perform()
# Update progress bar
loaded = int((progress_bar_counter / number_of_lines) * 100)
progress_bar = "[" + "=" * loaded + " " * (100 - loaded) + "]"
progress_bar_counter += 1
sys.stdout.write("\033[F")
sys.stdout.write("\033[K")
print(colored("Downloading " + str(loaded) + "% ==> ", 'red') + colored("".join(progress_bar), 'green'))
# Close to start new session due to target server limits
curl.close()
except:
sys.stdout.write("\033[F")
sys.stdout.write("\033[K")
sys.stdout.write("\033[F")
sys.stdout.write("\033[K")
print(colored("[!] Error Downloading: " + fileName, 'red'))
failedVods.append(line.strip())
downloaded = False
break
if downloaded:
sys.stdout.write("\033[F")
sys.stdout.write("\033[K")
print(colored("[+] Downloaded", 'green'))
# Convert & Close everything
segments_file.close()
os.remove(fileName + "_segments.txt")
if(convertVods):
print(colored("[-] Converting", 'red'))
os.system('ffmpeg -y -i "' + fileName + '.ts" "' + fileName + '" -loglevel quiet')
os.remove(fileName + ".ts")
sys.stdout.write("\033[F")
sys.stdout.write("\033[K")
sys.stdout.write("\033[F")
sys.stdout.write("\033[K")
sys.stdout.write("\033[F")
sys.stdout.write("\033[K")
print(colored("[+] Done downloading " + fileName, 'green'))
# Starting the everything
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--convert", help="Convert to mp4", action="store_true")
convertVods = parser.parse_args().convert
try:
vods = open("VODs.txt", 'r')
except:
print(colored("[+] No VODs to download", 'green'))
sys.exit()
failedVods = []
for line in vods:
fileName = line.split('==')[0].strip()
base_url = getIndexUrl(line.split('==')[1].strip())
dacastUrl = base_url.split('stream.ismd')[0] + "stream.ismd/"
if(base_url):
fetchSegments(base_url)
downloadSegments()
vods.close()
os.remove("VODs.txt")
if len(failedVods) > 0:
with open('VODs.txt', 'w') as f:
for item in failedVods:
f.write("%s\n" % item)