Spaces:
Running
Running
import re | |
import os | |
import time | |
from urllib.parse import quote | |
import base64 | |
import requests | |
import dotenv | |
import mistune | |
dotenv.load_dotenv() | |
access_key = os.getenv('ACCESS_KEY') | |
client_id = os.getenv('CLIENT_ID') | |
client_secret = os.getenv('CLIENT_SECRET') | |
refresh_token = os.getenv('REFRESH_TOKEN') | |
blog_id = os.getenv('BLOG_ID') | |
def extract_summary(text): | |
text = text.replace("#", "").strip().lower() | |
match = re.search(r"summary(.*?)highlights", text.replace("#", ""), re.DOTALL) | |
if match: | |
return match.group(1).replace("#", "").replace("\n", "").strip() | |
return None | |
from PIL import Image | |
import io | |
def verify_image(image_data): | |
try: | |
image_stream = io.BytesIO(image_data) | |
image = Image.open(image_stream) | |
image.verify() | |
image_stream.seek(0) | |
image = Image.open(image_stream) | |
return True | |
except Exception as e: | |
print(f"Error verifying image: {e}") | |
return False | |
def generate_image(title, summary): | |
default_image = "https://i.ibb.co/qdC1nSx/landscape-placeholder-e1608289113759.png" | |
extracted_summary = extract_summary(summary) | |
if extracted_summary is None: | |
extracted_summary = title | |
data = quote(f"{title} : {extracted_summary}") | |
url = f"https://image.pollinations.ai/prompt/{data}?width=1280&height=720&seed=623862700&nologo=true&model=turbo" | |
headers = { | |
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7", | |
"accept-language": "en-US,en-GB;q=0.9,en;q=0.8", | |
"cache-control": "max-age=0", | |
"priority": "u=0, i", | |
"sec-ch-ua": "\"Google Chrome\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"", | |
"sec-ch-ua-mobile": "?1", | |
"sec-ch-ua-platform": "\"Android\"", | |
"sec-fetch-dest": "document", | |
"sec-fetch-mode": "navigate", | |
"sec-fetch-site": "none", | |
"sec-fetch-user": "?1", | |
"upgrade-insecure-requests": "1", | |
} | |
response = requests.get(url, headers=headers) | |
if response.status_code != 200: | |
print(f"Failed to fetch image. Status code: {response.status_code}") | |
return default_image | |
elif not verify_image(response.content): | |
return default_image | |
else: | |
return url | |
def generate_post_html(title, summary, mindmap, citation): | |
title = title.replace("{", r"{{").replace("}", r"}}") | |
summary = summary.replace("{", r"{{").replace("}", r"}}") | |
mindmap = mindmap.replace("{", r"{{").replace("}", r"}}") | |
citation = citation.replace("{", r"{{").replace("}", r"}}") | |
image = generate_image(title, summary) | |
html_summary = mistune.html(summary) | |
post = f""" | |
<div> | |
<script src="https://cdn.jsdelivr.net/npm/markmap-autoloader@latest"></script> | |
<style> | |
.markmap {{ | |
position: relative; | |
}} | |
.markmap > svg {{ | |
width: 100%; | |
border: 2px solid #000; | |
height: 80dvh; | |
}} | |
</style> | |
<img style='display:block; width:100%;height:100%;' id='paper_image' src='{image}' /> | |
<br> | |
<p id="paper_summary" data="{summary.replace("&", "&")}">{html_summary.replace("&", "&")}</p> | |
<br> | |
<br> | |
<h2>Mindmap</h2> | |
<div class="markmap" id="paper_mindmap" data="# {title} \n {mindmap.replace("&", "&")}"> | |
<script type="text/template"> | |
{mindmap.replace("&", "&")} | |
</script> | |
</div> | |
<br> | |
<h2>Citation</h2> | |
<p id="paper_citation" data="{citation.replace("&", "&")}"> | |
{mistune.html(citation.replace("&", "&"))} | |
</p> | |
</div> | |
""" | |
return post, image | |
def create_post(title, category, summary, mindmap, citation): | |
post_title = title | |
post_category = f"{category}" | |
post_body, post_image = generate_post_html(title, summary, mindmap, citation) | |
return post_title, post_category, post_body, post_image | |
def post_post(title, category, body, image): | |
data = None | |
try: | |
data = requests.post( | |
url='https://oauth2.googleapis.com/token', | |
data={ | |
'grant_type': 'refresh_token', | |
'client_secret': client_secret, | |
'refresh_token': refresh_token, | |
'client_id': client_id, | |
}, | |
).json() | |
url = f"https://blogger.googleapis.com/v3/blogs/{blog_id}/posts" | |
headers = { | |
'Authorization': f"Bearer {data['access_token']}", | |
"content-type": "application/json" | |
} | |
post_data = { | |
"kind": "blogger#post", | |
"blog": { | |
"id": blog_id | |
}, | |
"images": [{ | |
"url": image | |
}], | |
"title": title, | |
"content": body, | |
"labels": [category, "recent"] | |
} | |
data = requests.post(url, headers=headers, json=post_data).json() | |
if data['status'] == 'LIVE': | |
print(f"The post '{title}' is {data['status']}") | |
return True | |
else: | |
print(f"Error posting {title}: {data}") | |
return False | |
except Exception as e: | |
print(f"Error posting {title}: {e}") | |
return False | |
def post_blog(title, category, summary, mindmap, citation, uaccess_key): | |
if uaccess_key != access_key: | |
return False | |
else: | |
post_title, post_category, post_body, post_image = create_post(title, category, summary, mindmap, citation) | |
status = post_post(post_title, post_category, post_body, post_image) | |
print(f"Waiting for {2*60} seconds...") | |
time.sleep(2*60) | |
if status: | |
print('Post created successfully') | |
return True | |
else: | |
print('Failed to create post') | |
return False | |
def test(access_key): | |
data = { | |
"status": "success", | |
"Astrophysics": { | |
"2412.16344": { | |
"id": "2412.16344", | |
"doi": "https://doi.org/10.48550/arXiv.2412.16344", | |
"title": "On the Interplay of Constraints from $B_s$, $D$, and $K$ Meson Mixing in $Z^\\prime$ Models with Implications for $b\to s ν\barν$ Transitions", | |
"category": "Astrophysics", | |
"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", | |
"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.", | |
"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" | |
} | |
} | |
} | |
if data['status'] != 'success': | |
print('Failed to fetch data') | |
else: | |
for category, catdata in data.items(): | |
if category != 'status': | |
for paper_id, paperdata in catdata.items(): | |
title = paperdata['title'] | |
category = paperdata['category'] | |
summary = paperdata['summary'] | |
mindmap = paperdata['mindmap'] | |
citation = paperdata['citation'] | |
access_key = access_key | |
status = post_blog(title, category, summary, mindmap, citation, access_key) | |
print(status) | |
return status | |
if __name__ == '__main__': | |
# test(access_key) | |
print(generate_image("title", "summary summaryyyyyy highlights")) |