diff --git a/main.py b/main.py index 36ad485..0bc200d 100644 --- a/main.py +++ b/main.py @@ -67,6 +67,7 @@ from html.parser import HTMLParser class TokenFinder(HTMLParser): def __init__(self, admin_url): self.token = None + self.public_link = None self.delete_links = [] self.admin_url = admin_url super().__init__() @@ -77,30 +78,44 @@ class TokenFinder(HTMLParser): 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 : 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/') : self.delete_links.append(attrs['href']) def create_framavote (guild, names): - erase_framadate(guild['framavote']) - for i in range(len(guild['members'])): - create_line_framadate(guild['framavote'], 'AnneONyme'+str(i), names) - - -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')) + # Parse html to find data + finder = TokenFinder(guild['framavote']) + finder.feed(requests.get(guild['framavote']).content.decode('UTF-8')) if not finder.token: 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) @scheduler.scheduled_job('cron', day=25) @@ -161,15 +176,15 @@ async def on_message(message): if message.guild.id not in guilds: return 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:] return - if message.content == 'Pamplemousse agrivoltaiste' and message.type == discord.MessageType.reply : - create_framavote(guilds[message.guild.id], ['1', '2', '3', '4', '5', '6']) + elif message.content == 'Pamplemousse agrivoltaiste' and message.type == discord.MessageType.reply : + public_link = create_framavote(guilds[message.guild.id], randomvote) randomvote = None - await message.reply("C’est fait ! Vérifiez bien par vous même parce que je suis un peu fini à l’arache…") + await message.reply("C’est fait ! Vérifiez bien par vous même parce que je suis un peu fini à l’arache… " + public_link) 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) # Actually starts the bot