raannakasturi commited on
Commit
4b0577b
·
1 Parent(s): bf374cc

Enhance error handling and debugging in blog posting functionality; remove unused print statements and update access key handling

Browse files
Files changed (5) hide show
  1. app.py +1 -1
  2. fetch_data.py +0 -1
  3. main.py +6 -40
  4. post_blog.py +63 -79
  5. requirements.txt +0 -1
app.py CHANGED
@@ -38,4 +38,4 @@ with gr.Blocks(theme=theme, title="ReXplore Backend", fill_height=True) as app:
38
  show_progress="full",
39
  )
40
 
41
- app.queue(default_concurrency_limit=25).launch(show_api=True)
 
38
  show_progress="full",
39
  )
40
 
41
+ app.queue(default_concurrency_limit=25).launch(show_api=True, show_error=True, debug=True)
fetch_data.py CHANGED
@@ -60,7 +60,6 @@ def fetch_paper_data_with_category(cat_ids_api_key):
60
  for category, ids in cat_ids.items():
61
  print(f"Fetching data for category: {category}")
62
  paper_data = fetch_paper_data_concurrently(ids['ids'])
63
- print(paper_data)
64
  if paper_data:
65
  data[category] = paper_data
66
  return json.dumps(data, indent=4, ensure_ascii=False)
 
60
  for category, ids in cat_ids.items():
61
  print(f"Fetching data for category: {category}")
62
  paper_data = fetch_paper_data_concurrently(ids['ids'])
 
63
  if paper_data:
64
  data[category] = paper_data
65
  return json.dumps(data, indent=4, ensure_ascii=False)
main.py CHANGED
@@ -11,9 +11,9 @@ from post_blog import post_blog
11
 
12
  # Load environment variables
13
  dotenv.load_dotenv()
14
- cat_ids_api_key = os.getenv("DATA_API_ACCESS_KEY")
15
  summarizer_api_key = os.getenv("SUMMARIZER_API_KEY")
16
  mail_api = os.getenv("MAIL_API")
 
17
 
18
  def paper_data(paper_data):
19
  data = {"status": "success"}
@@ -31,7 +31,7 @@ def paper_data(paper_data):
31
  print(f"Skipping paper with ID: {paper_id} (missing details)")
32
  continue
33
  summary, mindmap = summarize_paper(pdf_url, paper_id, summarizer_api_key)
34
- post_blog(title, category, summary, mindmap, citation, os.getenv('ACCESS_KEY'))
35
  data['data'][category][paper_id] = {
36
  "id": paper_id,
37
  "doi": doi,
@@ -41,6 +41,7 @@ def paper_data(paper_data):
41
  "summary": summary,
42
  "mindmap": mindmap,
43
  }
 
44
  output_file = "paper_data_with_summary.json"
45
  data = json.dumps(data, indent=4, ensure_ascii=False)
46
  with open(output_file, "w", encoding="utf-8") as file:
@@ -48,44 +49,9 @@ def paper_data(paper_data):
48
  print(f"Processed data saved to {output_file}")
49
  return data
50
 
51
- def create_attachment(content, filename):
52
- attachment = MIMEBase('application', 'octet-stream')
53
- attachment.set_payload(content)
54
- encoders.encode_base64(attachment)
55
- attachment.add_header('Content-Disposition', f'attachment; filename="{filename}"')
56
- return attachment
57
-
58
- def send_email(data):
59
- configuration = sib_api_v3_sdk.Configuration()
60
- configuration.api_key['api-key'] = mail_api
61
- api_instance = sib_api_v3_sdk.TransactionalEmailsApi(sib_api_v3_sdk.ApiClient(configuration))
62
-
63
- data = data
64
- data_attachment = create_attachment(data.encode('utf-8'), "data.json")
65
-
66
- subject = "ReXplore - Blogs Generated. Here are the details"
67
- sender = {"name": "Project Gatekeeper", "email": "projectgatekeeper@silerudaagartha.eu.org"}
68
- reply_to = {"name": "Project Gatekeeper", "email": "gatekeeper@raannakasturi.eu.org"}
69
- text_content = data
70
- attachments = [
71
- {"content": data_attachment.get_payload(), "name": data_attachment.get_filename()},
72
- ]
73
- to = [{"email": "raannakasturi@gmail.com"}]
74
- send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(to=to, reply_to=reply_to, attachment=attachments, text_content=text_content, sender=sender, subject=subject)
75
- try:
76
- api_response = api_instance.send_transac_email(send_smtp_email)
77
- print(api_response)
78
- print("Email Sent")
79
- return True
80
- except ApiException as e:
81
- print("Can't send email")
82
- print("Exception when calling SMTPApi->send_transac_email: %s\n" % e)
83
- return False
84
-
85
- def post_blogpost(access_key):
86
- if access_key != os.getenv('cat_ids_api_key'):
87
  return False
