This content originally appeared on DEV Community and was authored by Authors'
Medium.com has a little known API that does a few things. Here are the things it can do!
I initially got started researching Medium's interface to make a library which would sync and syndicate articles. The very initial use case was pulling articles from Medium, yet such a functionality isn't available on their documented API. A secondary use case is to syndicate articles to Medium, which is indeed available via a simple HTTP request (go figure!). It's quite a curious API.
The full docs are on Github. Medium developers no longer maintain it, but it is still open for use. Here's what it can do.
import requests as requests
access_token = "Bearer MEDIUMINTEGRATIONTOKENHERE"
base_url = "https://api.medium.com/v1"
headers = {}
headers['Authorization'] = access_token
def get_current_user():
req = requests.get(base_url + '/me', headers=headers)
res = req.json()
return res
def get_user_publications(user_id):
req = requests.get(base_url + '/users/' + user_id + '/publications', headers=headers)
res = req.json()
return res
def get_publication_contributors(pub_id):
req = requests.get(base_url + '/publications/' + pub_id + '/contributors', headers=headers)
res = req.json()
return res
def create_post(author_id, data):
# data = {
# title, (required) This is only used for SEO listing. You should also include the title in the content field. <100 chars
# contentFormat, (required) HTML or Markdown
# content, (required) Valid html or markdown of the article
# canonicalUrl, Original URL if the content was published elsewhere first
# tags, Only three tags will be counted. < 25 chars
# publishStatus, Must be “public”, “draft”, or “unlisted”. Public
# license, Valid values are “all-rights-reserved”, “cc-40-by”, “cc-40-by-sa”, “cc-40-by-nd”, “cc-40-by-nc”, “cc-40-by-nc-nd”, “cc-40-by-nc-sa”, “cc-40-zero”, “public-domain” Default: “all-rights-reserved”
# notifyFollowers, Boolean
# }
req = requests.post(base_url + '/users/' + author_id + '/posts', headers=headers, data=data)
res = req.json()
return res
def create_image(image, content_type):
# image, in this case, should be a file on-disk
# content_type must be one of 'image/jpeg', 'image/png', 'image/gif', or 'image/tiff'
files = {
'image': (image, open(image, 'rb'), content_type)
# 'image': ('symbol.png', open('symbol.png', 'rb'), 'image/png')
}
req = requests.post(base_url + '/images', files=files, headers=headers)
res = req.json()
return res
And that's all! The public API is no longer maintained, but it is still open, and it is still possible to get an Integration Token in one's Medium Settings. Grab an integration token, replace MEDIUMINTEGRATIONTOKENHERE
, and invoke those functions today.
A couple of other options exist for getting one's own articles from Medium. First, is to use the RSS feed functionality. The response from the RSS endpoint is XML, so most developers pipe the raw response through some RSS-to-JSON functionality (see: the first NPM library I ever made 👶, medium-post-parser).
Another option is to request a data download of all of one's Medium information via email. It is non-trivial to make this method purely automated, nevertheless if retrieving a .zip file from an email fits into one's workflow, it is surely a possiblity! You can request a .zip file containing HTML files of your Medium posts and drafts, info that you have shared on your profile, stories you’ve clapped on, and more in Medium Settings.
Next, I'll be investigating this typescript library which promises to solve the post retrieval shortcoming of the API.
A
FYI... This many tabs will be saved if you just copy the code!
Find the Author on X, @theholyspiritcc.
Coffees are accepted! BuyMeACoffee
This content originally appeared on DEV Community and was authored by Authors'
Authors' | Sciencx (2024-09-20T13:00:00+00:00) Medium.com API in Python. Retrieved from https://www.scien.cx/2024/09/20/medium-com-api-in-python/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.