Parcourir la source

various tweaks

master
Haku il y a 4 ans
Parent
révision
782762d36e
1 fichiers modifiés avec 27 ajouts et 24 suppressions
  1. +27
    -24
      founderlessnotify/founderlessnotify.py

+ 27
- 24
founderlessnotify/founderlessnotify.py Voir le fichier

@@ -16,18 +16,18 @@ def canonicalize(in_str):
return in_str.lower().replace(' ', '_')


def get_founderless_regions():
async def get_founderless_regions():
"""Return the list of founderless regions."""
time.sleep(RATE_LIMIT)
await asyncio.sleep(RATE_LIMIT)
r = requests.get('https://www.nationstates.net/cgi-bin/api.cgi?q=regionsbytag;tags=founderless',
headers=R_HEADERS)
tree = et.fromstring(r.text)
return [canonicalize(region) for region in tree[0].text.split(',')]


def download_region_dump():
async def download_region_dump():
"""Download the latest region dump from the NS API."""
time.sleep(RATE_LIMIT)
await asyncio.sleep(RATE_LIMIT)
r = requests.get('https://www.nationstates.net/pages/regions.xml.gz',
headers=R_HEADERS)
with open('regions.xml.gz', 'wb') as f:
@@ -62,23 +62,23 @@ class FounderlessNotify(commands.Cog):
def cog_unload(self):
pass

@commands.command()
@checks.is_owner()
async def is_task_running(self, ctx):
"""Check if the main loop is running."""
if self.bg_loop_task:
await ctx.send('True')
else:
await ctx.send('False')
@commands.command()
@checks.is_owner()
async def start_task(self, ctx):
"""Start the main loop if it is not running."""
if self.bg_loop_task:
await ctx.send('Task is already running')
else:
self.bg_loop_task = asyncio.create_task(self.bg_loop())
# @commands.command()
# @checks.is_owner()
# async def is_task_running(self, ctx):
# """Check if the main loop is running."""
# if self.bg_loop_task:
# await ctx.send('True')
# else:
# await ctx.send('False')
# @commands.command()
# @checks.is_owner()
# async def start_task(self, ctx):
# """Start the main loop if it is not running."""
# if self.bg_loop_task:
# await ctx.send('Task is already running')
# else:
# self.bg_loop_task = asyncio.create_task(self.bg_loop())

@commands.command()
@checks.is_owner()
@@ -94,7 +94,7 @@ class FounderlessNotify(commands.Cog):
channel_id = await self.config.notify_channel()
channel = self.bot.get_channel(channel_id)
await channel.send(f'Beginning {update_type} check...')
new_founderless_regions = get_founderless_regions()
new_founderless_regions = await get_founderless_regions()
previous_founderless_regions = await self.config.previous_founderless()
# A region is now founderless if it is in the current list, but wasn't in the previous list
now_founderless = list()
@@ -110,7 +110,7 @@ class FounderlessNotify(commands.Cog):
try:
region_endos = now_founderless_endos[region]
except KeyError:
region_endos = -1
region_endos = 0
out.append((region_endos, region))
# Sort the output by delegate endos in descending order
out.sort()
@@ -118,7 +118,10 @@ class FounderlessNotify(commands.Cog):
# 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"
if region[0] == 0:
message_content += f"https://www.nationstates.net/region={region[1]} (N/A)\n"
else:
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)


Chargement…
Annuler
Enregistrer