88
- data = fetch_paper_data_with_category(cat_ids_api_key)
89
  pdata = paper_data(data)
90
- send_email(pdata)
91
  return pdata
 
11
 
12
  # Load environment variables
13
  dotenv.load_dotenv()
 
14
  summarizer_api_key = os.getenv("SUMMARIZER_API_KEY")
15
  mail_api = os.getenv("MAIL_API")
16
+ access_key = os.getenv("ACCESS_KEY")
17
 
18
  def paper_data(paper_data):
19
  data = {"status": "success"}
 
31
  print(f"Skipping paper with ID: {paper_id} (missing details)")
32
  continue
33
  summary, mindmap = summarize_paper(pdf_url, paper_id, summarizer_api_key)
34
+ post_blog(title, category, summary, mindmap, citation, access_key)
35
  data['data'][category][paper_id] = {
36
  "id": paper_id,
37
  "doi": doi,
 
41
  "summary": summary,
42
  "mindmap": mindmap,
43
  }
44
+ break
45
  output_file = "paper_data_with_summary.json"
46
  data = json.dumps(data, indent=4, ensure_ascii=False)
47
  with open(output_file, "w", encoding="utf-8") as file:
 
49
  print(f"Processed data saved to {output_file}")
50
  return data
51
 
52
+ def post_blogpost(uaccess_key):
53
+ if uaccess_key != access_key:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  return False
55
+ data = fetch_paper_data_with_category(uaccess_key)
56
  pdata = paper_data(data)
 
57
  return pdata
post_blog.py CHANGED
@@ -6,46 +6,44 @@ import dotenv
6
  import mistune
7
 
8
  dotenv.load_dotenv()
9
- api_endpoint = os.getenv('API_ENDPOINT')
10
- password = os.getenv('BLOG_PASSWORD')
11
- username = os.getenv('BLOG_USERNAME')
 
 
12
 
13
  def generate_post_html(title, summary, mindmap, citation):
14
  html_summary = mistune.html(summary)
15
  post = f"""
16
- <!-- wp:html -->
17
- <div>
18
- <script src="https://cdn.jsdelivr.net/npm/markmap-autoloader@latest"></script>
19
- <style>
20
- .markmap {{
21
- position: relative;
22
- }}
23
- .markmap > svg {{
24
- width: 100%;
25
- border: 2px solid #000;
26
- height: 80dvh;
27
- }}
28
- </style>
29
- <p id="paper_summary" data="{summary.replace("&amp;", "&")}">
30
- {html_summary.replace("&amp;", "&")}
31
- </p>
32
- <br>
33
- <br>
34
- <h2>Mindmap</h2>
35
- <div class="markmap" data="{mindmap.replace("&amp;", "&")}">
36
- <script type="text/template">
37
- # {title.replace("&amp;", "&")}
38
- {mindmap.replace("&amp;", "&")}
39
- </script>
40
- </div>
41
- <br>
42
- <br>
43
- <h2>Citation</h2>
44
- <p id="paper_citation" data="{citation.replace("&amp;", "&")}">
45
  {mistune.html(citation.replace("&amp;", "&"))}
46
- </p>
47
- </div>
48
- <!-- /wp:html -->
49
  """
50
  return post
51
 
@@ -65,53 +63,39 @@ def create_post(title, category, summary, mindmap, citation):
65
  post_body = generate_post_html(title, summary, mindmap, sanitize_citation(citation))
66
  return post_title, post_category, post_body
67
 
68
- def create_category_if_not_exists(category_name, headers):
69
- categories_url = f"{api_endpoint}/categories"
70
- response = requests.get(categories_url, headers=headers)
71
- if response.status_code == 200:
72
- categories = response.json()
73
- for category in categories:
74
- if category['name'].lower() == category_name.lower():
75
- return category['id']
76
- create_response = requests.post(
77
- categories_url,
78
- headers=headers,
79
- json={"name": category_name}
80
- )
81
- if create_response.status_code == 201:
82
- return create_response.json()['id']
83
- else:
84
- print(f"Error creating category: {create_response.text}")
85
- return None
86
-
87
  def post_post(title, category, body):
88
- credentials = f"{username}:{password}"
89
- url = f"{api_endpoint}/posts"
90
- auth_key = base64.b64encode(credentials.encode('utf-8')).decode('utf-8')
91
- headers = {
92
- "Authorization": f"Basic {auth_key}"
93
- }
94
- category_ids = []
95
- category_id = create_category_if_not_exists(category, headers)
96
- if category_id:
97
- category_ids.append(category_id)
98
- post_data = {
99
- "title": title,
100
- "status": "publish",
101
- "categories": category_ids,
102
- "content": body
103
- }
104
- response = requests.post(url, headers=headers, json=post_data)
105
- print(response.status_code)
106
- if response.status_code == 201:
107
- print(f"Posted to blog... [{category}] {title}")
 
 
 
 
 
