Skip to content

Instantly share code, notes, and snippets.

Avatar

Rahul Banerjee rahulbanerjee26

View GitHub Profile
View hashnodeClass.py
import requests
class HashnodePoster:
def __init__(self, token, domain):
self._token = token
self._endpoint = "https://api.hashnode.com"
self._domain = domain
self._headers = {"Authorization": self._token}
self._session = self._create_hashnode_session()
View devtoClass.py
import requests
class DevtoPoster:
def __init__(self, token, domain):
self._token = token
self._domain = domain
self._endpoint = "https://dev.to/api/"
self._headers = {
"Accept": "application/json",
"api-key": self._token,
View __init__.py
medium_post_content_html = markDownToHtml(medium_markdown_content)
medium = MediumPoster(MEDIUM_TOKEN, domain)
medium_url = medium.create_post_medium(
title,
medium_post_content_html,
canonicalUrl=wp_post_url,
tags=tags)
print(f'Find Medium Post: {medium_url}')
View mediumCreatePost.py
def create_post_medium(
self,
title,
content,
publish_status="draft",
contentFormat="html",
tags=[],
notifyFollowers = False,
canonicalUrl=None
):
View __init__.py
def getUserID(self):
response = self._session.get(f"{self._endpoint}me")
if response.status_code == 200:
return response.json()["data"]["id"]
else:
print("Error while retreiving user id")
print(response)
View mediumClass.py
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",
View wpSample.py
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
View markdownToHtml.py
def markDownToHtml(markDownContent):
html = markdown.markdown(markDownContent, extensions=["fenced_code"])
return html
View wpCreatePost.py
def create_post_wordpress(
self,
title,
content,
publish_status="draft",
categories=[],
tags=[],
featuredImageId=None,
):
response = self.session.post(
View wpCreateCategories.py
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"])