Spaces:
Sleeping
Sleeping
File size: 3,346 Bytes
f07275b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
import re
import os
import dotenv
import mistune
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
dotenv.load_dotenv()
def generate_post_html(title, summary, mindmap, citation):
html_summary = mistune.html(summary)
post = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{title}</title>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/markmap-autoloader@latest"></script>
<style>
.markmap {{
position: relative;
}}
.markmap > svg {{
width: 100%;
border: 2px solid #000;
height: 80dvh;
}}
</style>
<h1 id="paper_title" data="{title.replace("&", "&")}">{title.replace("&", "&")}</h1>
<hr>
<br>
<h2>SUMMARY</h2>
<p id="paper_summary" data="{summary.replace("&", "&")}">
{html_summary.replace("&", "&")}
</p>
<br>
<br>
<h2>MINDMAP</h2>
<div class="markmap" data="{mindmap.replace("&", "&")}">
<script type="text/template">
# {title.replace("&", "&")}
{mindmap.replace("&", "&")}
</script>
</div>
<br>
<br>
<h2>CITATION</h2>
<p id="paper_citation" data="{citation.replace("&", "&")}">
{mistune.html(citation.replace("&", "&"))}
</p>
</body>
</html>
#end
"""
return post
def sanitize_citation(citation):
pattern = r"(https://doi\.org/\S+)"
sanitized_citation = re.sub(
pattern,
lambda match: f"[{match.group(1)}](https://doi.org/{match.group(1).split('/')[-1]})",
citation
)
return sanitized_citation
def create_post(title, category, summary, mindmap, citation):
mail_title = f"[{category}] {title}"
mail_body = generate_post_html(title, summary, mindmap, sanitize_citation(citation))
return mail_title, mail_body
def send_mail(mail_title, mail_body):
sender_email = os.getenv('SENDER_EMAIL')
sender_password = os.getenv('SENDER_PASSWORD')
receiver_email = os.getenv('RECEIVER_EMAIL')
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = mail_title
msg.attach(MIMEText(mail_body, 'HTML'))
smtp_server = os.getenv('SMTP_SERVER')
smtp_port = os.getenv('SMTP_PORT')
res = None
try:
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(sender_email, sender_password)
text = msg.as_string()
server.sendmail(sender_email, receiver_email, text)
print('Email sent successfully')
except Exception as e:
print('Email not sent. An error occurred:', str(e))
finally:
server.quit()
return res
def main(title, category, summary, mindmap, citation, access_key):
if access_key != os.getenv('ACCESS_KEY'):
return False
try:
mail_title, mail_body = create_post(title, category, summary, mindmap, citation)
send_mail(mail_title, mail_body)
return True
except Exception as e:
print('An error occurred:', str(e))
return False |