Skip to content

Instantly share code, notes, and snippets.

View rahulbanerjee26's full-sized avatar

Rahul Banerjee rahulbanerjee26

View GitHub Profile
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",
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
def markDownToHtml(markDownContent):
html = markdown.markdown(markDownContent, extensions=["fenced_code"])
return html
def create_post_wordpress(
self,
title,
content,
publish_status="draft",
categories=[],
tags=[],
featuredImageId=None,
):
response = self.session.post(
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"])
def get_tag_ids(self, tags):
tag_ids = []
tags_endpoint = f"{self._endpoint}tags"
for tag in tags:
response = self.session.post(tags_endpoint, json={"name": tag})
# The term already exists
if "data" in response.json():
tag_ids.append(response.json()["data"]["term_id"])
elif "id" in response.json():
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}"',
}
)
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())
import os
from dotenv import load_dotenv
load_dotenv()
GITHUB_TOKEN = os.environ.get('GITHUB_TOKEN')
domain = ""
title = ""
post_wp = True
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 = {}