Created
September 25, 2022 22:36
-
-
Save rahulbanerjee26/3722a2ec4ad6da6eafe2dcdf79be653d to your computer and use it in GitHub Desktop.
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 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