Spaces:
Running
Running
Refactor paper_data and summarize_paper functions for improved parameter handling and clarity
adbfd57
import os | |
import html | |
import re | |
import time | |
import requests | |
import dotenv | |
import mistune | |
from image import fetch_image | |
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') | |
imgbb_api_key = os.getenv('IMGBB_API_KEY') | |
def generate_post_html(doi, title, category, summary, mindmap, citation): | |
doi = doi.split("https://")[-1] | |
title = title.replace("{", r'{').replace("}", r'}') | |
title = re.sub(r"\\(?![a-zA-Z])", r"", title) | |
summary = summary.replace("{", r'{').replace("}", r'}') | |
mindmap = mindmap.replace("{", r'{').replace("}", r'}') | |
citation = citation.replace("{", r'{').replace("}", r'}') | |
citation = mistune.html(citation.replace("&", "&").strip()) | |
image = fetch_image(title, summary, imgbb_api_key) | |
html_summary = mistune.html(summary) | |
post = f""" | |
<div id="paper_post"> | |
<img style="display:block; width:100%; height:100%;" id="paper_image" | |
src="{image.strip()}" | |
alt="{title.strip()}"> | |
<br> | |
<br> | |
<div id="paper_summary"> | |
{html_summary.replace("&", "&").strip()} | |
</div> | |
<br> | |
<h2>Mindmap</h2> | |
<p><small><em>If MindMap doesn't load, go to the <a href="/">Homepage</a> and visit blog again or <a | |
href="/#">Switch to Android App (Under Development)</a>.</em></small></p> | |
<div class="markmap" id="paper_mindmap"> | |
<script type="text/template"> | |
{mindmap.replace("&", "&").replace(":", "=>").strip()} | |
</script> | |
</div> | |
<br> | |
<h2>Citation</h2> | |
<div id="paper_citation"> | |
{mistune.html(citation.replace("&", "&").strip())} | |
</div> | |
<script> | |
const paperImage = document.querySelector( | |
'img[style="display:block; width:100%; height:100%;"][id="paper_image"]' | |
); | |
if (paperImage) {{ | |
const tablOfContents = document.createElement("div"); | |
tablOfContents.innerHTML = "<b>{{getToc}} $title={{Table of Contents}}</b>"; | |
const brElement = paperImage.nextElementSibling; | |
if (brElement && brElement.tagName === "BR") {{ | |
brElement.insertAdjacentElement("afterend", tablOfContents); | |
}} | |
}} | |
</script> | |
<script> | |
window.markmap = {{ | |
autoLoader: {{ | |
toolbar: true, | |
}}, | |
}}; | |
</script> | |
<script src="https://cdn.jsdelivr.net/npm/markmap-autoloader@latest"></script> | |
<script> | |
window.addEventListener('load', function() {{ | |
setTimeout(function() {{ | |
const element = document.querySelector('div.mm-toolbar-item[title="Fit window size"]'); | |
console.log(element); | |
if (element) {{ | |
element.click(); | |
}} else {{ | |
console.log('Element not found'); | |
}} | |
}}, 0); | |
}}); | |
</script> | |
<style> | |
.markmap {{ | |
position: relative; | |
}} | |
.markmap > svg {{ | |
width: 100%; | |
border: 2px solid #000; | |
height: 80dvh; | |
}} | |
</style> | |
<script> | |
window.addEventListener('load', function() {{ | |
const anchor = document.querySelector('a.mm-toolbar-brand'); | |
if (anchor) {{ | |
anchor.href = '/'; | |
const img = anchor.querySelector('img'); | |
if (img) {{ | |
img.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAABHNCSVQICAgIfAhkiAAAFe1JREFUaIF9mtmvZMlx3n8RmWepU8vde+/pWTkiKVHUiIL0YgmSYcCA7P/WDwZswaQNGRRNmZIoixouMz29375brWfLJfxQt0laIp2Fesk6hRPrlxFfpPD/WYLipHAYRWTIwPjutyN/yof1t49P9aOPJpy8P7HThwu5eza1kyPNbhFymMYUKjPFqx+86s40rHd2ebOyNxetXr8cZfn00r78xRfDj65v4uWvv7r0VIoQkoVk5N8qo/9togNiZIs2JJDk8DgpnJOiKmmaM/nwzizdeziheTT17v7c57PDYjicunZRiJtmjRMjFSCo+EB2k2hxUkWbFKlo1qlu2jibjfHO4V338Usz93a0rk0WhmhhjIxg9k4eETDDfrOk/+/G3u6GaSZl2P/rUO9yWr5378g/+LTh+ONpPn1varOzealHhw3zo1k5O2qm9XwynVSlK11phffJqRNydikM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lW8jlNejDM7DxNbJWHIQ8Y6XNikfrjappy+2nZv/+b6+vPvnb/92x9cXP79q137Ov7KwIqI2d7yv272f7UUESeQ1fbDPwCm0wcs5k8eTqf3vjapjj6ZNmeP6+rkji/mh841M++ns2lR1QeFTo58qA61L2oZHUBnZVrlSbiOxbAac9+mvotxu02x24awuen7q4u2u3jeDTc/2+3e/HS9+erlbvfqV0KKc6AZS/YvL378BgV+uS2/um0hiDicK51qUXs/aY4PP753dPjp4+n0/qOyXNypqqOzZnJy0NSLg0J1KhYn5FjubVKMGd+FnHZtv163/dVyGG4uxnF9vtu9enFz89MXN6ufv4mxa3MOfUpjMku86172abtvCf7l+i23VQzARBTVwplR5jzEGGMAduO42mF2UZSLC/XVCrGNL4rW5OA0qO87rQ66aLMxpwqgEjfUKlunwypLuIl5ezGG1etuuPlq2775+fXyZ2+77vzXBShUKydCyDkks/yvJb9d/xfNailUP1oiJgAAAABJRU5ErkJggg=='; | |
img.alt = 'ReXplore'; | |
}} | |
const span = anchor.querySelector('span'); | |
if (span) {{ | |
span.textContent = 'ReXplore'; | |
}} | |
}} else {{ | |
console.log('Anchor element not found'); | |
}} | |
}}); | |
</script> | |
</div> | |
""" | |
return post, image | |
def create_post(doi, title, category, summary, mindmap, citation): | |
post_title = title | |
post_category = f"{category}" | |
try: | |
post_body, post_image = generate_post_html(doi, title, category, summary, mindmap, citation) | |
# with open('post.html', 'w') as f: | |
# f.write(post_body) | |
# exit(code=0) | |
except Exception as e: | |
print(f"Error generating post: {e}") | |
return None, None, None, None | |
return post_title, post_category, post_body, post_image | |
def post_post(title, category, body, image): | |
title = re.sub(r"\\(?![a-zA-Z])", r"", title) | |
response = 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, "ZZZZZZZZZ"] | |
} | |
response = requests.post(url, headers=headers, json=post_data).json() | |
if response['status'] != 'LIVE': | |
print(response) | |
if response['status'] == 'LIVE': | |
print(f"The post '{title}' is {response['status']}") | |
return True | |
else: | |
print(response) | |
print(f"Error posting {title}: {response}") | |
return False | |
except Exception as e: | |
print(response) | |
print(f"Error posting {title}: {e}") | |
return False | |
def post_blog(doi, title, category, summary, mindmap, citation, uaccess_key, wait_time=5): | |
if uaccess_key != access_key: | |
return False | |
else: | |
status = True | |
post_title, post_category, post_body, post_image = create_post(doi, title, category, summary, mindmap, citation) | |
if not all([post_title, post_category, post_body, post_image]): | |
print(f'Failed to create post {post_title}') | |
return False | |
post_title = post_title.replace("&", "&") | |
if "&" in post_title: | |
return False | |
status = post_post(post_title, post_category, post_body, post_image) | |
print(f"Waiting for {wait_time*60} seconds...") | |
time.sleep(wait_time*60) | |
if status: | |
print('Post created successfully') | |
return True | |
else: | |
print('Failed to create post') | |
return False | |