Spaces:
Running
Running
Update post_blog.py
Browse files- post_blog.py +162 -139
post_blog.py
CHANGED
@@ -1,139 +1,162 @@
|
|
1 |
-
import re
|
2 |
-
import os
|
3 |
-
import
|
4 |
-
import
|
5 |
-
import
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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("&", "&")}">{html_summary.replace("&", "&")}</p>
|
52 |
+
<br>
|
53 |
+
<br>
|
54 |
+
<h2>Mindmap</h2>
|
55 |
+
<div class="markmap" id="paper_mindmap" data="# {title} \n {mindmap.replace("&", "&")}">
|
56 |
+
<script type="text/template">
|
57 |
+
# {title}
|
58 |
+
{mindmap.replace("&", "&")}
|
59 |
+
</script>
|
60 |
+
</div>
|
61 |
+
<br>
|
62 |
+
<br>
|
63 |
+
<h2>Citation</h2>
|
64 |
+
<p id="paper_citation" data="{citation.replace("&", "&")}">
|
65 |
+
{mistune.html(citation.replace("&", "&"))}
|
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., & 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)
|