diff --git a/main.py b/main.py index 0bc200d..43b6a5f 100644 --- a/main.py +++ b/main.py @@ -30,20 +30,23 @@ def load_guilds_data(): print(exc) -def send_mail(guild, subject, content): +def send_mass_mail(guild, subject, content): + send_mail(guild, guild['mailing'], subject, content) + + +def send_mail(guild, to, subject, content): msg = EmailMessage() msg['Subject'] = subject msg['From'] = guild['mail_from'] - msg['To'] = guild['mailing'] + msg['To'] = to msg.set_content(content) with smtplib.SMTP_SSL(guild['smtp_host'], guild['smtp_port'], context=ssl_context) as server: server.login(guild['smtp_username'], guild['smtp_pass']) server.send_message(msg) - def mail_message(message): - send_mail(guilds[message.guild.id], f'Nouveau message discord de {message.author.display_name}', f'{message.author.display_name}:\n{message.content}') + send_mass_mail(guilds[message.guild.id], f'[Mutubot] Nouveau message discord de {message.author.display_name}', f'{message.author.display_name}:\n{message.content}') def req(url, data): x = requests.post(url, headers={'Content-Type': 'application/x-www-form-urlencoded'}, data=data) @@ -69,6 +72,7 @@ class TokenFinder(HTMLParser): self.token = None self.public_link = None self.delete_links = [] + self.public_links = [] self.admin_url = admin_url super().__init__() @@ -82,14 +86,17 @@ class TokenFinder(HTMLParser): 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']) + elif tag == 'a' and 'href' in attrs and self.public_link and attrs['href'].startswith(self.public_link + '/vote/') : + self.public_links.append(attrs['href']) +def scrap_framavote (admin_url): + # Parse html to find data + finder = TokenFinder(admin_url) + finder.feed(requests.get(admin_url).content.decode('UTF-8')) + return finder def create_framavote (guild, names): - # 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') + finder = scrap_framavote(guild['framavote']) # Remove everything erase_framadate(guild['framavote'], finder.delete_links) @@ -99,12 +106,24 @@ def create_framavote (guild, names): req(guild['framavote'], 'choice='+name+'&confirm_add_column=') # Update control sum - finder.feed(requests.get(guild['framavote']).content.decode('UTF-8')) + finder = scrap_framavote(guild['framavote']) # Add lines for i in range(len(guild['members'])): create_line_framadate(finder.token, guild['framavote'], 'AnneONyme'+str(i), names) + # Update links + finder = scrap_framavote(guild['framavote']) + + # Send links + for mail,link in zip(guild['members'], finder.public_links): + content = f""" +Ce mail remplace tous les précédents ! +Voici votre lien de vote anonyme : +{{link}} + """ + send_mail(guild, mail, '[Mutubot] Votre lien de vote anonyme', content) + return finder.public_link def erase_framadate (admin_url, delete_links=[]): @@ -130,7 +149,7 @@ def reminder (): for i in guilds: print(f"reminding {i} : {guilds[i]['mailing']}") message = generate_reminder_message(guilds[i]) - send_mail(guilds[i], 'La mutunion c’est bientôt !', message) + send_mass_mail(guilds[i], '[Mutubot] La mutunion c’est bientôt !', message) channel = client.get_channel(guilds[i]['reminder_channel']) asyncio.run_coroutine_threadsafe(channel.send(message), client.loop)