import os 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') def generate_post_html(doi, title, category, summary, mindmap, citation): doi = doi.split("https://")[-1] mindmap = mindmap.replace("{", r'{').replace("}", r'}') citation = mistune.html(repr(citation.replace("&", "&").replace("```plaintext\n", "").replace("\n```", "").strip())[1:-1]) image = fetch_image(title, category, summary) html_summary = mistune.html(summary) post = f"""
{title.strip()}

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

Mindmap

If MindMap doesn't load, please try refreshing the page.


Citation

{citation}
""" 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) # print("_____________________\n\n",title,"\n\n_____________________") # with open('index.html', 'w', encoding='utf-8') as f: # f.write(post_body) # exit() 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): 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": r"{}".format(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