-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscreepscli.py
115 lines (87 loc) · 2.73 KB
/
screepscli.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
"""Screeps CLI
Usage:
screeps.py log [--ptr]
screeps.py me [--ptr]
screeps.py download [--dest=<directory>] [--ptr] [--debug]
"""
import json
import logging
import os
import sys
import requests
from docopt import docopt
from screeps.screeps import Connection
from pprint import pprint
import threading
def save(name, data, dest):
with open('{}/{}.js'.format(dest, name), 'w') as f:
f.write(data)
def me(email, password, arguments):
connection = Connection(email, password, arguments['--ptr'])
pprint(connection.get_me())
def download(email, password, arguments):
print(arguments)
dest = arguments.get('--dest')
if not dest:
dest = 'dist'
if not os.path.isdir(dest):
os.mkdir(dest)
url = 'https://screeps.com/api/user/code'
if arguments.get('--ptr'):
url = 'https://screeps.com/ptr/api/user/code?branch=$activeWorld'
auth = (email, password)
response = requests.get(url, auth=auth)
response.raise_for_status()
data = response.json()
if 'error' in data:
sys.exit(data['error'])
for module in data['modules']:
if data['modules'][module]:
if arguments.get('--debug'):
print('save {}'.format(module))
save(module, data['modules'][module], dest)
else:
try:
os.remove('{}/{}.js'.format(dest, module))
except OSError:
pass
def sysout(message):
if message.startswith('time'):
return
data = json.loads(message)
if 'messages' in data[1]:
if 'log' in data[1]['messages']:
for line in data[1]['messages']['log']:
print(line)
return
print('on_message', message)
class ReadStdin(threading.Thread):
def __init__(self, connection):
threading.Thread.__init__(self)
self.connection = connection
self.running = True
def run(self):
while self.running:
line = input()
self.connection.console(line)
def main():
logging.basicConfig()
arguments = docopt(__doc__)
email = os.environ.get('email')
password = os.environ.get('password')
if not email or not password:
sys.exit('Please set email and password as environment variables.')
if arguments.get('log'):
connection = Connection(email, password, arguments['--ptr'])
read_stdin = ReadStdin(connection)
try:
read_stdin.start()
connection.startWebSocket(sysout)
except KeyboardInterrupt:
read_stdin.running = False
if arguments.get('download'):
download(email, password, arguments)
if arguments.get('me'):
me(email, password, arguments)
if __name__ == '__main__':
main()