forked from dennisbruner/zsdl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzsdl.py
executable file
·152 lines (125 loc) · 4.52 KB
/
zsdl.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
#!/usr/bin/python3
'''
MIT License
Copyright (c) 2018 Dennis Bruner
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
import sys
import re
import math
import requests
import argparse
from urllib.parse import unquote
from os.path import join
LINK_PATTERN = re.compile(r'https?:\/\/www([\d]*)\.zippyshare\.com\/v\/([\w\d]*)\/file\.html')
INFO_PATTERN = re.compile(r'document\.getElementById\(\'dlbutton\'\)\.href = \"\/d\/([\w\d]*)\/\" \+ \(([\d]*) % ([\d]*) \+ ([\d]*) % ([\d]*)\) \+ \"\/(.*)\";')
# Source: https://stackoverflow.com/a/14822210
def convert_size(size_bytes):
if size_bytes == 0:
return '0 B'
size_name = ('B', 'KiB', 'MiB', 'GiB', 'TiB')
i = int(math.floor(math.log(size_bytes, 1024)))
p = math.pow(1024, i)
s = round(size_bytes / p, 2)
return '%s %s' % (s, size_name[i])
# Custom errors
class InvalidLinkException(Exception):
pass
class UnavailableFileException(Exception):
pass
# Remote file hosted on zippyshare.com
class RemoteFile:
def __init__(self, url, size, name):
self.url = url
self.size = size
self.name = name
def open(self):
r = requests.get(self.url, stream=True, timeout=10)
return r
def get_file(link):
# Match link (get subdomain)
link_match = LINK_PATTERN.match(link)
if not link_match:
raise InvalidLinkException('Invalid link')
subdomain = 'www' + link_match.group(1)
# Match info (containing hash variables and filename)
html = requests.get(link_match.group(0), timeout=10)
info = INFO_PATTERN.findall(html.text)
if len(info) == 0:
raise UnavailableFileException('File is unavailable')
info = info[0]
# ID
link_id = info[0]
# Hash
h1 = int(info[1])
h2 = int(info[2])
h3 = int(info[3])
h4 = int(info[4])
h = str(h1 % h2 + h3 % h4)
# Name
name = unquote(info[5])
# Construct url
url = 'https://' + subdomain + '.zippyshare.com/d/' + link_id + '/' + h + '/' + info[5]
# Do HEAD request to get content-length header
head = requests.head(url, timeout=10)
size = int(head.headers['content-length'])
return RemoteFile(url, size, name)
def main():
parser = argparse.ArgumentParser(description='Download files from zippyshare.com')
parser.add_argument('-d', '--dir', default='./', help='Target download directory')
parser.add_argument('links', nargs='+', help='zippyshare.com URL')
args = parser.parse_args()
# Match links
links = []
for link in args.links:
m = LINK_PATTERN.match(link)
if not m:
continue
links.append(link)
if len(links) == 0:
print('No links found!')
sys.exit(1)
# Get files
print('Fetching information...')
files = []
for link in links:
try:
f = get_file(link)
files.append(f)
except InvalidLinkException:
print("Invalid link: %s" % link)
except UnavailableFileException:
print("File unavailable: %s" % link)
except Exception as e:
print(e)
sys.exit(1)
# Download files
for file in files:
print('Downloading "%s" (%s)...' % (file.name, convert_size(file.size)))
target_file = join(args.dir, file.name)
with open(target_file, 'wb') as f:
# Copy bytes
r = file.open()
for chunk in r.iter_content(chunk_size=8192):
if chunk: f.write(chunk)
# Close
r.close()
f.flush()
f.close()
print('Download done!')
if __name__ == '__main__':
main()