raannakasturi commited on
Commit
ea7c078
·
1 Parent(s): 9194514

Add email sending functionality and refactor post creation to include wait time parameter

Browse files
Files changed (4) hide show
  1. app.py +7 -7
  2. main.py +14 -8
  3. post_blog.py +4 -4
  4. send_mail.py +69 -0
app.py CHANGED
@@ -2,9 +2,6 @@ import gradio as gr
2
  from main import post_blogpost
3
  from post_blog import test
4
 
5
- def clear_everything(title, category, summary, mindmap, citation, status, access_key):
6
- return None, None, None, None, None, None, None
7
-
8
  theme = gr.themes.Soft(
9
  primary_hue="purple",
10
  secondary_hue="cyan",
@@ -25,9 +22,12 @@ with gr.Blocks(theme=theme, title="ReXplore Backend", fill_height=True) as app:
25
  """)
26
  with gr.Row():
27
  with gr.Column():
28
- access_key = gr.Textbox(label="Access Key", placeholder="Enter the Access Key", type="password")
29
- start = gr.Button(value="Start", variant="primary")
30
- test_btn = gr.Button(value="Test")
 
 
 
31
  status = gr.Textbox(label="Data", placeholder="Enter the data", lines=7)
32
  start.click(
33
  post_blogpost,
@@ -46,6 +46,6 @@ with gr.Blocks(theme=theme, title="ReXplore Backend", fill_height=True) as app:
46
  scroll_to_output=True,
47
  show_api=True,
48
  api_name="rexplore_backend_test",
49
- show_progress="full",)
50
 
51
  app.queue(default_concurrency_limit=25).launch(show_api=True, show_error=True, debug=True)
 
2
  from main import post_blogpost
3
  from post_blog import test
4
 
 
 
 
5
  theme = gr.themes.Soft(
6
  primary_hue="purple",
7
  secondary_hue="cyan",
 
22
  """)
23
  with gr.Row():
24
  with gr.Column():
25
+ with gr.Row():
26
+ access_key = gr.Textbox(label="Access Key", placeholder="Enter the Access Key", type="password")
27
+ wait_time = gr.Number(value=5, minimum=3, maximum=15, step=1, label="Wait Time", interactive=True)
28
+ with gr.Row():
29
+ start = gr.Button(value="Start", variant="primary")
30
+ test_btn = gr.Button(value="Test", variant="secondary")
31
  status = gr.Textbox(label="Data", placeholder="Enter the data", lines=7)
32
  start.click(
33
  post_blogpost,
 
46
  scroll_to_output=True,
47
  show_api=True,
48
  api_name="rexplore_backend_test",
49
+ show_progress="full")
50
 
51
  app.queue(default_concurrency_limit=25).launch(show_api=True, show_error=True, debug=True)
main.py CHANGED
@@ -10,7 +10,10 @@ dotenv.load_dotenv()
10
  summarizer_api_key = os.getenv("SUMMARIZER_API_KEY")
11
  access_key = os.getenv("ACCESS_KEY")
12
 
13
- def paper_data(paper_data):
 
 
 
14
  data = {"status": "success"}
15
  data['data'] = {}
16
  paper_data = json.loads(paper_data)
@@ -26,7 +29,7 @@ def paper_data(paper_data):
26
  print(f"Skipping paper with ID: {paper_id} (missing details)")
27
  continue
28
  summary, mindmap = summarize_paper(pdf_url, paper_id, summarizer_api_key)
29
- post_blog(title, category, summary, mindmap, citation, access_key)
30
  data['data'][category][paper_id] = {
31
  "id": paper_id,
32
  "doi": doi,
@@ -36,16 +39,19 @@ def paper_data(paper_data):
36
  "summary": summary,
37
  "mindmap": mindmap,
38
  }
39
- output_file = "paper_data_with_summary.json"
40
  data = json.dumps(data, indent=4, ensure_ascii=False)
41
- with open(output_file, "w", encoding="utf-8") as file:
42
- json.dump(data, file, indent=4)
43
- print(f"Processed data saved to {output_file}")
44
  return data
45
 
46
- def post_blogpost(uaccess_key):
47
  if uaccess_key != access_key:
48
  return False
49
  data = fetch_paper_data_with_category(uaccess_key)
50
- pdata = paper_data(data)
 
 
 
 
 
 
 
51
  return pdata
 
10
  summarizer_api_key = os.getenv("SUMMARIZER_API_KEY")
11
  access_key = os.getenv("ACCESS_KEY")
12
 
13
+ def send_mail():
14
+ return "Mail Sent"
15
+
16
+ def paper_data(paper_data, wait_time=3):
17
  data = {"status": "success"}
18
  data['data'] = {}
19
  paper_data = json.loads(paper_data)
 
29
  print(f"Skipping paper with ID: {paper_id} (missing details)")
30
  continue
31
  summary, mindmap = summarize_paper(pdf_url, paper_id, summarizer_api_key)
32
+ post_blog(title, category, summary, mindmap, citation, access_key, wait_time)
33
  data['data'][category][paper_id] = {
34
  "id": paper_id,
35
  "doi": doi,
 
39
  "summary": summary,
40
  "mindmap": mindmap,
41
  }
 
42
  data = json.dumps(data, indent=4, ensure_ascii=False)
 
 
 
43
  return data
44
 
45
+ def post_blogpost(uaccess_key, wait_time=3):
46
  if uaccess_key != access_key:
