Ver código fonte

output now sorts in descending order of delegate endos and cleaned up function

master
Haku 5 anos atrás
pai
commit
4f08a29b39
1 arquivos alterados com 17 adições e 8 exclusões
  1. +17
    -8
      founderlessnotify/founderlessnotify.py

+ 17
- 8
founderlessnotify/founderlessnotify.py Ver arquivo

@@ -22,7 +22,7 @@ def get_founderless_regions():
r = requests.get('https://www.nationstates.net/cgi-bin/api.cgi?q=regionsbytag;tags=founderless',
headers=R_HEADERS)
tree = et.fromstring(r.text)
return tree[0].text.split(',')
return [canonicalize(region) for region in tree[0].text.split(',')]


def download_region_dump():
@@ -44,7 +44,8 @@ def get_region_endos(regions):
root = tree.getroot()
for region in root:
if region[0].text in regions:
endo_dict[region[0].text] = int(region[5].text) - 1
region_name = canonicalize(region[0].text)
endo_dict[region_name] = int(region[5].text) - 1
return endo_dict


@@ -97,17 +98,25 @@ class FounderlessNotify(commands.Cog):
await channel.send(f'Beginning {update_type} check...')
new_founderless_regions = get_founderless_regions()
previous_founderless_regions = await self.config.previous_founderless()
# A region is now foundered if it was in the previous list, but isn't in the current list
# now_foundered = [region for region in previous_founderless_regions if (region not in new_founderless_regions)]
# A region is now founderless if it is in the current list, but wasn't in the previous list
now_founderless = [region for region in new_founderless_regions if (region not in previous_founderless_regions)]
# Get the region endos for both lists
# now_foundered_endos = get_region_endos(now_foundered)
now_founderless_endos = get_region_endos(now_founderless)
message_content = "The following regions are now **Founderless**:\n"
# Create an ordered set with each region's name and delegate endorsement level
out = []
for region in now_founderless:
message_content += f"https://www.nationstates.net/region={canonicalize(region)} " \
f"({now_founderless_endos[region]})\n"
try:
region_endos = now_founderless_endos[region]
except KeyError:
region_endos = -1
out.append((region_endos, region))
# Sort the output by delegate endos in descending order
out.sort()
out.reverse()
# Prep output
message_content = "The following regions are now **Founderless**:\n"
for region in out:
message_content += f"https://www.nationstates.net/region={region[1]} ({region[0]})\n"
for page in pagify(message_content):
await channel.send(page)
await self.config.previous_founderless.set(new_founderless_regions)


Carregando…
Cancelar
Salvar