retrieves the stats (clapCount, voterCount, responseCount, and readingTime) of Medium posts
On this article, I’ll clarify the best way to retrieve the stats ( claps rely
, voter rely
, response rely
, and studying time
) of Medium feed posts utilizing a REST API, the shortage of this characteristic within the Medium RSS feed is what motivated me to create this superior Medium API. That is my forty first Medium article.
I needed to acquire the stats (
claps rely
,voter rely
,response rely
, andstudying time
) of my Medium posts whereas engaged on my private web site improvement. 2 years earlier than once I implement the medium widget to my web site I discovered Pixelpoint.io which generated a widget with clap counts of a medium publish. At that second I questioned how they applied it? Then I neglect to analysis it however final month I discovered that pixelpoint.io’s widget isn’t working. I appeared in every single place for an answer to my wants however couldn’t discover one, so I had no alternative however to code it myself.
My preliminary instinct was to take a look at the RSS feed however there have been no such particulars are offered then I appeared on the official documentation of the general public REST API by Medium, and nothing was there both. The Medium REST API solely lets you create a Medium article by HTTP POST
methodology(cf part 3.3). You possibly can’t retrieve or edit a Medium article through the use of the Medium API 😔.
To be able to get an understanding of how the helpful stats are retrieved within the Medium publish, I began to examine the Medium utilizing browser developer instruments. Shockingly, there was no request to acquire the stats amongst all of the XHR calls, which push me to take a look at the HTML response of the Medium publish.
curl https://medium.com/p/unique_Id_of_the_post >> publish.html
As a javascript variable, the claps rely, voter rely, response rely, and studying time are straight injected into the HTML response. You possibly can confirm that by typing console.log(window.__APOLLO_STATE__);
within the Javascript console of the Publish web page.
window.__APOLLO_STATE__ object
Then I scrape these helpful stats utilizing request
and cheerio
.
In a medium publish, information comes as JSON in a script tag. Through the use of javascript, these information are picked up and rendered in DOM on the browser-side. Normally, information in a script tag takes this way:
Then I scrape Medium publish information with no headless browser.
First, I get the web site and search for errors. Then I confirmed whether or not the physique exists, and cargo the physique utilizing cheerio
in $
. In strive
block, all of the tag script contents are mapped in an array. Then discover the factor which incorporates window.__APOLLO_STATE__
, change the project window.__APOLLO_STATE__ =
with an empty string. I change the /"/g
with ‘"
’ and parse with JSON.parse
. Lastly, get the suitable values from the JSON.
The benefits of this system over headless browsers are tremendous quick and takes manner much less processing and sources.
You may ready to make use of the Superior Medium API that I developed as follows:
There are 5 varieties of requests. you could possibly have the ability to get all of the responses through the use of HTTP GET
methodology.
Medium feed in JSON
You may capable of get the RSS feed of the final 10 Medium posts through the use of the next hyperlinks (change your username as an alternative of @username
).
medium.com/feed/@username
or
username.medium.com/feed
The next request of the API provides the direct JSON conversion of that RSS Feed.
curl https://advanced-medium-api.herokuapp.com/medium/consumer/{userId}
Medium Superior Knowledge
You may capable of get the Medium feed in JSON with the lacking a part of the Medium feed akin to clapCount
, voterCount
, responseCount
, readingTime
. every lacking information injected in each publish(gadgets) object.
The next request of the API provides the JSON conversion of the RSS feed with the injection of lacking information.
curl https://advanced-medium-api.herokuapp.com/superior/consumer/{userId}
Medium Personalized Knowledge
JSON conversion of the Medium RSS feed is personalized based on the classes. Order the Medium publish’s tags based on their use rely among the many newest 10 posts and the tagOrder
return that tags’ order rank. Medium’s newest 10 posts had been divided by 3 and each 3 posts had been pushed in an array and people arrays had been pushed in a single array. There may be an algorithm that returns probably the most appropriate tag for the Medium publish among the many different tags of that Medium publish.
The next request of the API provides the personalized model of the Medium feed in JSON
curl https://advanced-medium-api.herokuapp.com/personalized/consumer/{userId}
Medium Personalized Superior Knowledge
This response incorporates the Medium personalized information with the injection of lacking stats akin to clapCount
, voterCount
, responseCount
, readingTime
.
The next request of the API provides the personalized model of the Medium feed in JSON with the lacking a part of the Medium feed.
curl https://advanced-medium-api.herokuapp.com/superior/personalized/consumer/{userId}
Lacking information of a selected publish.
This response incorporates solely the lacking half (clapCount
, voterCount
, responseCount
, readingTime
) of Medium feed for a selected Medium publish.
Request 💻 ➡ 🌎 :
curl https://advanced-medium-api.herokuapp.com/medium/publish/{postId}
Response 🌎➡💻 :
{
"clapCount": 98,
"responseCount": 4,
"voterCount": 12,
"readingTime": 4
}
This API is Open Supply and I welcome your contributions. final 2 years I used the medium widget from Pixelpoint.io which generated a widget with clap counts of a medium publish. I didn’t do analysis to get the lacking a part of the Medium feed once I used Pixelpoint.io. however 2 months earlier than Pixelpoint.io isn’t working and this incident impacts my private web site. Subsequently I pushed myself and created this API.