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"""
{title.strip()}

{html_summary.replace("&", "&").strip()}

Mindmap

If MindMap doesn't load, go to the Homepage and visit blog again or Switch to Android App (Under Development).


Citation

{mistune.html(citation.replace("&", "&").strip())}
""" 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