raannakasturi commited on
Commit
aa34b1f
·
verified ·
1 Parent(s): d82d9bc

Update post_blog.py

Browse files
Files changed (1) hide show
  1. post_blog.py +162 -139
post_blog.py CHANGED
@@ -1,139 +1,162 @@
1
- import re
2
- import os
3
- import requests
4
- import dotenv
5
- import mistune
6
-
7
- dotenv.load_dotenv()
8
- access_key = os.getenv('ACCESS_KEY')
9
- client_id = os.getenv('CLIENT_ID')
10
- client_secret = os.getenv('CLIENT_SECRET')
11
- refresh_token = os.getenv('REFRESH_TOKEN')
12
- blog_id = os.getenv('BLOG_ID')
13
-
14
- def generate_post_html(title, summary, mindmap, citation):
15
- html_summary = mistune.html(summary)
16
- post = f"""
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;", "&")}">{html_summary.replace("&amp;", "&")}</p>
30
- <br>
31
- <br>
32
- <h2>Mindmap</h2>
33
- <div class="markmap" id="paper_mindmap" data="# {title} \n {mindmap.replace("&amp;", "&")}">
34
- <script type="text/template">
35
- # {title}
36
- {mindmap.replace("&amp;", "&")}
37
- </script>
38
- </div>
39
- <br>
40
- <br>
41
- <h2>Citation</h2>
42
- <p id="paper_citation" data="{citation.replace("&amp;", "&")}">
43
- {mistune.html(citation.replace("&amp;", "&"))}
44
- </p>
45
- </div>
46
- """
47
- return post
48
-
49
- def sanitize_citation(citation):
50
- pattern = r"(https://doi\.org/\S+)"
51
- sanitized_citation = re.sub(
52
- pattern,
53
- lambda match: f"[{match.group(1)}](https://doi.org/{match.group(1).split('/')[-1]})",
54
- citation
55
- )
56
- return sanitized_citation
57
-
58
-
59
- def create_post(title, category, summary, mindmap, citation):
60
- post_title = f"{title}"
61
- post_category = f"{category}"
62
- post_body = generate_post_html(title, summary, mindmap, sanitize_citation(citation))
63
- return post_title, post_category, post_body
64
-
65
- def post_post(title, category, body):
66
- try:
67
- data = requests.post(
68
- url='https://oauth2.googleapis.com/token',
69
- data={
70
- 'grant_type': 'refresh_token',
71
- 'client_secret': client_secret,
72
- 'refresh_token': refresh_token,
73
- 'client_id': client_id,
74
- },
75
- ).json()
76
- url = f"https://blogger.googleapis.com/v3/blogs/{blog_id}/posts"
77
- headers = {
78
- 'Authorization': f"Bearer {data['access_token']}",
79
- "content-type": "application/json"
80
- }
81
- post_data = {
82
- "kind": "blogger#post",
83
- "blog": {
84
- "id": blog_id
85
- },
86
- "title": title,
87
- "content": body,
88
- "labels": [category]
89
- }
90
- data = requests.post(url, headers=headers, json=post_data).json()
91
- return True
92
- except Exception as e:
93
- print('An error occurred:', str(e))
94
- return False
95
-
96
- def post_blog(title, category, summary, mindmap, citation, uaccess_key):
97
- if uaccess_key != access_key:
98
- return False
99
- try:
100
- post_title, post_category, post_body = create_post(title, category, summary, mindmap, citation)
101
- status = post_post(post_title, post_category, post_body)
102
- if status:
103
- print('Post created successfully')
104
- return True
105
- else:
106
- print('Failed to create post')
107
- return False
108
- except Exception as e:
109
- print('An error occurred:', str(e))
110
- return False
111
-
112
- if __name__ == '__main__':
113
- data = {
114
- "status": "success",
115
- "Astrophysics": {
116
- "2412.16344": {
117
- "id": "2412.16344",
118
- "doi": "https://doi.org/10.48550/arXiv.2412.16344",
119
- "title": "Focal Plane of the Arcus Probe X-Ray Spectrograph",
120
- "category": "Astrophysics",
121
- "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",
122
- "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.",
123
- "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"
124
- }
125
- }
126
- }
127
- if data['status'] != 'success':
128
- print('Failed to fetch data')
129
- else:
130
- for category, catdata in data.items():
131
- if category != 'status':
132
- for paper_id, paperdata in catdata.items():
133
- title = paperdata['title']
134
- category = paperdata['category']
135
- summary = paperdata['summary']
136
- mindmap = paperdata['mindmap']
137
- citation = paperdata['citation']
138
- access_key = access_key
139
- post_blog(title, category, summary, mindmap, citation, access_key)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import os
3
+ import base64
4
+ import requests
5
+ import dotenv
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 extract_summary(text):
16
+ match = re.search(r"Summary\n(.*?)\nHighlights", text, re.DOTALL)
17
+ if match:
18
+ return match.group(1).replace("#", "").replace("\n", "").strip()
19
+ return None
20
+
21
+ def generate_image(title, summary, category):
22
+ extracted_summary = extract_summary(summary)
23
+ print(extracted_summary)
24
+ url = f"https://image.pollinations.ai/prompt/(({category}))%20{title}%20%3A%20{extracted_summary}?width=1280&height=720&seed=623862700&nologo=true&model=turbo"
25
+ data = requests.get(url).content
26
+ with open("image.png", "wb") as file:
27
+ file.write(data)
28
+ encoded_image = base64.b64encode(data)
29
+ image = f"data:image/png;base64,{encoded_image.decode("utf-8")}"
30
+ return image
31
+
32
+ def generate_post_html(title, summary, mindmap, category, citation):
33
+ image = generate_image(title, summary, category)
34
+ html_summary = mistune.html(summary)
35
+ post = f"""
36
+ <div>
37
+ <script src="https://cdn.jsdelivr.net/npm/markmap-autoloader@latest"></script>
38
+ <style>
39
+ .markmap {{
40
+ position: relative;
41
+ }}
42
+ .markmap > svg {{
43
+ width: 100%;
44
+ border: 2px solid #000;
45
+ height: 80dvh;
46
+ }}
47
+ </style>
48
+ <img style='display:block; width:100%;height:100%;' id='paper_image' src='{image}' />
49
+ <br>
50
+ <br>
51
+ <p id="paper_summary" data="{summary.replace("&amp;", "&")}">{html_summary.replace("&amp;", "&")}</p>
52
+ <br>
53
+ <br>
54
+ <h2>Mindmap</h2>
55
+ <div class="markmap" id="paper_mindmap" data="# {title} \n {mindmap.replace("&amp;", "&")}">
56
+ <script type="text/template">
57
+ # {title}
58
+ {mindmap.replace("&amp;", "&")}
59
+ </script>
60
+ </div>
61
+ <br>
62
+ <br>
63
+ <h2>Citation</h2>
64
+ <p id="paper_citation" data="{citation.replace("&amp;", "&")}">
65
+ {mistune.html(citation.replace("&amp;", "&"))}
66
+ </p>
67
+ </div>
68
+ """
69
+ return post
70
+
71
+ def sanitize_citation(citation):
72
+ pattern = r"(https://doi\.org/\S+)"
73
+ sanitized_citation = re.sub(
74
+ pattern,
75
+ lambda match: f"[{match.group(1)}](https://doi.org/{match.group(1).split('/')[-1]})",
76
+ citation
77
+ )
78
+ return sanitized_citation
79
+
80
+
81
+ def create_post(title, category, summary, mindmap, citation):
82
+ post_title = f"{title}"
83
+ post_category = f"{category}"
84
+ post_body = generate_post_html(title, summary, mindmap, category, sanitize_citation(citation))
85
+ return post_title, post_category, post_body
86
+
87
+ def post_post(title, category, body):
88
+ try:
89
+ data = requests.post(
90
+ url='https://oauth2.googleapis.com/token',
91
+ data={
92
+ 'grant_type': 'refresh_token',
93
+ 'client_secret': client_secret,
94
+ 'refresh_token': refresh_token,
95
+ 'client_id': client_id,
96
+ },
97
+ ).json()
98
+ url = f"https://blogger.googleapis.com/v3/blogs/{blog_id}/posts"
99
+ headers = {
100
+ 'Authorization': f"Bearer {data['access_token']}",
101
+ "content-type": "application/json"
102
+ }
103
+ post_data = {
104
+ "kind": "blogger#post",
105
+ "blog": {
106
+ "id": blog_id
107
+ },
108
+ "title": title,
109
+ "content": body,
110
+ "labels": [category, "recent"]
111
+ }
112
+ data = requests.post(url, headers=headers, json=post_data).json()
113
+ print(data)
114
+ return True
115
+ except Exception as e:
116
+ print('An error occurred:', str(e))
117
+ return False
118
+
119
+ def post_blog(title, category, summary, mindmap, citation, uaccess_key):
120
+ if uaccess_key != access_key:
121
+ return False
122
+ try:
123
+ post_title, post_category, post_body = create_post(title, category, summary, mindmap, citation)
124
+ status = post_post(post_title, post_category, post_body)
125
+ if status:
126
+ print('Post created successfully')
127
+ return True
128
+ else:
129
+ print('Failed to create post')
130
+ return False
131
+ except Exception as e:
132
+ print('An error occurred:', str(e))
133
+ return False
134
+
135
+ if __name__ == '__main__':
136
+ data = {
137
+ "status": "success",
138
+ "Astrophysics": {
139
+ "2412.16344": {
140
+ "id": "2412.16344",
141
+ "doi": "https://doi.org/10.48550/arXiv.2412.16344",
142
+ "title": "Focal Plane of the Arcus Probe X-Ray Spectrograph",
143
+ "category": "Astrophysics",
144
+ "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",
145
+ "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.",
146
+ "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"
147
+ }
148
+ }
149
+ }
150
+ if data['status'] != 'success':
151
+ print('Failed to fetch data')
152
+ else:
153
+ for category, catdata in data.items():
154
+ if category != 'status':
155
+ for paper_id, paperdata in catdata.items():
156
+ title = paperdata['title']
157
+ category = paperdata['category']
158
+ summary = paperdata['summary']
159
+ mindmap = paperdata['mindmap']
160
+ citation = paperdata['citation']
161
+ access_key = access_key
162
+ post_blog(title, category, summary, mindmap, citation, access_key)