working ?

This commit is contained in:
Adrian 2024-11-07 20:54:08 +01:00
parent 937782e8b3
commit 6b060476ce

41
main.py
View File

@ -30,20 +30,23 @@ def load_guilds_data():
print(exc) 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 = EmailMessage()
msg['Subject'] = subject msg['Subject'] = subject
msg['From'] = guild['mail_from'] msg['From'] = guild['mail_from']
msg['To'] = guild['mailing'] msg['To'] = to
msg.set_content(content) msg.set_content(content)
with smtplib.SMTP_SSL(guild['smtp_host'], guild['smtp_port'], context=ssl_context) as server: with smtplib.SMTP_SSL(guild['smtp_host'], guild['smtp_port'], context=ssl_context) as server:
server.login(guild['smtp_username'], guild['smtp_pass']) server.login(guild['smtp_username'], guild['smtp_pass'])
server.send_message(msg) server.send_message(msg)
def mail_message(message): 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): def req(url, data):
x = requests.post(url, headers={'Content-Type': 'application/x-www-form-urlencoded'}, data=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.token = None
self.public_link = None self.public_link = None
self.delete_links = [] self.delete_links = []
self.public_links = []
self.admin_url = admin_url self.admin_url = admin_url
super().__init__() super().__init__()
@ -82,14 +86,17 @@ class TokenFinder(HTMLParser):
self.public_link = attrs['value'] 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'])
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): def create_framavote (guild, names):
# Parse html to find data finder = scrap_framavote(guild['framavote'])
finder = TokenFinder(guild['framavote'])
finder.feed(requests.get(guild['framavote']).content.decode('UTF-8'))
if not finder.token:
print('Framavote token not found')
# Remove everything # Remove everything
erase_framadate(guild['framavote'], finder.delete_links) erase_framadate(guild['framavote'], finder.delete_links)
@ -99,12 +106,24 @@ def create_framavote (guild, names):
req(guild['framavote'], 'choice='+name+'&confirm_add_column=') req(guild['framavote'], 'choice='+name+'&confirm_add_column=')
# Update control sum # Update control sum
finder.feed(requests.get(guild['framavote']).content.decode('UTF-8')) finder = scrap_framavote(guild['framavote'])
# Add lines # Add lines
for i in range(len(guild['members'])): for i in range(len(guild['members'])):
create_line_framadate(finder.token, guild['framavote'], 'AnneONyme'+str(i), names) 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 return finder.public_link
def erase_framadate (admin_url, delete_links=[]): def erase_framadate (admin_url, delete_links=[]):
@ -130,7 +149,7 @@ def reminder ():
for i in guilds: for i in guilds:
print(f"reminding {i} : {guilds[i]['mailing']}") print(f"reminding {i} : {guilds[i]['mailing']}")
message = generate_reminder_message(guilds[i]) message = generate_reminder_message(guilds[i])
send_mail(guilds[i], 'La mutunion cest bientôt !', message) send_mass_mail(guilds[i], '[Mutubot] La mutunion cest bientôt !', message)
channel = client.get_channel(guilds[i]['reminder_channel']) channel = client.get_channel(guilds[i]['reminder_channel'])
asyncio.run_coroutine_threadsafe(channel.send(message), client.loop) asyncio.run_coroutine_threadsafe(channel.send(message), client.loop)