raannakasturi commited on
Commit
46f4775
·
1 Parent(s): 1fc2404

Update requirements.txt to include requests library and remove email dependency

Browse files
Files changed (3) hide show
  1. .gitignore +1 -0
  2. post_blog.py +95 -51
  3. requirements.txt +2 -2
.gitignore CHANGED
@@ -1 +1,2 @@
1
  .env
 
 
1
  .env
2
+ __*
post_blog.py CHANGED
@@ -1,24 +1,20 @@
1
  import re
2
  import os
 
 
3
  import dotenv
4
  import mistune
5
- import smtplib
6
- from email.mime.text import MIMEText
7
- from email.mime.multipart import MIMEMultipart
8
 
9
  dotenv.load_dotenv()
 
 
 
10
 
11
  def generate_post_html(title, summary, mindmap, citation):
12
  html_summary = mistune.html(summary)
13
  post = f"""
14
- <!DOCTYPE html>
15
- <html lang="en">
16
- <head>
17
- <meta charset="UTF-8">
18
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
19
- <title>{title}</title>
20
- </head>
21
- <body>
22
  <script src="https://cdn.jsdelivr.net/npm/markmap-autoloader@latest"></script>
23
  <style>
24
  .markmap {{
@@ -30,16 +26,12 @@ def generate_post_html(title, summary, mindmap, citation):
30
  height: 80dvh;
31
  }}
32
  </style>
33
- <h1 id="paper_title" data="{title.replace("&amp;", "&")}">{title.replace("&amp;", "&")}</h1>
34
- <hr>
35
- <br>
36
- <h2>SUMMARY</h2>
37
  <p id="paper_summary" data="{summary.replace("&amp;", "&")}">
38
  {html_summary.replace("&amp;", "&")}
39
  </p>
40
  <br>
41
  <br>
42
- <h2>MINDMAP</h2>
43
  <div class="markmap" data="{mindmap.replace("&amp;", "&")}">
44
  <script type="text/template">
45
  # {title.replace("&amp;", "&")}
@@ -48,13 +40,12 @@ def generate_post_html(title, summary, mindmap, citation):
48
  </div>
49
  <br>
50
  <br>
51
- <h2>CITATION</h2>
52
  <p id="paper_citation" data="{citation.replace("&amp;", "&")}">
53
  {mistune.html(citation.replace("&amp;", "&"))}
54
  </p>
55
- </body>
56
- </html>
57
- #end
58
  """
59
  return post
60
 
@@ -69,42 +60,95 @@ def sanitize_citation(citation):
69
 
70
 
71
  def create_post(title, category, summary, mindmap, citation):
72
- mail_title = f"[{category}] {title}"
73
- mail_body = generate_post_html(title, summary, mindmap, sanitize_citation(citation))
74
- return mail_title, mail_body
 
75
 
76
- def send_mail(mail_title, mail_body):
77
- sender_email = os.getenv('SENDER_EMAIL')
78
- sender_password = os.getenv('SENDER_PASSWORD')
79
- receiver_email = os.getenv('RECEIVER_EMAIL')
80
- msg = MIMEMultipart()
81
- msg['From'] = sender_email
82
- msg['To'] = receiver_email
83
- msg['Subject'] = mail_title
84
- msg.attach(MIMEText(mail_body, 'HTML'))
85
- smtp_server = os.getenv('SMTP_SERVER')
86
- smtp_port = os.getenv('SMTP_PORT')
87
- res = None
88
- try:
89
- server = smtplib.SMTP(smtp_server, smtp_port)
90
- server.starttls()
91
- server.login(sender_email, sender_password)
92
- text = msg.as_string()
93
- server.sendmail(sender_email, receiver_email, text)
94
- print('Email sent successfully')
95
- except Exception as e:
96
- print('Email not sent. An error occurred:', str(e))
97
- finally:
98
- server.quit()
99
- return res
100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  def main(title, category, summary, mindmap, citation, access_key):
102
  if access_key != os.getenv('ACCESS_KEY'):
103
  return False
104
  try:
105
- mail_title, mail_body = create_post(title, category, summary, mindmap, citation)
106
- send_mail(mail_title, mail_body)
107
- return True
 
 
 
 
 
108
  except Exception as e:
109
  print('An error occurred:', str(e))
110
- return False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import re
2
  import os
