-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
449 lines (381 loc) · 14.2 KB
/
main.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
from datetime import timedelta, datetime
import xml.etree.ElementTree as ElementTree
import pandas as pd
import re
from rich.console import Console
from rich.theme import Theme
from rich.progress import track
console = Console(theme=Theme({"logging.level.success": "bright_green"}))
def log(message, level="info"):
console.print(
f"[log.time][{datetime.now().strftime('%H:%M:%S')}][/log.time] [logging.level.{level}]{level}[/logging.level.{level}] \t{message}"
)
log("Running Purée CLI…", level="info")
passworded = (
ElementTree.parse("passworded.xml").getroot().find("REGIONS").text.split(",")
)
log("Loaded passworded regions.", level="success")
tree = ElementTree.parse("regions.xml")
log("Loaded daily dump.", level="success")
root = tree.getroot()
regions = []
practice_regions = []
region_nodes = root.findall("REGION")
log("Loaded all regions.", level="success")
update_start = int(region_nodes[0].find("LASTUPDATE").text)
update_length = int(region_nodes[-1].find("LASTUPDATE").text) - update_start
log(f"Found update length: {update_length} seconds.", level="info")
wfe_criteria = {
"[region]the brotherhood of malice[/region]": "BoM",
"[region]the black hawks[/region]": "TBH",
"[region]valle de arena[/region]": "Osiris",
"[region]lily[/region]": "Lily",
"[region]lone wolves united[/region]": "LWU",
"[region]the militia[/region]": None,
"[region]kantrias[/region]": "Kantrias",
"[region]sparkalia[/region]": "Sparkalia",
"[region]junta[/region]": "Junta",
"[region]the eternal order[/region]": "TEO",
"[region]the brothers of auralia[/region]": "TEO",
"[region]lair of the dragon[/region]": "LotD",
"region=the_brotherhood_of_malice": "BoM",
"region=the_black_hawks": "TBH",
"region=valle_de_arena": "Osiris",
"region=lily": "Lily",
"region=the_militia": None,
"region=kantrias": "Kantrias",
"region=sparkalia": "Sparkalia",
"region=junta": "Junta",
"region=the_eternal_order": "TEO",
"region=the_brothers_of_auralia": "TEO",
"lilystates.proboards.com": "Lily",
"region=lone_wolves_united": "LWU",
"region=warzone_trinidad": "WZT",
"region=lair_of_the_dragon": "LotD",
"www.forum.the-black-hawks.org": "TBH",
"forums.europeians.com/index.php?forums/office-of-naval-recruitment.59364": "ERN",
"www.nationstates.net/page=dispatch/id=1344417": "TWP",
"forum.theeastpacific.com/executive-application-thread-t16445.html": "EPSA",
"forum.theeastpacific.com/t/apply-to-join-the-eastern-pacific-sovereign-army/16275": "EPSA",
"tag run": None,
"www.nationstates.net/nation=huben/detail=factbook/id=1585974": "Kaiserreich",
"www.youtube.com/watch?v=em8GF93zQfk".lower(): "Kaiserreich",
"list=PLErS5N1ZzdNvmV-TD8VqhvfCeHcfPwq5U".lower(): "Kaiserreich",
# Kaiserreich playlist (https://www.youtube.com/playlist?list=PLErS5N1ZzdNvmV-TD8VqhvfCeHcfPwq5U)
"forum.thenorthpacific.org/topic/9145375": "NPA",
"nationstates.net/region=the_auralian_knights": "TEO",
"nationstates.net/page=dispatch/id=1881670": "Junta",
"discord.gg/z7qWg9Snzb".lower(): "Junta",
"discord.gg/8qWbXFUMAr".lower(): "Ijaka",
"osiris.valthost.com": None,
"discord.gg/k9fTy9wm".lower(): "Osiris",
"(trigger)": None,
# TEO tagging practices are often inconsistent and reliable signatures are hard to come by, but
# they have to put their name on it somewhere. These are common phrases in TEO tags that are unlikely
# to occur elsewhere - but I've put them at the end so they can easily be commented out if
# too many false positives occur.
"by the eternal order": "TEO",
"by the brothers of auralia": "TEO",
"long live the eternal order": "TEO",
"long live king nephmir": "TEO",
"www.nationstates.net/page=dispatch/id=1957508": None,
}
roman_numeral_regex = "m{0,4}(cm|cd|d?c{0,3})(xc|xl|l?x{0,3})(ix|iv|v?i{0,3})"
# https://stackoverflow.com/a/267405
offices_criteria = {
"raider unity": None,
"thorn1000": None,
"join tbh": "TBH",
"join %%lily%%": "Lily",
"lily": "Lily",
"the funny": None,
"empress wasc": None,
"ern": "ERN",
"twpirate": "TWP",
"twpirates": "TWP",
"kanye omari west": None,
"aga gang": "EPSA",
"epsa": "EPSA",
"hellfire hawk": None,
"kantrian overseer": "Kantrias",
"thaempirial army": "THA",
"moxxie cultist": None,
"blanket stealer": "BFL",
"not atlae": None,
"chimera": None,
"npa": "NPA",
"brony": None,
"praf": "TCB",
"the eo": "TEO",
"the silver dragon": "LotD",
}
ro_criteria = [
"guy_\d+",
"rc_cola_\d+",
"ijaka(\d|10)",
"taiko_no_tatsujin_\d+",
"bobberino\d+",
"\d+(rd|th|nd|st)_catgirl_division",
"switz_got_lazy_\d+",
"switz_\d+",
"sweeze_\d+",
"tls_\d+",
"flap_flap_boom_\d+",
"liliarchy_ancillary_\d+",
"lucklife_\d+",
"lurklife_\d+",
"wednesday_\d+",
"thursday_\d+",
"thorn\d+",
"wascoitan_?\d+",
"pineapple_on_pizza_is_good_\d+",
"foxes_\d+",
"oversized_operativez_\d+",
"cretanja_garrison_\d+",
f"legionnaries_{roman_numeral_regex}",
f"legionnary_{roman_numeral_regex}",
"souls\d+",
"rb\d+",
"remus_\d+",
f"remus_{roman_numeral_regex}",
"yor_\d+",
"punch_from_mark_lee_\d+",
"upc_is_not_fast_\d+",
"beans_on_toast_\d+",
"bigred\d+",
"terberrinse_\d+",
"flame_of_chaos_\d+",
"narioni_\d+",
"jyezet_fighter_\d+",
f"{roman_numeral_regex}_proleterska_vazduhoplovna_brigada",
"fedaykin_\d+",
"blanket_stealer_\d+",
"brutal_honesty_\d+",
"meep\d+",
"dragoe_\d+(rd|th|nd|st)_army_division",
"kaf-\d+",
"notas_\d+(rd|th|nd|st)_puppet",
f"ruby_{roman_numeral_regex}",
"aurum_riders_\d+(rd|th|nd|st)_treasury",
"a_crazy_person_\d+",
"bcc_\d+",
"chimoreian_imperial_legion_{roman_numeral_regex}",
]
embassies_criteria = {
"The Black Hawks": "TBH",
"The Brotherhood of Malice": "BoM",
"Valle de Arena": "Osiris",
"Red Front": "TCB",
"Plum Island": "Lily",
"Kingdom of Australia": "EoGB",
"Pasridi Confederacy": "EPSA",
"Ijaka": "Ijaka",
"Islarabia": "THA",
"The Militia": None,
"Agheasma": "Sparkalia",
"The Northern Reach": "BFL",
"JUNTA": "Junta",
"Leningrad Airfield": "TCB",
"Warzone Trinidad Corporate Office": "WZT",
"Astra": "Astoria",
"Lair of the Dragon": "LotD",
}
def find_issues(region):
if (
region.find("NAME").text
in [
"Suspicious",
"The Black Hawks",
"The Brotherhood of Malice",
"Lily",
"Osiris",
]
or "X" not in region.find("DELEGATEAUTH").text
or region.findall("./EMBASSIES/EMBASSY[.='Antifa']")
or region.find("DELEGATE").text != "0"
):
return {}
issues = []
organizations = set()
wfe = (region.find("FACTBOOK").text or "").lower()
flagged_wfe = [substring for substring in wfe_criteria if substring in wfe]
if flagged_wfe:
issues.append("WFE")
organizations.update(
{wfe_criteria[key] for key in flagged_wfe if wfe_criteria[key] is not None}
)
officers = region.find("OFFICERS").findall("OFFICER")
officer_appointers = [
(officer.find("BY").text or "").lower() for officer in officers
]
officer_offices = [
(officer.find("OFFICE").text or "").lower() for officer in officers
]
flagged_offices = [
office for office in officer_offices if office in offices_criteria
]
if flagged_offices:
issues.append("RO")
organizations.update(
{
offices_criteria[office]
for office in flagged_offices
if offices_criteria[office] is not None
}
)
if "RO" not in issues and any(
any(re.fullmatch(regex, officer_appointer) for regex in ro_criteria)
for officer_appointer in officer_appointers
):
issues.append("RO")
embassies = [
embassy.text
for embassy in region.findall(f"./EMBASSIES/EMBASSY")
if embassy.get("type") not in ["closing", "rejected", "denied"]
]
flagged_embassies = [
embassy for embassy in embassies if embassy in embassies_criteria
]
if flagged_embassies:
issues.append("Embassies")
organizations.update(
{
embassies_criteria[key]
for key in flagged_embassies
if embassies_criteria[key] is not None
}
)
native_embassies = any(
embassy.text
for embassy in region.findall(f"./EMBASSIES/EMBASSY")
if embassy.get("type") in ["closing", "rejected"]
and embassy.text not in embassies_criteria
)
# if issues found, report them
if issues:
return {
"issues": issues,
"organizations": sorted(organizations),
"native_embassies": native_embassies,
}
del issues
del organizations
del native_embassies
# otherwise, check for practice detags
practice_issues = []
practice_organizations = set()
practice_wfe_criteria = {
"Help cleanse this region of the [b][color=C7996C]thiccness[/color][/b], the [b][color=C7996C]gooeyness[/color][/b], and the [b][color=E3CBA1]drip[/color][/b]... [b][color=D6B589]drip[/color][/b]... [b][color=C7996C]drip[/color][/b]... of eggnog by detagging it.".lower(): "Eggnog"
}
practice_flagged_wfe = [
substring for substring in practice_wfe_criteria if substring in wfe
]
if practice_flagged_wfe:
practice_issues.append("WFE")
practice_organizations.update(
{
practice_wfe_criteria[key]
for key in practice_flagged_wfe
if practice_wfe_criteria[key] is not None
}
)
practice_offices_criteria = {"eggnog": "Eggnog"}
practice_flagged_offices = [
office for office in officer_offices if office in practice_offices_criteria
]
if practice_flagged_offices:
practice_issues.append("RO")
practice_organizations.update(
{
practice_offices_criteria[office]
for office in practice_flagged_offices
if practice_offices_criteria[office] is not None
}
)
practice_embassies_criteria = {"Eggnog": "Eggnog"}
practice_flagged_embassies = [
embassy for embassy in embassies if embassy in practice_embassies_criteria
]
if practice_flagged_embassies:
practice_issues.append("Embassies")
practice_organizations.update(
{
practice_embassies_criteria[key]
for key in practice_flagged_embassies
if practice_embassies_criteria[key] is not None
}
)
return {
"practice_issues": practice_issues,
"organizations": sorted(practice_organizations),
"native_embassies": False,
}
for region in track(region_nodes, description="Flagging regions…"):
if region.find("NAME").text in passworded:
continue
region_issues = find_issues(region)
if ("issues" in region_issues and region_issues["issues"]) or (
"practice_issues" in region_issues and region_issues["practice_issues"]
):
name = region.find("NAME").text
progress = (int(region.find("LASTUPDATE").text) - update_start) / update_length
minor_progress = round(progress * 3600)
major_progress = round(progress * 5400)
issues = (
region_issues["issues"]
if "issues" in region_issues
else region_issues["practice_issues"]
)
region_data = {
"Region": f"{name}",
"Issues": f"{', '.join(issues)}",
"Minor": minor_progress,
"MinorTimestamp": f"{str(timedelta(seconds=minor_progress))}",
"Major": major_progress,
"MajorTimestamp": f"{str(timedelta(seconds=major_progress))}",
"NativeEmbassies": region_issues["native_embassies"],
"Link": f"https://www.nationstates.net/region={name.lower().replace(' ', '_')}",
"Organizations": f"{', '.join(region_issues['organizations'])}"
if region_issues["organizations"]
else "Unknown",
}
for key, value in region_data.items():
if type(value) is str and value[0] in ["=", "+", "-", "@"]:
region_data[key] = f"'{value}"
if "issues" in region_issues and region_issues["issues"]:
regions.append(region_data)
elif "practice_issues" in region_issues and region_issues["practice_issues"]:
practice_regions.append(region_data)
today = (datetime.utcfromtimestamp(update_start) - timedelta(1)).strftime("%d %B %Y")
detags = pd.DataFrame.from_records(regions, index="Region")
detags.to_csv("_data/detags.csv")
detags.to_csv("data/detags.csv")
detags.to_excel("data/detags.xlsx", sheet_name=today)
detags.reset_index().to_json("data/detags.json", orient="records", indent=2)
log(f"Recorded {len(regions)} detags found.", level="success")
with open("_includes/count.html", "w") as outfile:
outfile.write(str(len(regions)))
history = pd.read_csv("_data/history.csv", index_col="Date")
if today not in history.index:
row = pd.DataFrame.from_records(
[{"Date": today, "Count": len(regions)}], index="Date"
)
history = pd.concat([history, row])
history.to_csv("_data/history.csv")
history.to_csv("data/history.csv")
history.to_excel("data/history.xlsx", sheet_name="History")
history.reset_index().to_json("data/history.json", orient="records", indent=2)
log("Recorded history.", level="info")
else:
log("No new history entries found.", level="info")
if practice_regions:
practice_detags = pd.DataFrame.from_records(practice_regions, index="Region")
practice_detags.to_csv("_data/practice_detags.csv")
practice_detags.to_csv("data/practice_detags.csv")
detags.to_excel("data/practice_detags.xlsx", sheet_name=today)
practice_detags.reset_index().to_json(
"data/practice_detags.json", orient="records", indent=2
)
log(f"Recorded {len(practice_regions)} practice_detags found.", level="success")
with open("_includes/practice_count.html", "w") as outfile:
outfile.write(str(len(practice_regions)))