DEV Community

Cover image for PyDevTo - unofficial dev.to api for python
Loftie Ellis
Loftie Ellis

Posted on • Originally published at loftie.com

PyDevTo - unofficial dev.to api for python

Dev.to has a pretty awesome community for sharing coding knowledge. I wanted to republish my posts to them (and also link to the comments) so I made this library, and in the interest of sharing decided to opensource it for anyone to use.

PyDevTo is a a wrapper around the dev.to api endoints, with a few helper functions to help with the distribution of articles.

Features

  • Implements all endpoints from https://docs.dev.to/api/

  • Implements a few other api endpoints not documented but available in the source, such as users and follow_suggestions.

  • Includes a helper method to convert html to dev.to specific markdown, including support for dev.to specific embeds such as YouTube.

Installation

Use the package manager pip to install pydevto.

pip install pydevto

Usage

Make sure you have an api key to use the authenticated endpoints. You can get your key from https://dev.to/settings/account (You can use pydevto without an api key for some functions, such as the public articles)

import pydevto
api = pydevto.PyDevTo(api_key='MY_KEY')
api.articles()  # returns list of your own published articles

Methods

import pydevto
api = pydevto.PyDevTo(api_key='MY_KEY')
api.public_articles(page=None, tag=None, username=None, state=None, top=None)  # Return list of public (published) articles
api.public_article(id)  # Return a single public (published) article given its id
api.articles(page=None, per_page=None, state="published")  # Return a list of user articles
api.create_article(...)  # Create an article
api.update_article(id, ...)  # Update an article
api.user(id=None, username=None)  # Return user information
api.follow_suggestions(page=None)  # Return list of follow suggestions
api.tags(page=None)  # Return list of tags
api.webhooks()  # Return list of webhooks
api.webhook(id)  # Return single webhook with id
api.create_webhook(source, target_url, events)  # Create a new webhook
api.delete_webhook(id)  # Delete  a webhook with id

PyDevTo contains a helper function to convert html to dev.to specific markdown. (https://dev.to/p/editor_guide) It supports images with captions using the HTML figcaption tag, and converts embeds such as YouTube to dev.to specific liquid tags.

>>> import pydevto
>>> pydevto.html_to_markdown('<h1>Heading</h1') 
>>> '# Heading\n\n'
>>> pydevto.html_to_markdown('<iframe src="https://www.youtube.com/embed/kmjiUVEMvI4"></iframe>') 
>>> '\n{% youtube kmjiUVEMvI4 %}\n'  

GitHub logo lpellis / pydevto

Unofficial dev.to api

PyDevTo

Unofficial dev.to api for python.

Features

  • Implements all endpoints from https://docs.dev.to/api/
  • Implements a few other api endpoints not documented but available in the source, such as users and follow_suggestions
  • Includes a helper method to convert html to dev.to specific markdown, including support for dev.to specific embeds such as YouTube.

Installation

Use the package manager pip to install pydevto.

pip install pydevto

Usage

Make sure you have an api key to use the authenticated endpoints. You can get your key from https://dev.to/settings/account (You can use pydevto without an api key for some functions, such as the public articles)

import pydevto
api = pydevto.PyDevTo(api_key='MY_KEY')
api.articles()  # returns list of your own published articles

Methods

import pydevto
api = pydevto.PyDevTo(api_key='MY_KEY')
api.public_articles(page=None, tag=None, username=None



Let me know if you run into any issues.

PS I originally published this post at https://loftie.com/post/pydevto-unofficial-devto-api and used this very library to make this post :)

Oldest comments (5)

Collapse
 
rhymes profile image
rhymes

Looks cool @lpellis !

What led you to integrate the markdown converter? Interesting approach to revert embeds to their own liquid tag original.

ps. I've recently started switching my projects from requests to httpx, mostly because it's more modern, supports async and doesn't have the weird aura of its author around it :D

Collapse
 
lpellis profile image
Loftie Ellis

The motivation for this library is being able to publish posts from my blog to dev.to. My posts are html, and while dev.to kinda supports some html it didn't really work good enough for me, hence the markdown conversion. I figured this type of syndication might be a common use case and is actually tricky to get right, so hope others can find it useful.

httpx looks very promising, does it work as basically a drop-in replacement for you? I have yet to really use asyncio, I've looked at aiohttp but found it quite a bit more complicated.

Collapse
 
rhymes profile image
rhymes

httpx looks very promising, does it work as basically a drop-in replacement for you?

yeah, for the most part, the only drawback is that httpx requires Python 3.6, it doesn't work with 3.5, but I see your library is already asking for 3.6.

I just switched my prototype client to httpx, you can see here: github.com/rhymes/devto-py/commit/...

I have yet to really use asyncio, I've looked at aiohttp but found it quite a bit more complicated

I've kinda given up on asyncio/aiohttp directly, they are really complicated. I'd like to try trio and/or starlette. But async, in general, is not easy :D

Collapse
 
nombrekeff profile image
Keff

Cool stuff, do you know if there is a similar thing but for JS? If not, I might give it a go! Or might create a post on how to create an API library for JS...

Collapse
 
mandarvaze profile image
Mandar Vaze

".. and used this very library to make this post"

Best part of the article ever.