Skip to content

Instantly share code, notes, and snippets.

@rahulbanerjee26
Created September 25, 2022 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahulbanerjee26/380f8eb8ba01504d135349ce30d8a55b to your computer and use it in GitHub Desktop.
Save rahulbanerjee26/380f8eb8ba01504d135349ce30d8a55b to your computer and use it in GitHub Desktop.
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()
def _create_hashnode_session(self):
session = requests.session()
session.headers.update(self._headers)
return session
def create_post_hashnode(
self,
title,
content,
publicationID,
tags=[],
canonicalUrl="",
hideFromHashnodeFeed=True,
):
query = """
mutation createPublicationStory($input: CreateStoryInput!,$publicationId: String!,$hideFromHashnodeFeed: Boolean) {
createPublicationStory(input: $input,publicationId: $publicationId, hideFromHashnodeFeed: $hideFromHashnodeFeed) {
code
success
message,
post{
type,
isDelisted,
isActive,
slug,
cuid,
_id,
}
}
}
"""
variables = {
"input": {
"title": title,
"contentMarkdown": content,
"tags": [{"_id": tag, "slug": tag, "name": tag} for tag in tags],
"isRepublished": {"originalArticleURL": canonicalUrl},
},
"publicationId": publicationID,
"hideFromHashnodeFeed": hideFromHashnodeFeed,
}
response = requests.post(
self._endpoint,
json={"query": query, "variables": variables},
headers=self._headers,
)
return self._domain + response.json()["data"]["createPublicationStory"]["post"]["slug"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment