forked from alexnormand/savevideo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
savevideo.py
executable file
·103 lines (78 loc) · 3.06 KB
/
savevideo.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
#! /usr/bin/env python
import urllib
import urllib2
import argparse
import sys
import re
import time
from webbrowser import open_new_tab
from urlparse import urlparse
def get_download_links(url):
"""Returns a list of links to download the video from th url"""
requestHeaders = {
"Origin": "http://savevideo.me",
"X-Requested-With": "XMLHttpRequest",
"User-Agent": "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2",
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "*/*",
"Referer": "http://savevideo.me/?lang=en",
"Accept-Language": "en-US,en;q=0.8",
"Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.3"
}
request = urllib2.Request('http://savevideo.me/get/',
urllib.urlencode({"url": url}),
requestHeaders)
try:
response = urllib2.urlopen(request)
xml = response.read()
pattern = r'<a\s+href="(.*?)"\s*>.*?</a>(.*?)<br\s*/>'
links = re.findall(pattern, xml)
return links
except urllib2.HTTPError, error:
print error.read()
def download_video(url):
o = urlparse(url)
timestamp = int(time.mktime(time.localtime()))
file_name = str(timestamp) + o.path.split('/')[-1]
u = urllib2.urlopen(url)
f = open(file_name, 'wb')
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0]) / 1048576.
print "Downloading: %s Size: %.2f MB" % (file_name, file_size)
file_size_dl = 0
block_sz = 8192
while True:
buffer = u.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer) / 1048576.
f.write(buffer)
status = r"%.2f MB [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
status = status + chr(8)*(len(status)+1)
print status,
f.close()
def main():
parser = argparse.ArgumentParser(description='Download videos\
from various video hosting sites\
(vimeo, bliptv, dailymotion,...)')
parser.add_argument("-cli",
help="Download link on the command line instead\
of opening link in the system's default browser ",
action='store_true')
parser.add_argument("url",
help="The url of the file you wish to download")
args = parser.parse_args()
links = get_download_links(args.url)
if links is not None:
print "Choose File you wish to Download"
for i, link in enumerate(links):
print "[%d] : %s" % (i, link[1])
chosen_link = int(raw_input('> '))
if args.cli:
download_video(links[chosen_link][0])
else:
open_new_tab(links[chosen_link][0])
else:
print "Service unavailable please try again"
if __name__ == "__main__":
main()