working a bit more

This commit is contained in:
Adrian 2024-11-07 20:29:41 +01:00
parent 8f9e7d0bd6
commit 937782e8b3

55
main.py
View File

@ -67,6 +67,7 @@ from html.parser import HTMLParser
class TokenFinder(HTMLParser): class TokenFinder(HTMLParser):
def __init__(self, admin_url): def __init__(self, admin_url):
self.token = None self.token = None
self.public_link = None
self.delete_links = [] self.delete_links = []
self.admin_url = admin_url self.admin_url = admin_url
super().__init__() super().__init__()
@ -77,30 +78,44 @@ class TokenFinder(HTMLParser):
attrs = dict(attrs_tuple) attrs = dict(attrs_tuple)
if tag == 'input' and 'type' in attrs and attrs['type'] == 'hidden' and 'name' in attrs and attrs['name'] == 'control' and 'value' in attrs : if tag == 'input' and 'type' in attrs and attrs['type'] == 'hidden' and 'name' in attrs and attrs['name'] == 'control' and 'value' in attrs :
self.token = attrs['value'] self.token = attrs['value']
elif tag == 'input' and 'id' in attrs and attrs['id'] == 'public-link' and 'value' in attrs :
self.public_link = attrs['value']
elif tag == 'a' and 'href' in attrs and attrs['href'].startswith(self.admin_url + '/action/delete_column/') : elif tag == 'a' and 'href' in attrs and attrs['href'].startswith(self.admin_url + '/action/delete_column/') :
self.delete_links.append(attrs['href']) self.delete_links.append(attrs['href'])
def create_framavote (guild, names): def create_framavote (guild, names):
erase_framadate(guild['framavote']) # Parse html to find data
for i in range(len(guild['members'])): finder = TokenFinder(guild['framavote'])
create_line_framadate(guild['framavote'], 'AnneONyme'+str(i), names) finder.feed(requests.get(guild['framavote']).content.decode('UTF-8'))
def erase_framadate (admin_url):
req(admin_url, 'remove_all_votes=')
req(admin_url, 'confirm_remove_all_votes=')
def create_line_framadate (admin_url, voter, names):
finder = TokenFinder()
finder.feed(requests.get(admin_url).content.decode('UTF-8'))
if not finder.token: if not finder.token:
print('Framavote token not found') print('Framavote token not found')
data = 'control=' + finder.token + '&name=' + voter + ''.join('&choices%5B'+str(i)+'%5D=+' for i in range(len(names))) + '&save=' # Remove everything
erase_framadate(guild['framavote'], finder.delete_links)
print(data) # Add columns
for name in names:
req(guild['framavote'], 'choice='+name+'&confirm_add_column=')
# Update control sum
finder.feed(requests.get(guild['framavote']).content.decode('UTF-8'))
# Add lines
for i in range(len(guild['members'])):
create_line_framadate(finder.token, guild['framavote'], 'AnneONyme'+str(i), names)
return finder.public_link
def erase_framadate (admin_url, delete_links=[]):
req(admin_url, 'remove_all_votes=')
req(admin_url, 'confirm_remove_all_votes=')
for link in delete_links:
requests.get(link)
def create_line_framadate (token, admin_url, voter, names):
data = 'control=' + token + '&name=' + voter + ''.join('&choices%5B'+str(i)+'%5D=+' for i in range(len(names))) + '&save='
x = req(admin_url, data) x = req(admin_url, data)
@scheduler.scheduled_job('cron', day=25) @scheduler.scheduled_job('cron', day=25)
@ -161,15 +176,15 @@ async def on_message(message):
if message.guild.id not in guilds: if message.guild.id not in guilds:
return return
if message.content.startswith('!randomvote '): if message.content.startswith('!randomvote '):
await message.reply('Êtes vous sûr·e ? Répondez « Pamplemousse agrivoltaiste » pour confirmer') await message.reply('Cela va détruire le framavote actuel. Êtes vous sûr·e ? Répondez « Pamplemousse agrivoltaiste » pour confirmer')
randomvote = message.content.split(' ')[1:] randomvote = message.content.split(' ')[1:]
return return
if message.content == 'Pamplemousse agrivoltaiste' and message.type == discord.MessageType.reply : elif message.content == 'Pamplemousse agrivoltaiste' and message.type == discord.MessageType.reply :
create_framavote(guilds[message.guild.id], ['1', '2', '3', '4', '5', '6']) public_link = create_framavote(guilds[message.guild.id], randomvote)
randomvote = None randomvote = None
await message.reply("Cest fait ! Vérifiez bien par vous même parce que je suis un peu fini à larache…") await message.reply("Cest fait ! Vérifiez bien par vous même parce que je suis un peu fini à larache… " + public_link)
return return
if message.channel.id in guilds[message.guild.id]['mailed_channels']: elif message.channel.id in guilds[message.guild.id]['mailed_channels']:
mail_message(message) mail_message(message)
# Actually starts the bot # Actually starts the bot