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/3722a2ec4ad6da6eafe2dcdf79be653d to your computer and use it in GitHub Desktop.
Save rahulbanerjee26/3722a2ec4ad6da6eafe2dcdf79be653d to your computer and use it in GitHub Desktop.
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,
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36",
}
self._session = self._create_devto_session()
def _create_devto_session(self):
session = requests.session()
session.headers.update(self._headers)
return session
def create_post_devto(
self,
title,
body_markdown,
published=False,
series="",
main_image="",
canonical_url="",
description="",
tags=[],
):
data = {
"article": {
"title": title,
"body_markdown": body_markdown,
"published": published,
"tags": tags,
}
}
if canonical_url:
data["article"]["canonical_url"] = canonical_url
if main_image:
data["article"]["main_image"] = main_image
if series:
data["article"]["series"] = series
if description:
data["article"]["description"] = description
response = self._session.post(f"{self._endpoint}articles", json=data)
if response.status_code == 201:
return 'https://dev.to/dashboard'
else:
return 'ERROR'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment