-
Notifications
You must be signed in to change notification settings - Fork 0
/
ofs-getsave.py
executable file
·83 lines (67 loc) · 2.73 KB
/
ofs-getsave.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
#!/usr/bin/env python3
###
# This file is part of Ottd File scripts (OFS).
#
# OFS 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, version 2.
#
# OFS 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 OFS. If not, see
# <http://www.gnu.org/licenses/>.
###
# Where all the savegames go.
savedir = './save'
# -------------------- DO NOT EDIT ANYTHING BELOW THIS LINE --------------------
import os, os.path
import sys
from urllib.request import urlopen
from urllib.error import HTTPError, URLError
def main():
ReturnValues = assignReturnValues()
# set current working directory to wherever ofs-getsave is located in case of relative paths
os.chdir(os.path.dirname(os.path.abspath(__file__)))
if len(sys.argv) <= 1:
print('Error: No URL supplied.')
sys.exit(ReturnValues.get('BADURL'))
else:
saveUrl = sys.argv[1]
if not os.path.isdir(savedir):
print('Error: Savedir "%s" is invalid.' % savedir)
sys.exit(ReturnValues.get('INVALIDSAVEDIR'))
savegame = downloadFile(saveUrl, savedir)
if isinstance(savegame, tuple):
print('Error: Encountered error %s while downloading %s. File not saved' % (savegame[0], savegame[1]))
sys.exit(ReturnValues.get('BADURL'))
elif not os.path.isfile(savegame):
print('Error: File downloaded succesfully, but file was not written. Please check your permissions on %s' % savedir)
sys.exit(ReturnValues.get('DOWNLOADFAILED'))
print('File downloaded succesfully. File saved as %s' % savegame)
sys.exit(ReturnValues.get('SUCCESS'))
def downloadFile(url, directory):
try:
savefile = os.path.join(directory, os.path.basename(url))
game = urlopen(url)
with open(savefile, 'wb') as local_file:
local_file.write(game.read())
except HTTPError as e:
return (e.code, url)
except URLError as e:
return (e.reason, url)
except IOError:
return 'couldn\'t write file'
return savefile
def assignReturnValues():
values = {
'SUCCESS' : 0x00, # Program finished successfully
'INVALIDSAVEDIR' : 0x01, # Savedir is not a valid or existing directory
'DOWNLOADFAILED' : 0x02, # Failed to write downloaded file to disk
'BADURL' : 0x03, # Download failed due to bad url (eg, 404, not an actual url)
}
return values
if __name__ == '__main__':
main()