This repository has been archived by the owner on Aug 2, 2021. It is now read-only.
forked from Sorrow446/ZS-DL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzs-dl.py
195 lines (176 loc) · 4.71 KB
/
zs-dl.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
#!/usr/bin/env python3
import os
import re
import sys
import math
import json
import time
import argparse
import traceback
try:
from urllib.parse import unquote
except ImportError:
from urllib import unquote
import requests
from tqdm import tqdm
def read_txt(abs):
with open(abs) as f:
return [u.strip() for u in f.readlines()]
def decrypt_dlc(abs):
# Thank you, dcrypt owner(s).
url = "http://dcrypt.it/decrypt/paste"
r = s.post(url, data={
'content': open(abs)
}
)
r.raise_for_status()
j = json.loads(r.text)
if not j.get('success'):
raise Exception(j)
return j['success']['links']
def parse_prefs():
parser = argparse.ArgumentParser()
parser.add_argument(
'-u', '--urls',
nargs='+', required=True,
help='URLs separated by a space or an abs path to a txt file.'
)
parser.add_argument(
'-o', '--output-path',
default='ZS-DL downloads',
help='Abs output directory.'
)
parser.add_argument(
'-ov', '--overwrite',
action='store_true',
help='Overwrite file if already exists.'
)
parser.add_argument(
'-p', '--proxy',
help='HTTPS only. <IP>:<port>.'
)
parser.add_argument(
'-s', '--save-links',
action='store_true',
help='Save links to file, instead of downloading.'
)
args = parser.parse_args()
if args.urls[0].endswith('.txt'):
args.urls = read_txt(args.urls[0])
for url in args.urls:
if url.endswith('.dlc'):
print("Processing DLC container: " + url)
try:
args.urls = args.urls + decrypt_dlc(url)
except Exception as e:
err("Failed to decrypt DLC container: " + url)
args.urls.remove(url)
time.sleep(1)
return args
def dir_setup():
if not os.path.isdir(cfg.output_path):
os.makedirs(cfg.output_path)
def err(txt):
print(txt)
traceback.print_exc()
def set_proxy():
s.proxies.update({'https': 'https://' + cfg.proxy})
def check_url(url):
regex = r'https://www(\d{1,3}).zippyshare.com/v/([a-zA-Z\d]{8})/file.html'
match = re.match(regex, url)
if match:
return match.group(1), match.group(2)
raise ValueError("Invalid URL: " + str(url))
def extract(url, server, _id):
regex = (
r'document.getElementById\(\'dlbutton\'\).href = "/d/[a-zA-Z\d]{8}/" \+ '
r'\((\d{6}) % 51245 \+ (\d{6}) % 913\) \+ "/([\w%-.]+)";'
)
for _ in range(3):
r = s.get(url)
if r.status_code != 500:
break
time.sleep(1)
r.raise_for_status()
meta = re.search(regex, r.text, re.DOTALL)
if not meta:
raise Exception('Failed to get file URL. File down or pattern changed.')
num_1 = int(meta.group(1))
num_2 = int(meta.group(2))
final_num = num_1 % 51245 + num_2 % 913
enc_fname = meta.group(3)
file_url = "https://www{}.zippyshare.com/d/{}/{}/{}".format(server, _id, final_num, enc_fname)
return file_url, unquote(enc_fname)
def get_file(ref, url):
s.headers.update({
'Range': "bytes=0-",
'Referer': ref
})
r = s.get(url, stream=True)
del s.headers['Range']
del s.headers['Referer']
r.raise_for_status()
length = int(r.headers['Content-Length'])
return r, length
def download(ref, url, fname):
print(fname)
abs = os.path.join(cfg.output_path, fname)
if os.path.isfile(abs):
if cfg.overwrite:
print("File already exists locally. Will overwrite.")
else:
print("File already exists locally.")
return
r, size = get_file(ref, url)
with open(abs, 'wb') as f:
with tqdm(total=size, unit='B',
unit_scale=True, unit_divisor=1024,
initial=0, miniters=1) as bar:
for chunk in r.iter_content(32*1024):
if chunk:
f.write(chunk)
bar.update(len(chunk))
def save(fname, file_url):
print(fname)
file = open("direct-links.txt", "a+")
file.write(file_url+"\n")
file.close()
def main(url):
server, _id = check_url(url)
file_url, fname = extract(url, server, _id)
if cfg.save_links:
save(fname, file_url)
else:
download(url, file_url, fname)
if __name__ == '__main__':
try:
if hasattr(sys, 'frozen'):
os.chdir(os.path.dirname(sys.executable))
else:
os.chdir(os.path.dirname(__file__))
except OSError:
pass
s = requests.Session()
s.headers.update({
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome"
"/75.0.3770.100 Safari/537.36"
})
print("""
_____ _____ ____ __
|__ | __|___| \| |
| __|__ |___| | | |__
|_____|_____| |____/|_____|
""")
open('direct-links.txt', 'w').close()
cfg = parse_prefs()
dir_setup()
if cfg.proxy:
set_proxy()
total = len(cfg.urls)
for num, url in enumerate(cfg.urls, 1):
print("\nURL {} of {}:".format(num, total))
try:
main(url)
except Exception as e:
err('URL failed.')