This repository was archived by the owner on Mar 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqlcfg.py
208 lines (157 loc) · 6.69 KB
/
qlcfg.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
196
197
198
199
200
201
202
203
204
205
206
207
208
import os
import argparse
import urllib.request
import urllib.error
import json
import base64
import collections
from subprocess import call
REPOSITORY = 'https://api.github.com/repos/OverFrag/QL-Server-Settings'
ACCESS_FILE = 'baseq3/access.txt'
WORKSHOP_FILE = 'baseq3/workshop.txt'
APP_ID = 282440
def update_access(files, allow_remove):
remote = get_remote_file(ACCESS_FILE)
for file in files:
print('Processing file: %s' % file)
current = collections.OrderedDict({})
linenum = 1
f = open(os.path.expanduser(file), 'a+')
f.seek(0)
for line in f:
if not line.startswith('#') and not line.startswith(';'):
steamid, access = map(str.strip, line.split('|'))
current[steamid.strip()] = {'access': access.strip(), 'linenum': linenum}
linenum += 1
for line in remote:
if line.startswith(';ex'):
line = line[3:].split(';')
steamid, access = map(str.strip, line[0].split('|'))
name = line[1]
if steamid in current:
if allow_remove:
del current[steamid]
else:
print('\tSteamID "%s" (name: %s) is marked as removed. Consider updating access.txt to remove it [line %i]' % (steamid, name, current[steamid]['linenum']))
elif not line.startswith('#') and not line.startswith(';'):
line = line.split(';')
steamid, access = map(str.strip, line[0].split('|'))
name = line[1]
if steamid in current:
if current[steamid]['access'] != access:
if allow_remove:
print('\tSteamID "%s" (name: %s) changes access level from '
'%s to %s' % (
steamid,
name,
current[steamid]['access'],
access
))
current[steamid] = {'access': access}
else:
print('\tSteamID "%s" (name: %s) is marked to change access from %s to %s. '
'Consider updating access.txt [line %i]' % (
steamid,
name,
current[steamid]['access'],
access,
current[steamid]['linenum']
))
else:
print('\tAdding SteamID "%s" (name: %s) as %s' % (steamid, name, access))
current[steamid] = {'access': access}
write = []
for steamid in current:
write.append('%s|%s' % (steamid, current[steamid]['access']))
f.seek(0)
f.truncate()
f.write('\n'.join(write))
f.close()
def update_workshop(files):
remote = get_remote_file(WORKSHOP_FILE)
for file in files:
print('Processing file: %s' % file)
current = []
f = open(os.path.expanduser(file), 'a+')
f.seek(0)
for line in f:
if not line.startswith('#') and not line.startswith(';'):
current.append(line.strip())
for line in remote:
if not line.startswith('#') and not line.startswith(';'):
itemid, name = map(str.strip, line.split(';'))
if itemid not in current:
print('\tAdding Workshop item "%s" (name: %s)' % (itemid, name))
current.append(itemid)
f.seek(0)
f.truncate()
f.write('\n'.join(current))
f.close()
def steamcmd_call_workshop(steamcmd):
print('Calling steamcmd to update workshop items')
steamcmd = os.path.expanduser(steamcmd)
if not os.path.exists(steamcmd) or not os.access(steamcmd, os.X_OK):
print('[ERROR] steamcmd doesn\'t exist in specified location or is not executable')
exit(1)
workshop = get_remote_file(WORKSHOP_FILE)
cmd = [
steamcmd,
'+login', 'anonymous'
]
for line in workshop:
if not line.startswith('#') and not line.startswith(';'):
itemid, name = map(str.strip, line.split(';'))
cmd.append('+workshop_download_item')
cmd.append(str(APP_ID))
cmd.append(itemid.sttip())
cmd.append('+quit')
call(cmd)
def get_remote_file(file):
try:
response = urllib.request.urlopen('/'.join([REPOSITORY, 'contents', file]))
json_ = json.loads(response.read().decode('utf-8'))
data = base64.b64decode(json_['content'])
return data.decode('utf-8').splitlines()
except urllib.error.HTTPError as e:
print('[ERROR] Could not connect to settings repository (code: %s)' % e.code)
exit(1)
except urllib.error.ContentTooShortError:
print('[ERROR] There was a problem with downloading the data. Try again later')
exit(1)
except urllib.error.URLError:
print('[ERROR] There was an unknown error. PM admins at http://discord.overfrag.com/')
exit(1)
def main():
parser = argparse.ArgumentParser(description='OverFrag\'s Quake Live Settings Manager',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--access',
dest='access',
nargs='*',
help='Update defined access files',
metavar='FILE'
)
parser.add_argument('--remove',
dest='remove',
help='Allows script to remove access ids marked as removed',
action='store_true')
parser.add_argument('--workshop',
dest='workshop',
nargs='*',
help='Update defined workshop files',
metavar='FILE'
)
parser.add_argument('--steamcmd',
dest='steamcmd',
metavar='PATH',
help='Path to SteamCMD - if path is valid, it will update workshop items. This argument '
'excludes all other args')
args = parser.parse_args()
if args.steamcmd:
steamcmd_call_workshop(args.steamcmd)
exit(0)
if args.access is not None:
update_access(args.access, args.remove)
if args.workshop is not None:
update_workshop(args.workshop)
if __name__ == '__main__':
main()