-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
392 lines (319 loc) · 11.3 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
# bot.py
import asyncio
import json
import os
import discord
import requests
from discord.ext import commands, tasks
from dotenv import load_dotenv
from message_formatter import (
format_data_as_embed,
format_help_as_embed,
format_subscription_as_embed,
)
load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
URL = "http://localhost:8090"
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(
command_prefix=("e?", "?"),
description="Election Updates for Federal and Provincial",
intents=intents,
)
@bot.event
async def on_ready():
print(f"{bot.user} is connected to the following guild:\n")
election_updater.start()
@tasks.loop(minutes=2)
async def election_updater():
with open("db.json", "r") as rf:
subscription_data = json.load(rf)
subscriptions = []
for guild_id, subscription_list in subscription_data.items():
for subscription in subscription_list:
if subscription not in subscriptions:
subscriptions.append(subscription)
bulk_list = ""
for subscription in subscriptions:
state_no, district = subscription["pradesh"], subscription["district"]
bulk_list += f"pradesh-{state_no}/district-{district},"
# remove last comma
bulk_list = bulk_list[:-1]
bulk_list_url = f"{URL}/bulk?list={bulk_list}"
data = None
try:
print("requesting at ", bulk_list_url)
data = requests.get(bulk_list_url).json()
data = get_top_3_of_all(data)
except Exception as e:
print("Error during fetching bulk lists", e)
return False
cache_data = {}
try:
with open("cache.json", "r") as rf:
cache_data = json.load(rf)
except Exception as e:
print("Failed loading cache", e)
if cache_data == data:
print("Cache Matched: No updates")
return
with open("cache.json", "w") as wf:
json.dump(data, wf)
try:
await update_guilds(subscriptions, cache_data, data)
except Exception as e:
print("Failed updating guilds", e)
async def update_guilds(subscriptions, cache_data, data):
with open("db.json", "r") as rf:
subscription_data = json.load(rf)
for subscription in subscriptions:
guild_ids_with_sub = await filter_guild_id_with_sub(
subscription, subscription_data
)
await need_for_sync(guild_ids_with_sub, subscription, cache_data, data)
async def filter_guild_id_with_sub(subscription_to_filter, subscription_data):
guild_ids = []
for guild_id, subscription_list in subscription_data.items():
for subscription in subscription_list:
if subscription_to_filter == subscription:
guild_ids.append(guild_id)
return guild_ids
async def need_for_sync(guild_ids, subscription, cache_data, data):
district_name, area_no = subscription["district"], subscription["area"]
try:
cache_info = cache_data[district_name][f"constituency : {area_no}"]
except Exception as e:
print(f"No cache data for {district_name} {area_no} {e}...")
return
print(f":: processing {district_name} {area_no}")
try:
new_info = data[district_name][f"constituency : {area_no}"]
except Exception as e:
print(f"Failed obtaining data for {district_name} {area_no}... {e}")
return
if cache_info != new_info:
print(f":: cached unmatched {district_name} {area_no}")
embed = format_data_as_embed(new_info, subscription)
for guild_id in guild_ids:
guild = bot.get_guild(int(guild_id))
print("Fechted guild", guild)
channel = find_channel_to_send_msg(guild)
await channel.send(embed=embed)
else:
print(f":: cached matched {district_name} {area_no}")
@bot.event
async def on_guild_join(guild):
with open("db.json", "r") as rf:
subscription_data = json.load(rf)
subscriptions = [
{"pradesh": "7", "district": "dadeldhura", "area": "1"},
{"pradesh": "3", "district": "chitwan", "area": "2"},
]
channel = find_channel_to_send_msg(guild)
try:
await channel.send(embed=format_help_as_embed())
for embed in gen_subscription_embeds(subscriptions):
await channel.send(embed=embed)
print("New guild message sent")
await asyncio.sleep(1)
except Exception as e:
print(e, channel.guild.name)
subscription_data[str(guild.id)] = subscriptions
with open("db.json", "w") as wf:
json.dump(subscription_data, wf)
@bot.event
async def on_guild_remove(guild):
with open("db.json", "r") as rf:
subscription_data = json.load(rf)
try:
del subscription_data[str(guild.id)]
except Exception as e:
print("failed deleting guild records", e)
with open("db.json", "w") as wf:
json.dump(subscription_data, wf)
@bot.command()
async def sub(ctx, *args):
"""
Subscribe to a constituency: eg state 3 kathmandu 1
"""
parsed_cmd = parse_command(" ".join(args))
print("parsed as ", parsed_cmd)
data = validate_sub(parsed_cmd)
if not parsed_cmd or not data:
await ctx.send(
f"Invalid subscription: {' '.join(args)}\n"
"Try again or review syntax\n"
"Syntax eg: ?sub state 3 kathmandu 10"
)
return
if is_subscribed(ctx.channel.guild.id, parsed_cmd):
await ctx.send("❗ You are already subscribed.")
return
if add_subscription(ctx.channel.guild.id, parsed_cmd):
await ctx.send("✅ Successfully subscribed.")
embed = format_data_as_embed(data, parsed_cmd)
await ctx.send(embed=embed)
else:
await ctx.send("❌ Something went wrong our side. Please try again.")
@bot.command()
async def unsub(ctx, *args):
"""
Unsubscribe to a constituency: eg state 3 kathmandu 1
"""
parsed_cmd = parse_command(" ".join(args))
print("parsed as ", parsed_cmd)
if not parsed_cmd:
await ctx.send(
f"Invalid subscription: {' '.join(args)}\n"
"Try again or review syntax\n"
"Syntax eg: ?unsub state 3 kathmandu 10"
)
return
if not is_subscribed(ctx.channel.guild.id, parsed_cmd):
await ctx.send("❗ You never subscribed to this region.")
return
if remove_subscription(ctx.channel.guild.id, parsed_cmd):
await ctx.send("✅ Successfully unsubscribed.")
else:
await ctx.send("❌ Something went wrong our side. Please try again.")
@bot.command()
async def check(ctx, *args):
"""
Check a given constituency: eg state 3 kathmandu 10
"""
parsed_cmd = parse_command(" ".join(args))
print("parsed as ", parsed_cmd)
data = validate_sub(parsed_cmd)
if not parsed_cmd or not data:
await ctx.send(
f"Invalid area code: {' '.join(args)}\n"
"Try again or review syntax\n"
"Syntax eg: ?check state 3 kathmandu 10"
)
return
try:
embed = format_data_as_embed(data, parsed_cmd)
await ctx.send(embed=embed)
except Exception as e:
print("Cannot output check: ", e)
await ctx.send("❌ Something went wrong our side. Please try again.")
@bot.command()
async def listsub(ctx):
"""
List all subscriptions
"""
subscriptions = get_subscriptions(ctx.channel.guild.id)
if not subscriptions:
return
await ctx.send(embed=format_subscription_as_embed(subscriptions))
def get_subscriptions(guild_id):
guild_id = str(guild_id)
with open("db.json", "r") as rf:
subscription_data = json.load(rf)
try:
subs = subscription_data[guild_id]
return subs
except Exception as e:
print("❌ Something wrong happened, remove and reinvite the bot", e)
def add_subscription(guild_id, parsed_cmd):
guild_id = str(guild_id)
with open("db.json", "r") as rf:
subscription_data = json.load(rf)
print(":: Adding sub, sub data", subscription_data)
try:
prev_subs = subscription_data[guild_id]
prev_subs.append(parsed_cmd)
subscription_data[guild_id] = prev_subs
except Exception as e:
print("Failed adding subscription", e)
return False
with open("db.json", "w") as wf:
json.dump(subscription_data, wf)
return True
def remove_subscription(guild_id, parsed_cmd):
guild_id = str(guild_id)
with open("db.json", "r") as rf:
subscription_data = json.load(rf)
try:
prev_subs = subscription_data[guild_id]
prev_subs.remove(parsed_cmd)
subscription_data[guild_id] = prev_subs
except Exception as e:
print("Failed removing subscription", e)
return False
with open("db.json", "w") as wf:
json.dump(subscription_data, wf)
return True
def is_subscribed(guild_id, parsed_cmd):
guild_id = str(guild_id)
with open("db.json", "r") as rf:
subscription_data = json.load(rf)
try:
prev_subs = subscription_data[guild_id]
return parsed_cmd in prev_subs
except Exception as e:
print("Failed checking subscription", e)
return False
return True
def parse_command(command):
cmd_parts = command.split(" ")
cmd_parts = [i.strip().lower() for i in cmd_parts if i.strip()]
print(cmd_parts)
try:
state_no, district_name, area_no = cmd_parts[1], cmd_parts[2], cmd_parts[3]
return {"pradesh": state_no, "district": district_name, "area": area_no}
except Exception as e:
print("Error during parsing command", e)
return False
def validate_sub(parsed_command):
print("validating", parsed_command)
try:
state_no, district, area_no = (
parsed_command["pradesh"],
parsed_command["district"],
parsed_command["area"],
)
url = make_url(state_no, district)
print("requesting at ", url)
data = requests.get(url).json()
print("Got data", len(data))
data = data[f"constituency : {area_no}"]
return data
except Exception as e:
print("Error during validating sub", e)
return False
def make_url(state_no, district):
url_f = f"{URL}/area?name=pradesh-{state_no}/district-{district}"
return url_f
def gen_subscription_embeds(subscriptions):
embeds = []
for parsed_cmd in subscriptions:
data = validate_sub(parsed_cmd)
if not data:
continue
embed = format_data_as_embed(data, parsed_cmd)
embeds.append(embed)
return embeds
def find_channel_to_send_msg(guild):
channel_to_send = None
channels = []
for channel in guild.channels:
print(":: scanning channel ", channel.name)
channels.append(channel)
if channel.name.lower() == "election-updates":
return channel
elif channel.name.lower() == "general":
channel_to_send = channel
print(f":: Scanned {len(channels)} channels")
return channel_to_send or channels[0]
def get_top_3_of_all(data):
new_data = dict()
for district, constituency_dict in data.items():
new_constituency_dict = dict()
for constituency, data_list in constituency_dict.items():
new_constituency_dict[constituency] = data_list[:3]
new_data[district] = new_constituency_dict
return new_data
if __name__ == "__main__":
bot.run(TOKEN)