-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVideoDownloader.py
202 lines (178 loc) · 6.08 KB
/
VideoDownloader.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import re
import os
from os.path import isfile, getsize
import urllib
import urllib2
from bs4 import *
"""
A general videodownloader which extracts the video source from any given webpage using regex.
It is still in experimental stage and may or may not work for your source and you are free to make changes to it"""
def request(url):
req = urllib2.Request(url)
req.add_header('User-agent', 'Mozilla 5.10')
return req
def url_extract(url):
try:
req = request(url)
html = urllib2.urlopen(req).read(2000000)
print "loading ....."
src = str(html)
soup = BeautifulSoup(html, "lxml")
name = str(soup.title.contents[0])[0:250]
#regex to extract https and http urls
puller = re.compile(r"http.*?['\";]")
loop = puller.findall(src)
sorted = []
# finding only urls containing video extension
#TODO find a way to check whether it is a video file or not without all this difficulties
for x in loop:
if ('mp4' in x) or ('flv' in x) or (
'm3u8' in x) or ('mpg' in x) or ('wmv' in x):
sorted.append(x)
realsorted = []
for x in sorted:
realsorted.append(x[0:-1])
ssort = []
ssort = list(set(realsorted))
return(ssort, name)
except Exception as e:
print '1:', e
pass
def url_decoder(lists):
try:
d = []
for x in lists:
x = urllib.unquote(x).decode('utf8')
d.append(x)
print x
for x in d:
if x[-1] == '/':
x = x[0:-1]
d.append(x)
if x[-3:] == 'amp':
x = x[0:-3]
d.append(x)
return(d)
except Exception as e:
print '2:', e
pass
def videolink_extract(dsort):
try:
videolink = []
ext = []
size = []
form = []
for x in dsort:
try:
req = request(x)
ht = urllib2.urlopen(req)
meta = ht.info()
m = meta.getheaders("Content-Type")[0]
if 'video' in m:
videolink.append(x)
ext.append(m[-3:])
si = int(
urllib2.urlopen(req).info().getheaders("Content-Length")[0])
size.append(si)
except:
pass
q = re.compile(r"[0-9]{3,4}[Pp]+")
for x in videolink:
try:
f = q.search(x)
form.append(str(f.group()))
except:
form = []
print form
return(ext, size, videolink, form)
# return(ext,size,videolink)
except Exception as e:
print '3:', e
pass
def resume_down(file_name, file_size, url):
req = request(url)
cursize = os.path.getsize(file_name)
req.add_header('Range', 'bytes=%s-%s' % (cursize, file_size))
r = urllib2.urlopen(req)
f = open(file_name, 'ab')
#meta = u.info()
#file_size = int(meta.getheaders("Content-Length")[0])
print "Downloading : %s \nOrginal MegaBytes: %s" % (file_name, float(file_size) / (1024**2))
print "Resuming at : %s" % float(cursize) / (1024**2)
file_size_dl = cursize
block_sz = 8192
while True:
buffer = r.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
f.write(buffer)
status = r"%10d [%3.2f%%]" % (
file_size_dl, file_size_dl * 100. / file_size)
status = status + chr(8) * (len(status) + 1)
print status,
f.flush()
f.close()
def dl(file_name, file_size, url):
try:
count = 1
req = request(url)
u = urllib2.urlopen(req)
if isfile(file_name):
if os.path.getsize(file_name) != file_size:
print "There already exists a file with do you want to resume the download press \'y\' to continue"
rp = raw_input()
if rp == 'y':
resume_down(file_name, file_size, url)
sys.exit()
else:
print "%s is already downloaded press \'n\' to stop the download" % file_name
if raw_input == 'n':
sys.exit(0)
fil = file_name[:-5] + str(count)
count += 1
file_name = fil + file_name[-4:]
f = open(file_name, 'wb')
#meta = u.info()
#file_size = int(meta.getheaders("Content-Length")[0])
print "Downloading: %s \nMegaBytes: %s" % (file_name, float(file_size) / (1024**2))
file_size_dl = 0
block_sz = 8192
while True:
buffer = u.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
f.write(buffer)
status = r"%10d [%3.2f%%]" % (
file_size_dl, file_size_dl * 100. / file_size)
status = status + chr(8) * (len(status) + 1)
print status,
f.flush()
f.close()
except Exception as e:
print '4:', e
pass
if __name__ == "__main__":
try:
url = raw_input("Enter url : ")
print "extracting all the available urls"
(ss, name) = url_extract(url)
print "decoding the available urls"
ds = url_decoder(ss)
print "extracting videolinks"
(ext, size, videolink, form) = videolink_extract(ds)
if (len(form) > 0):
for i in enumerate(form):
print i
else:
for i in enumerate(size):
print i
req = raw_input("press y to continue")
if req == 'y':
v = int(raw_input("enter the index of video quality you want"))
name = name + '.' + ext[v]
dl(name, size[v], videolink[v])
except Exception as e:
print '5:', e
pass