|
@@ -0,0 +1,200 @@
|
|
1
|
+import argparse
|
|
2
|
+import re
|
|
3
|
+import io
|
|
4
|
+import os
|
|
5
|
+import sys
|
|
6
|
+import time
|
|
7
|
+from datetime import datetime
|
|
8
|
+from ppadb.client import Client as AdbClient
|
|
9
|
+from PIL import Image
|
|
10
|
+import pytesseract
|
|
11
|
+
|
|
12
|
+# ----
|
|
13
|
+# Program
|
|
14
|
+# ----
|
|
15
|
+
|
|
16
|
+PROGRAM_NAME = "RoK Scraper"
|
|
17
|
+PROGRAM_VERSION = "0.3"
|
|
18
|
+PROGRAM_DESCRIPTION = "This program controls devices via ADB to take screenshots in Rise of Kingdoms."
|
|
19
|
+
|
|
20
|
+# TODO:
|
|
21
|
+# - Skip your own ranking
|
|
22
|
+# - Correct for device rotation
|
|
23
|
+# - Better logic for tap_profile()
|
|
24
|
+# - Draw box around skipped profiles
|
|
25
|
+# - Support for the last ~995-1000
|
|
26
|
+# - Coordinates to percentages
|
|
27
|
+
|
|
28
|
+# ----
|
|
29
|
+# Arguments
|
|
30
|
+# ----
|
|
31
|
+
|
|
32
|
+parser = argparse.ArgumentParser(description = PROGRAM_DESCRIPTION)
|
|
33
|
+parser.add_argument("-p", "--project", help = "project name", required = True)
|
|
34
|
+parser.add_argument("-s", "--serial", help = "device serial", required = True)
|
|
35
|
+parser.add_argument("-v", "--verbose", help = "be verbose", default = False, action = "store_true")
|
|
36
|
+parser.add_argument("-c", "--count", help = "how many to scrape", required = True, type = int)
|
|
37
|
+parser.add_argument("-n", "--start", help = "what number to start scraping at", default = 1, type = int)
|
|
38
|
+parser.add_argument("--debug", help = "debug", default = False, action = "store_true")
|
|
39
|
+arguments = parser.parse_args()
|
|
40
|
+
|
|
41
|
+# ----
|
|
42
|
+# Coordinates
|
|
43
|
+# ----
|
|
44
|
+
|
|
45
|
+ROTATE = 0 # TODO: fix this; shouldn't be needed
|
|
46
|
+
|
|
47
|
+TAP_LOCATIONS = {
|
|
48
|
+ "1": (460, 460),
|
|
49
|
+ "2": (460, 624),
|
|
50
|
+ "3": (460, 780),
|
|
51
|
+ "4": (460, 940),
|
|
52
|
+ "Middle": (460, 980),
|
|
53
|
+ "Middle + 1": (460, 1136),
|
|
54
|
+ "Middle + 2": (460, 1300)
|
|
55
|
+}
|
|
56
|
+
|
|
57
|
+# ----
|
|
58
|
+# Functions
|
|
59
|
+# ----
|
|
60
|
+
|
|
61
|
+# TODO: pass device instead of image
|
|
62
|
+def read_string_from_image(image, x, y, x2, y2):
|
|
63
|
+ return pytesseract.image_to_string(image.crop((x, y, x2, y2)), config="--psm 6").strip().replace('\n', ' ').replace('\r', '').replace('\t', ' ')
|
|
64
|
+
|
|
65
|
+def get_clipboard(device):
|
|
66
|
+ raw = device.shell('am broadcast -n "ch.pete.adbclipboard/.ReadReceiver"')
|
|
67
|
+ dataMatcher = re.compile("^.*\n.*data=\"(.*)\"$", re.DOTALL)
|
|
68
|
+ dataMatch = dataMatcher.match(raw)
|
|
69
|
+ return dataMatch.group(1)
|
|
70
|
+
|
|
71
|
+def take_screenshot(device, path):
|
|
72
|
+ screencap = device.screencap()
|
|
73
|
+ image = Image.open(io.BytesIO(screencap)).rotate(ROTATE, expand=1)
|
|
74
|
+ image.save(path + ".png", "PNG")
|
|
75
|
+ return
|
|
76
|
+
|
|
77
|
+# TODO: this can be done better
|
|
78
|
+def get_datetime_string():
|
|
79
|
+ d = datetime.now()
|
|
80
|
+ return str(d.year).zfill(4) + str(d.month).zfill(2) + str(d.day).zfill(2) + str(d.hour).zfill(2) + str(d.minute).zfill(2) + str(d.second).zfill(2)
|
|
81
|
+
|
|
82
|
+def read_single_power_ranking(device, folder):
|
|
83
|
+ datetime = get_datetime_string()
|
|
84
|
+
|
|
85
|
+ # Screenshot profile
|
|
86
|
+ # ID, Username, Power, Kill Points
|
|
87
|
+ time.sleep(1)
|
|
88
|
+ take_screenshot(device, folder + datetime + "_profile")
|
|
89
|
+
|
|
90
|
+ # Copy and save username
|
|
91
|
+ device.shell("input tap 1055 455")
|
|
92
|
+ time.sleep(1)
|
|
93
|
+ username = get_clipboard(device)
|
|
94
|
+ with open(projectFolder + "usernames.txt", 'a+', newline='', encoding='utf-8') as output_file:
|
|
95
|
+ output_file.write(datetime + "\t")
|
|
96
|
+ output_file.write(username)
|
|
97
|
+ output_file.write("\n")
|
|
98
|
+ if arguments.verbose: print(username, end = "", flush = True)
|
|
99
|
+
|
|
100
|
+ # Click "More Info"
|
|
101
|
+ # Screenshot "More Info"
|
|
102
|
+ # Power, Kill Points, Highest Power, Victory, Defeat, Dead, Scout Times, Resources Gathered, Resource Assistance, Alliance Help Times
|
|
103
|
+ device.shell("input tap 620 1070")
|
|
104
|
+ time.sleep(2)
|
|
105
|
+ take_screenshot(device, folder + datetime + "_more")
|
|
106
|
+
|
|
107
|
+ # Click "?"
|
|
108
|
+ # Screenshot Kills
|
|
109
|
+ # Kill Points, T1-5 Kills, T1-5 Points
|
|
110
|
+ device.shell("input tap 1725 250")
|
|
111
|
+ time.sleep(1)
|
|
112
|
+ take_screenshot(device, folder + datetime + "_kills")
|
|
113
|
+
|
|
114
|
+ # Exit
|
|
115
|
+ if arguments.verbose: print("") # TODO: say "done" or something here if successful
|
|
116
|
+ device.shell("input tap 2230 85")
|
|
117
|
+ time.sleep(1)
|
|
118
|
+ device.shell("input tap 2185 160")
|
|
119
|
+ return
|
|
120
|
+
|
|
121
|
+# Tap to enter a profile, skipping ones that cannot be clicked
|
|
122
|
+def tap_profile(i, folder): # TODO: fix this absolute mess of a function
|
|
123
|
+ if i == 1:
|
|
124
|
+ if is_profile(TAP_LOCATIONS["1"], folder, i): return 1
|
|
125
|
+ elif is_profile(TAP_LOCATIONS["2"], folder, i + 1): return 2
|
|
126
|
+ elif is_profile(TAP_LOCATIONS["3"], folder, i + 2): return 3
|
|
127
|
+ elif is_profile(TAP_LOCATIONS["4"], folder, i + 3): return 4
|
|
128
|
+ else: sys.exit("Profile not found.")
|
|
129
|
+ elif i == 2:
|
|
130
|
+ if is_profile(TAP_LOCATIONS["2"], folder, i): return 1
|
|
131
|
+ elif is_profile(TAP_LOCATIONS["3"], folder, i + 1): return 2
|
|
132
|
+ elif is_profile(TAP_LOCATIONS["4"], folder, i + 2): return 3
|
|
133
|
+ else: sys.exit("Profile not found.")
|
|
134
|
+ elif i == 3:
|
|
135
|
+ if is_profile(TAP_LOCATIONS["3"], folder, i): return 1
|
|
136
|
+ elif is_profile(TAP_LOCATIONS["4"], folder, i + 1): return 2
|
|
137
|
+ else: sys.exit("Profile not found.")
|
|
138
|
+ elif i == 4:
|
|
139
|
+ if is_profile(TAP_LOCATIONS["4"], folder, i): return 1
|
|
140
|
+ else: sys.exit("Profile not found.")
|
|
141
|
+ else:
|
|
142
|
+ if is_profile(TAP_LOCATIONS["Middle"], folder, i): return 1
|
|
143
|
+ elif is_profile(TAP_LOCATIONS["Middle + 1"], folder, i + 1): return 2
|
|
144
|
+ elif is_profile(TAP_LOCATIONS["Middle + 2"], folder, i + 2): return 3
|
|
145
|
+ else: sys.exit("Profile not found.")
|
|
146
|
+
|
|
147
|
+# Tap and test if you are in a profile
|
|
148
|
+def is_profile(coordinates, folder, i):
|
|
149
|
+ device.shell("input tap " + str(coordinates[0]) + " " + str(coordinates[1]))
|
|
150
|
+ time.sleep(1)
|
|
151
|
+ if read_string_from_image(Image.open(io.BytesIO(device.screencap())).rotate(ROTATE, expand=1), 1652, 1167, 1745, 1209) == "Mail":
|
|
152
|
+ return True
|
|
153
|
+ else:
|
|
154
|
+ device.shell("input tap " + str(coordinates[0]) + " " + str(coordinates[1]))
|
|
155
|
+ time.sleep(2)
|
|
156
|
+ if read_string_from_image(Image.open(io.BytesIO(device.screencap())).rotate(ROTATE, expand=1), 1652, 1167, 1745, 1209) == "Mail":
|
|
157
|
+ return True
|
|
158
|
+ if arguments.verbose: print("skipped rank " + str(i) + ". ", flush = True)
|
|
159
|
+ take_screenshot(device, folder + "skipped/" + get_datetime_string() + "_skipped_" + str(i))
|
|
160
|
+ return False
|
|
161
|
+
|
|
162
|
+# Read from the power rankings list
|
|
163
|
+def read_from_power_rankings(count, start, device, folder):
|
|
164
|
+ if arguments.verbose: print("Reading", count, "power rankings.")
|
|
165
|
+ time.sleep(1)
|
|
166
|
+
|
|
167
|
+ i = start
|
|
168
|
+ while i < (count + 1):
|
|
169
|
+ if arguments.verbose: print(str(i) + "/" + str(count) + ": ", end = "", flush = True)
|
|
170
|
+
|
|
171
|
+ # Scrape
|
|
172
|
+ i = i + tap_profile(i, folder)
|
|
173
|
+ read_single_power_ranking(device, folder)
|
|
174
|
+
|
|
175
|
+ # Rest
|
|
176
|
+ time.sleep(1)
|
|
177
|
+ if i%10 == 0: time.sleep(5)
|
|
178
|
+ if i%25 == 0: time.sleep(10)
|
|
179
|
+ return
|
|
180
|
+
|
|
181
|
+# ----
|
|
182
|
+# Main
|
|
183
|
+# ----
|
|
184
|
+
|
|
185
|
+if __name__ == '__main__':
|
|
186
|
+ # Connect to ADB device
|
|
187
|
+ client = AdbClient(host="127.0.0.1", port=5037)
|
|
188
|
+ device = client.device(arguments.serial)
|
|
189
|
+ print("Connected to", device.serial)
|
|
190
|
+
|
|
191
|
+ # Create project folder
|
|
192
|
+ projectFolder = "output/" + arguments.project + "/"
|
|
193
|
+ if not os.path.exists(projectFolder):
|
|
194
|
+ os.makedirs(projectFolder + "skipped/")
|
|
195
|
+
|
|
196
|
+ # Debug
|
|
197
|
+ if arguments.debug: take_screenshot(device, projectFolder + get_datetime_string() + "_debug")
|
|
198
|
+
|
|
199
|
+ # Scrape
|
|
200
|
+ read_from_power_rankings(arguments.count, arguments.start, device, projectFolder)
|