3
+ import requests
4
+ import base64
5
  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 {{
 
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;", "&")}
 
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
 
 
60
 
61
 
62
  def create_post(title, category, summary, mindmap, citation):
63
+ post_title = f"{title}"
64
+ post_category = f"{category}"
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
+ return True
108
+ else:
109
+ return False
110
+
111
  def main(title, category, summary, mindmap, citation, access_key):
112
  if access_key != os.getenv('ACCESS_KEY'):
113
  return False
114
  try:
115
+ post_title, post_category, post_body = create_post(title, category, summary, mindmap, citation)
116
+ status = post_post(post_title, post_category, post_body)
117
+ if status:
118
+ print('Post created successfully')
119
+ return True
120
+ else:
121
+ print('Failed to create post')
122
+ return False
123
  except Exception as e:
124
  print('An error occurred:', str(e))
125
+ return False
126
+
127
+ if __name__ == '__main__':
128
+ data = {
129
+ "status": "success",
130
+ "Astrophysics": {
131
+ "2412.16344": {
132
+ "id": "2412.16344",
133
+ "doi": "https://doi.org/10.48550/arXiv.2412.16344",
134
+ "title": "Focal Plane of the Arcus Probe X-Ray Spectrograph",
135
+ "category": "Astrophysics",
136
+ "citation": "Grant, C. E., Bautz, M. W., Miller, E. D., Foster, R. F., LaMarr, B., Malonis, A., Prigozhin, G., Schneider, B., Leitz, C., &amp; Falcone, A. D. (2024). Focal Plane of the Arcus Probe X-Ray Spectrograph. ArXiv. https://doi.org/10.48550/ARXIV.2412.16344",
137
+ "summary": "## Summary\nThe Arcus Probe mission concept provides high-resolution soft X-ray and UV spectroscopy to study the universe. The X-ray Spectrograph (XRS) uses two CCD focal planes to detect and record X-ray photons. Laboratory performance results meet observatory requirements.\n\n## Highlights\n- The Arcus Probe mission concept explores the formation and evolution of clusters, galaxies, and stars.\n- The XRS instrument includes four parallel optical channels and two detector focal plane arrays.\n- The CCDs are designed and manufactured by MIT Lincoln Laboratory (MIT/LL).\n- The XRS focal plane utilizes high heritage MIT/LL CCDs with proven technologies.\n- Laboratory testing confirms CCID-94 performance meets required spectral resolution and readout noise.\n- The Arcus mission includes two co-aligned instruments working simultaneously.\n- The XRS Instrument Control Unit (XICU) controls the activities of the XRS.\n\n## Key Insights\n- The Arcus Probe mission concept provides a significant improvement in sensitivity and resolution over previous missions, enabling breakthrough science in understanding the universe.\n- The XRS instrument's design, including the use of two CCD focal planes and four parallel optical channels, allows for high-resolution spectroscopy and efficient detection of X-ray photons.\n- The CCDs used in the XRS instrument are designed and manufactured by MIT Lincoln Laboratory (MIT/LL), which has a proven track record of producing high-quality CCDs for space missions.\n- The laboratory performance results of the CCID-94 device demonstrate that it meets the required spectral resolution and readout noise for the Arcus mission, indicating that the instrument is capable of achieving its scientific goals.\n- The XRS Instrument Control Unit (XICU) plays a crucial role in controlling the activities of the XRS, including gathering and storing data, and processing event recognition.\n- The Arcus mission's use of two co-aligned instruments working simultaneously allows for a wide range of scientific investigations, including the study of time-domain science and the physics of time-dependent phenomena.\n- The high heritage MIT/LL CCDs used in the XRS focal plane provide a reliable and efficient means of detecting X-ray photons, enabling the instrument to achieve its scientific goals.",
138
+ "mindmap": "## Arcus Probe Mission Concept\n- Explores formation and evolution of clusters, galaxies, stars\n- High-resolution soft X-ray and UV spectroscopy\n- Agile response capability for time-domain science\n\n## X-Ray Spectrograph (XRS) Instrument\n- Two nearly identical CCD focal planes\n- Detects and records X-ray photons from dispersed spectra\n- Zero-order of critical angle transmission gratings\n\n## XRS Focal Plane Characteristics\n- Frametransfer X-ray CCDs\n- 8-CCD array per Detector Assembly\n- FWHM < 70 eV @ 0.5 keV\n- System read noise ≤ 4 e- RMS @ 625 kpixels/sec\n\n## Detector Assembly\n- Eight CCDs in a linear array\n- Tilted to match curved focal surface\n- Gaps minimized between CCDs\n- Alignment optimized with XRS optics\n\n## Detector Electronics\n- Programmable analog clock waveforms and biases\n- Low-noise analog signal processing and digitization\n- 1 second frame time for negligible pileup\n\n## XRS Instrument Control Unit (XICU)\n- Controls XRS activities and data transfer\n- Event Recognition Processor (ERP) extracts X-ray events\n- Reduces data rate by many orders of magnitude\n\n## CCD X-Ray Performance\n- Measured readout noise 2-3 e- RMS\n- Spectral resolution meets Arcus requirements\n- FWHM < 70 eV at 0.5 keV\n\n## CCID-94 Characteristics\n- Back-illuminated frame-transfer CCDs\n- 2048 × 1024 pixel imaging array\n- 24 × 24 µm image area pixel size\n- 50 µm detector thickness\n\n## Contamination Blocking Filter (CBF)\n- Protects detectors from molecular contamination\n- 45 nm polyimide + 30 nm Al\n- Maintained above +20°C by heater control\n\n## Optical Blocking Filter (OBF)\n- Attenuates visible/IR stray light\n- 40 nm Al on-chip filter\n- Works in conjunction with CBF"
139
+ }
140
+ }
141
+ }
142
+ if data['status'] != 'success':
143
+ print('Failed to fetch data')
144
+ else:
145
+ for category, catdata in data.items():
146
+ if category != 'status':
147
+ for paper_id, paperdata in catdata.items():
148
+ title = paperdata['title']
149
+ category = paperdata['category']
150
+ summary = paperdata['summary']
151
+ mindmap = paperdata['mindmap']
152
+ citation = paperdata['citation']
153
+ access_key = os.getenv('ACCESS_KEY')
154
+ main(title, category, summary, mindmap, citation, access_key)
requirements.txt CHANGED
@@ -1,5 +1,5 @@
1
  gradio==5.9.1
2
  nbconvert==7.16.4
3
  mistune==3.0.2
4
- email==4.0.2
5
- python-dotenv==1.0.1
 
1
  gradio==5.9.1
2
  nbconvert==7.16.4
3
  mistune==3.0.2
4
+ python-dotenv==1.0.1
5
+ requests==2.26.0