-
Notifications
You must be signed in to change notification settings - Fork 2
/
photobackup.py
executable file
·115 lines (96 loc) · 3.5 KB
/
photobackup.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
#!/usr/bin/env python3
# Copyright (C) 2013-2015 Stéphane Péchard.
#
# This file is part of PhotoBackup.
#
# PhotoBackup is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PhotoBackup is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""PhotoBackup
Usage:
photobackup.py upload <url> <image>
photobackup.py test <url>
photobackup.py (-h | --help)
photobackup.py --version
Options:
-h --help Show this screen.
--version Show version.
"""
# stdlib
import getpass
import hashlib
import os
import sys
import urllib.parse
# pipped
from docopt import docopt
import requests
from blessings import Terminal
term = Terminal()
def make_data(filepath=None):
""" Asks the password of the server. """
password = getpass.getpass(prompt='The server password: ')
data = {'password': hashlib.sha512(password.encode('utf-8')).hexdigest()}
if filepath:
data['filesize'] = os.stat(filepath).st_size
return data
def response(status_code):
""" Prints a textual response. """
if status_code == 200:
print(term.green("Request was successful!"))
elif status_code == 400:
print(term.red("ERROR: missing file size in request!"))
elif status_code == 403:
print(term.red("ERROR: wrong password!"))
elif status_code == 404:
print(term.red("ERROR: unknown address!"))
elif status_code == 405:
print(term.red("ERROR: the server does not know this url!"))
elif status_code == 408:
print(term.red("ERROR: request took too long!"))
elif status_code == 411:
print(term.red("ERROR: file sizes do not match!"))
elif status_code == 413:
print(term.red("ERROR: file too large, modify your server config!"))
elif status_code == 500:
print(term.red("ERROR: server made a booboo!"))
else:
print(term.red("ERROR: request failed ({})!".format(status_code)))
def upload(url, image):
""" Uploads the given image to the given url. """
upfile = {'upfile': open(image, 'rb')}
try:
request = requests.post(url, files=upfile, data=make_data(image))
except requests.exceptions.MissingSchema:
sys.exit(term.red("ERROR: invalid URL: {}".format(url)))
except requests.exceptions.ConnectionError:
sys.exit(term.red("ERROR: Connection refused"))
response(request.status_code)
def test(url):
""" Tests the given url. """
test_url = urllib.parse.urljoin(url, '/test')
try:
request = requests.post(test_url, data=make_data())
except requests.exceptions.MissingSchema:
sys.exit(term.red("ERROR: invalid URL: {}".format(url)))
except requests.exceptions.ConnectionError:
sys.exit(term.red("ERROR: Connection refused"))
response(request.status_code)
def main(args):
""" Dispatches the given command. """
if args['upload']:
upload(args['<url>'], args['<image>'])
elif args['test']:
test(args['<url>'])
if __name__ == '__main__':
arguments = docopt(__doc__, version='PhotoBackup Python CLI client, v0.1.1')
main(arguments)