This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
class MediumPoster: | |
def __init__(self, token, domain): | |
self._token = token | |
self._domain = domain | |
self._endpoint = "https://api.medium.com/v1/" | |
self._headers = { | |
"Accept": "application/json", | |
"Accept-Charset": "utf-8", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wp_post_content_html = markDownToHtml(wp_markdown_content) | |
wp = WordpressPoster(domain, WP_USERNAME, WP_PASSWORD) | |
featuredImageID = wp.upload_media('./image.jpeg').get('id') | |
categoryIDs = wp.get_category_ids(["tutorial"]) | |
tagIDs = wp.get_tag_ids(['python','ai']) | |
wp_response = wp.create_post_wordpress( | |
title, wp_post_content_html, "draft", categoryIDs, tagIDs, featuredImageID |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def markDownToHtml(markDownContent): | |
html = markdown.markdown(markDownContent, extensions=["fenced_code"]) | |
return html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create_post_wordpress( | |
self, | |
title, | |
content, | |
publish_status="draft", | |
categories=[], | |
tags=[], | |
featuredImageId=None, | |
): | |
response = self.session.post( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_category_ids(self, categories): | |
category_ids = [] | |
category_endpoint = f"{self._endpoint}categories" | |
for category in categories: | |
response = self.session.post(category_endpoint, json={"name": category}) | |
# The term already exists | |
if "data" in response.json(): | |
category_ids.append(response.json()["data"]["term_id"]) | |
elif "id" in response.json(): | |
category_ids.append(response.json()["id"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def upload_media(self, filePath, alt_text="", caption=""): | |
new_session = self.session | |
data = open(filePath, "rb").read() | |
new_session.headers.update( | |
{ | |
"Content-Type": "", | |
"Content-Disposition": f'attachment; filename="{filePath}"', | |
} | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
class WordpressPoster: | |
def __init__(self, domain, username, password) -> None: | |
self._domain = domain | |
self._endpoint = f'{self._domain}wp-json/wp/v2/' | |
self._username = username | |
self._password = password | |
self._credentials = f"{self._username}:{self._password}" | |
self._token = base64.b64encode(self._credentials.encode()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from dotenv import load_dotenv | |
load_dotenv() | |
GITHUB_TOKEN = os.environ.get('GITHUB_TOKEN') | |
domain = "" | |
title = "" | |
post_wp = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
embedGist[blg](gists[counter]), | |
) | |
counter += 1 | |
newFileName = f"{blg}_gist+{title}" | |
with open(newFileName, "w+") as f: | |
f.write(new_content) | |
print("Saved file with gists for ", blg) | |
return new_content | |
result = {} |