Spaces:
Running
Running
import json | |
import os | |
import dotenv | |
from email.mime.text import MIMEText | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.base import MIMEBase | |
from email import encoders | |
import sib_api_v3_sdk | |
from sib_api_v3_sdk.rest import ApiException | |
from summarize_paper import summarize_paper | |
from fetch_data import fetch_paper_data_with_category | |
from post_blog import post_blog | |
# Load environment variables | |
dotenv.load_dotenv() | |
cat_ids_api_key = os.getenv("DATA_API_ACCESS_KEY") | |
summarizer_api_key = os.getenv("SUMMARIZER_API_KEY") | |
mail_api = os.getenv("MAIL_API") | |
def paper_data(paper_data): | |
data = {"status": "success"} | |
data['data'] = {} | |
paper_data = json.loads(paper_data) | |
for category, papers in paper_data.items(): | |
print(f"Processing category: {category}") | |
data['data'][category] = {} | |
for paper_id, details in papers.items(): | |
doi = details.get("doi") | |
pdf_url = details.get("pdf_url") | |
title = details.get("title") | |
citation = details.get("citation") | |
if not all([paper_id, doi, pdf_url, title, citation]): | |
print(f"Skipping paper with ID: {paper_id} (missing details)") | |
continue | |
summary, mindmap = summarize_paper(pdf_url, paper_id, summarizer_api_key) | |
post_blog(title, category, summary, mindmap, citation, os.getenv('ACCESS_KEY')) | |
data['data'][category][paper_id] = { | |
"id": paper_id, | |
"doi": doi, | |
"title": title, | |
"category": category, | |
"citation": citation, | |
"summary": summary, | |
"mindmap": mindmap, | |
} | |
output_file = "paper_data_with_summary.json" | |
data = json.dumps(data, indent=4, ensure_ascii=False) | |
with open(output_file, "w", encoding="utf-8") as file: | |
json.dump(data, file, indent=4) | |
print(f"Processed data saved to {output_file}") | |
return data | |
def create_attachment(content, filename): | |
attachment = MIMEBase('application', 'octet-stream') | |
attachment.set_payload(content) | |
encoders.encode_base64(attachment) | |
attachment.add_header('Content-Disposition', f'attachment; filename="{filename}"') | |
return attachment | |
def send_email(data): | |
configuration = sib_api_v3_sdk.Configuration() | |
configuration.api_key['api-key'] = mail_api | |
api_instance = sib_api_v3_sdk.TransactionalEmailsApi(sib_api_v3_sdk.ApiClient(configuration)) | |
data = data | |
data_attachment = create_attachment(data.encode('utf-8'), "data.json") | |
subject = "ReXplore - Blogs Generated. Here are the details" | |
sender = {"name": "Project Gatekeeper", "email": "[email protected]"} | |
reply_to = {"name": "Project Gatekeeper", "email": "[email protected]"} | |
text_content = data | |
attachments = [ | |
{"content": data_attachment.get_payload(), "name": data_attachment.get_filename()}, | |
] | |
to = [{"email": "[email protected]"}] | |
send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(to=to, reply_to=reply_to, attachment=attachments, text_content=text_content, sender=sender, subject=subject) | |
try: | |
api_response = api_instance.send_transac_email(send_smtp_email) | |
print(api_response) | |
print("Email Sent") | |
return True | |
except ApiException as e: | |
print("Can't send email") | |
print("Exception when calling SMTPApi->send_transac_email: %s\n" % e) | |
return False | |
def post_blogpost(access_key): | |
if access_key != os.getenv('cat_ids_api_key'): | |
return False | |
data = fetch_paper_data_with_category(cat_ids_api_key) | |
pdata = paper_data(data) | |
send_email(pdata) | |
return pdata |