108
  return True
109
- else:
110
- print(f"Failed to post to blog: {response.text}")
111
  return False
112
 
113
- def post_blog(title, category, summary, mindmap, citation, access_key):
114
- if access_key != os.getenv('ACCESS_KEY'):
115
  return False
116
  try:
117
  post_title, post_category, post_body = create_post(title, category, summary, mindmap, citation)
@@ -152,5 +136,5 @@ if __name__ == '__main__':
152
  summary = paperdata['summary']
153
  mindmap = paperdata['mindmap']
154
  citation = paperdata['citation']
155
- access_key = os.getenv('ACCESS_KEY')
156
  post_blog(title, category, summary, mindmap, citation, access_key)
 
6
  import mistune
7
 
8
  dotenv.load_dotenv()
9
+ access_key = os.getenv('ACCESS_KEY')
10
+ client_id = os.getenv('CLIENT_ID')
11
+ client_secret = os.getenv('CLIENT_SECRET')
12
+ refresh_token = os.getenv('REFRESH_TOKEN')
13
+ blog_id = os.getenv('BLOG_ID')
14
 
15
  def generate_post_html(title, summary, mindmap, citation):
16
  html_summary = mistune.html(summary)
17
  post = f"""
18
+ <div>
19
+ <script src="https://cdn.jsdelivr.net/npm/markmap-autoloader@latest"></script>
20
+ <style>
21
+ .markmap {{
22
+ position: relative;
23
+ }}
24
+ .markmap > svg {{
25
+ width: 100%;
26
+ border: 2px solid #000;
27
+ height: 80dvh;
28
+ }}
29
+ </style>
30
+ <p id="paper_summary" data="{summary.replace("&amp;", "&")}">{html_summary.replace("&amp;", "&")}</p>
31
+ <br>
32
+ <br>
33
+ <h2>Mindmap</h2>
34
+ <div class="markmap" id="paper_mindmap" data="# {title} \n {mindmap.replace("&amp;", "&")}">
35
+ <script type="text/template">
36
+ # {title}
37
+ {mindmap.replace("&amp;", "&")}
38
+ </script>
39
+ </div>
40
+ <br>
41
+ <br>
42
+ <h2>Citation</h2>
43
+ <p id="paper_citation" data="{citation.replace("&amp;", "&")}">
 
 
 
44
  {mistune.html(citation.replace("&amp;", "&"))}
45
+ </p>
46
+ </div>
 
47
  """
48
  return post
49
 
 
63
  post_body = generate_post_html(title, summary, mindmap, sanitize_citation(citation))
64
  return post_title, post_category, post_body
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  def post_post(title, category, body):
67
+ try:
68
+ data = requests.post(
69
+ url='https://oauth2.googleapis.com/token',
70
+ data={
71
+ 'grant_type': 'refresh_token',
72
+ 'client_secret': client_secret,
73
+ 'refresh_token': refresh_token,
74
+ 'client_id': client_id,
75
+ },
76
+ ).json()
77
+ url = f"https://blogger.googleapis.com/v3/blogs/{blog_id}/posts"
78
+ headers = {
79
+ 'Authorization': f"Bearer {data['access_token']}",
80
+ "content-type": "application/json"
81
+ }
82
+ post_data = {
83
+ "kind": "blogger#post",
84
+ "blog": {
85
+ "id": blog_id
86
+ },
87
+ "title": title,
88
+ "content": body,
89
+ "labels": [category]
90
+ }
91
+ data = requests.post(url, headers=headers, json=post_data).json()
92
  return True
93
+ except Exception as e:
94
+ print('An error occurred:', str(e))
95
  return False
96
 
97
+ def post_blog(title, category, summary, mindmap, citation, uaccess_key):
98
+ if uaccess_key != access_key:
99
  return False
100
  try:
101
  post_title, post_category, post_body = create_post(title, category, summary, mindmap, citation)
 
136
  summary = paperdata['summary']
137
  mindmap = paperdata['mindmap']
138
  citation = paperdata['citation']
139
+ access_key = access_key
140
  post_blog(title, category, summary, mindmap, citation, access_key)
requirements.txt CHANGED
@@ -1,7 +1,6 @@
1
  gradio==5.9.1
2
  gradio-client==1.5.2
3
  python-dotenv==1.0.1
4
- sib-api-v3-sdk==7.6.0
5
  mistune==3.0.2
6
  nbconvert==7.16.4
7
  requests==2.32.3
 
1
  gradio==5.9.1
2
  gradio-client==1.5.2
3
  python-dotenv==1.0.1
 
4
  mistune==3.0.2
5
  nbconvert==7.16.4
6
  requests==2.32.3