47
  return False
48
  data = fetch_paper_data_with_category(uaccess_key)
49
+ pdata = paper_data(data, wait_time)
50
+ try:
51
+ send_mail()
52
+ print("\n-------------------------------------------------------\nMail Sent\n-------------------------------------------------------\n")
53
+ except Exception as e:
54
+ print(f"\n-------------------------------------------------------\nError sending mail: {e}\n-------------------------------------------------------\n")
55
+ finally:
56
+ print("\n-------------------------------------------------------\nProcess Completed\n-------------------------------------------------------\n")
57
  return pdata
post_blog.py CHANGED
@@ -99,15 +99,15 @@ def post_post(title, category, body, image):
99
  print(f"Error posting {title}: {e}")
100
  return False
101
 
102
- def post_blog(title, category, summary, mindmap, citation, uaccess_key):
103
  if uaccess_key != access_key:
104
  return False
105
  else:
106
  status = True
107
  post_title, post_category, post_body, post_image = create_post(title, category, summary, mindmap, citation)
108
  status = post_post(post_title, post_category, post_body, post_image)
109
- print(f"Waiting for {2*60} seconds...")
110
- time.sleep(2*60)
111
  if status:
112
  print('Post created successfully')
113
  return True
@@ -142,7 +142,7 @@ def test(access_key):
142
  mindmap = paperdata['mindmap']
143
  citation = paperdata['citation']
144
  access_key = access_key
145
- status = post_blog(title, category, summary, mindmap, citation, access_key)
146
  print(status)
147
  return status
148
 
 
99
  print(f"Error posting {title}: {e}")
100
  return False
101
 
102
+ def post_blog(title, category, summary, mindmap, citation, uaccess_key, wait_time=3):
103
  if uaccess_key != access_key:
104
  return False
105
  else:
106
  status = True
107
  post_title, post_category, post_body, post_image = create_post(title, category, summary, mindmap, citation)
108
  status = post_post(post_title, post_category, post_body, post_image)
109
+ print(f"Waiting for {wait_time*60} seconds...")
110
+ time.sleep(wait_time*60)
111
  if status:
112
  print('Post created successfully')
113
  return True
 
142
  mindmap = paperdata['mindmap']
143
  citation = paperdata['citation']
144
  access_key = access_key
145
+ status = post_blog(title, category, summary, mindmap, citation, access_key, 0)
146
  print(status)
147
  return status
148
 
send_mail.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from email import encoders
2
+ from email.mime.base import MIMEBase
3
+ import os
4
+ from datetime import datetime
5
+ from pytz import timezone
6
+ import pytz
7
+ import sib_api_v3_sdk
8
+ from sib_api_v3_sdk.rest import ApiException
9
+ from dotenv import load_dotenv
10
+
11
+ load_dotenv()
12
+ mail_api = os.getenv("MAIL_API")
13
+
14
+ def mail_body(generation_details):
15
+ body = f"""
16
+ Hello,
17
+ These are the details of the Blogs Posted at ReXplore: Science @ Fingertips.
18
+
19
+ Date & Time: {get_current_time()}
20
+
21
+
22
+ {generation_details}
23
+
24
+
25
+ Regards,
26
+ Nayan Kasturi (Raanna),
27
+ Developer & Maintainer,
28
+ ReXplore.
29
+ """
30
+ return body
31
+
32
+ def get_current_time():
33
+ fmt = "%d-%m-%Y %H:%M:%S %Z%z"
34
+ now_utc = datetime.now(timezone('UTC'))
35
+ now_asia = now_utc.astimezone(timezone('Asia/Kolkata'))
36
+ return now_asia.strftime(fmt)
37
+
38
+ def create_attachment(content, filename):
39
+ attachment = MIMEBase('application', 'octet-stream')
40
+ attachment.set_payload(content)
41
+ encoders.encode_base64(attachment)
42
+ attachment.add_header('Content-Disposition', f'attachment; filename="{filename}"')
43
+ return attachment
44
+
45
+ def send_email(generation_details):
46
+ configuration = sib_api_v3_sdk.Configuration()
47
+ configuration.api_key['api-key'] = mail_api
48
+ api_instance = sib_api_v3_sdk.TransactionalEmailsApi(sib_api_v3_sdk.ApiClient(configuration))
49
+
50
+ data = mail_body(generation_details)
51
+ data_attchment = create_attachment(data.encode('utf-8'), "data.txt")
52
+
53
+ subject = "New Blog Batch Published to ReXplore at " + get_current_time()
54
+ sender = {"name": "Project Gatekeeper", "email": "projectgatekeeper@silerudaagartha.eu.org"}
55
+ reply_to = {"name": "Project Gatekeeper", "email": "gatekeeper@raannakasturi.eu.org"}
56
+ text_content = data
57
+ attachments = [
58
+ {"content": data_attchment.get_payload(), "name": data_attchment.get_filename()},
59
+ ]
60
+ to = [{"email": "raannakasturi@proton.me"}]
61
+ send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(to=to, reply_to=reply_to, attachment=attachments, text_content=text_content, sender=sender, subject=subject)
62
+ try:
63
+ api_instance.send_transac_email(send_smtp_email)
64
+ print("Email Sent")
65
+ return True
66
+ except ApiException as e:
67
+ print("Can't send email")
68
+ print("Exception when calling SMTPApi->send_transac_email: %s\n" % e)
69
+ return False