-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmonitor.py
182 lines (151 loc) · 5.96 KB
/
monitor.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
import logging
import PIL.Image
import random
import requests.adapters
import time
import urllib.request
import json
import fire
from colors import colormap, mapcolor, codemap, mapcode, pallete
class PlacestartMonitor:
def __init__(self, username=None, password=None, debug=False):
logging.basicConfig(
format='%(asctime)s - %(levelname)7s - %(message)s',
level=logging.INFO if not debug else logging.DEBUG
)
if username == None or password == None:
with open('config.json') as user_config:
data = json.load(user_config)
username = data['username']
password = data['password']
self._username = username
self._password = password
self._board = None
self._target = None
self._diff = []
self._wait = None
def update_template(self):
template_url = "https://raw.githubusercontent.com/PlaceStart/placestart/master/template.png"
urllib.request.urlretrieve(template_url, "template.png")
return
def load_target(self):
self._target = PIL.Image.open('template.png').convert('RGB')
width, height = self._target.size
target_pixels = self._target.load()
for i in range(width):
for j in range(height):
pixel = target_pixels[i,j]
if pixel not in colormap.values():
raise RuntimeError("Target pixel not expected: {} at {}".format(pixel, (i,j)))
return
def get_board(self):
board_url = 'https://www.reddit.com/api/place/board-bitmap'
board_bytes = iter(urllib.request.urlopen(board_url).read())
board_image = PIL.Image.new('P', (1000,1000))
board_image.putpalette(pallete)
pixels = board_image.load()
for i in range(4): next(board_bytes)
for y in range(1000):
for x in range(500):
datum = next(board_bytes)
color1 = datum >> 4
color2 = datum - (color1 << 4)
pixels[x*2 , y] = color1
pixels[x*2 + 1, y] = color2
self._board = board_image.convert('RGB')
return
def get_diff(self):
width, height = self._target.size
start_region = self._board.crop(box=(0,1000-height,width,1000))
assert width, height == start_region.size
target_pixels = self._target.load()
actual_pixels = start_region.load()
for i in range(width):
for j in range(height):
if (target_pixels[i,j] != actual_pixels[i,j] and
mapcolor[target_pixels[i,j]] != 'dummy'):
self._diff.append((i,j))
logging.info("Different pixels counted {}".format(
len(self._diff)
))
return
def fix_something(self):
# randomly choose, but among the leftmost ones
width, height = self._target.size
(x, y) = coord = random.choice(self._diff[:25])
y += 1000-height
new_color = self._target.load()[coord]
logging.info("Target pixel {} will be painted {}".format(
(x,y) , mapcolor[new_color]
))
session = requests.Session()
session.mount('https://www.reddit.com', requests.adapters.HTTPAdapter(max_retries=5))
session.headers["User-Agent"] = "PlacePlacer"
auth_request = session.post(
"https://www.reddit.com/api/login/{}".format(self._username),
data={"user": self._username, "passwd": self._password, "api_type": "json"}
)
try:
session.headers['x-modhash'] = auth_request.json()["json"]["data"]["modhash"]
except:
logging.error("Authentication failed for user {}".format(self._username))
return
# Is the target still wrong?
while True:
probe_request = session.get(
"http://reddit.com/api/place/pixel.json?x={}&y={}".format(x, y),
timeout=5
)
if probe_request.status_code == 200:
data = probe_request.json()
logging.debug("Probe response: %s" % data)
break
else:
logging.warn("Probling failed with %s: %s", probe_request, probe_request.text)
time.sleep(1)
old_color = codemap[data["color"]]
if new_color == old_color:
logging.info("Target pixel was already fixed")
return
# Draw it!
draw_request = session.post(
"https://www.reddit.com/api/place/draw.json",
data={"x": str(x), "y": str(y), "color": str(mapcode[new_color])}
)
logging.debug("Draw response: %s" % draw_request.json())
if "error" not in draw_request.json():
logging.info("Placed color!")
self._wait = 300
else:
logging.info("Something went wrong, trying again later (Probably cooldown).")
self._wait = 1
if "wait_seconds" in draw_request.json():
self._wait = float(draw_request.json()["wait_seconds"])
logging.info("On cooldown, waiting {} seconds".format(self._wait))
return
def wait(self):
if self._wait:
time.sleep(self._wait)
self._wait = None
def cleanup(self):
self._board = None
self._target = None
self._diff = []
self._wait = None
def maintenance(self):
while True:
try:
self.update_template()
self.load_target()
self.get_board()
self.get_diff()
self.fix_something()
self.wait()
self.cleanup()
except KeyboardInterrupt:
break
except Exception as e:
logging.warn("Something went wrong, restarting bot Cause: %s." % e)
self.cleanup()
if __name__ == "__main__":
fire.Fire(PlacestartMonitor)