-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathwetransfer.py
executable file
·127 lines (101 loc) · 3.63 KB
/
wetransfer.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Download WeTransfer files
#
# VERSION :1.0
# DATE :2014-08-01
# AUTHOR :Alejandro Alonso <https://github.com/superalex>
# URL :https://github.com/superalex/py-wetransfer
# DEPENDS :pip install requests
from urlparse import urlparse, parse_qs
import requests
import sys
import json
import getopt
DOWNLOAD_URL_PARAMS_PREFIX = "downloads/"
CHUNK_SIZE = 1024
def download(file_id, recipient_id, security_hash):
url = "https://www.wetransfer.com/api/v1/transfers/{0}/download?recipient_id={1}&security_hash={2}&password=&ie=false".format(
file_id, recipient_id, security_hash)
r = requests.get(url)
download_data = json.loads(r.content)
print "Downloading {0}...".format(url)
if 'direct_link' in download_data:
direct_link_path = urlparse(download_data['direct_link']).path
direct_link_path = direct_link_path.split('/')
file_name = direct_link_path[-1]
r = requests.get(download_data['direct_link'], stream=True)
else:
file_name = download_data['fields']['filename']
r = requests.post(
download_data['formdata']['action'],
data=download_data["fields"],
stream=True
)
file_size = int(r.headers["Content-Length"])
output_file = open(file_name, 'wb')
counter = 0
for chunk in r.iter_content(chunk_size=CHUNK_SIZE):
if chunk:
output_file.write(chunk)
output_file.flush()
sys.stdout.write(
'\r{0}% {1}/{2}'.format((counter * CHUNK_SIZE) * 100/file_size,
counter * CHUNK_SIZE,
file_size))
counter += 1
sys.stdout.write('\r100% {0}/{1}\n'.format(file_size, file_size))
output_file.close()
print "Finished! {0}".format(file_name)
def extract_params(url):
"""
Extracts params from url
"""
params = url.split(DOWNLOAD_URL_PARAMS_PREFIX)[1].split('/')
[file_id, recipient_id, security_hash] = ['', '', '']
if len(params) > 2:
# The url is similar to
# https://www.wetransfer.com/downloads/XXXXXXXXXX/YYYYYYYYY/ZZZZZZZZ
[file_id, recipient_id, security_hash] = params
else:
# The url is similar to https://www.wetransfer.com/downloads/XXXXXXXXXX/ZZZZZZZZ
# In this case we have no recipient_id
[file_id, security_hash] = params
return [file_id, recipient_id, security_hash]
def extract_url_redirection(url):
"""
Follow the url redirection if necesary
"""
return requests.get(url).url
def usage():
print """
You should have a we transfer address similar to https://www.wetransfer.com/downloads/XXXXXXXXXX/YYYYYYYYY/ZZZZZZZZ
So execute:
python wetransfer.py -u https://www.wetransfer.com/downloads/XXXXXXXXXXXXXXXXXXXXXXXXX/YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY/ZZZZZ
And download it! :)
"""
sys.exit()
def main(argv):
try:
opts, args = getopt.getopt(argv, "u:h", ['url', 'help'])
url = None
for opt, arg in opts:
if opt in ('-u', '--url'):
url = arg
if opt in ('-h', '--help'):
usage()
if len(argv) == 0:
usage()
if argv[0].find('http') == 0:
url = argv[0]
if not url:
usage()
url = extract_url_redirection(url)
[file_id, recipient_id, security_hash] = extract_params(url)
download(file_id, recipient_id, security_hash)
except getopt.GetoptError:
usage()
sys.exit(2)
if __name__ == "__main__":
main(sys.argv[1:])