Friday, October 7, 2022
HomeSoftware EngineeringThe Medium RSS Feed’s Lacking Half | by Sabesan Sathananthan | Geek...

The Medium RSS Feed’s Lacking Half | by Sabesan Sathananthan | Geek Tradition | Jun, 2022


retrieves the stats (clapCount, voterCount, responseCount, and readingTime) of Medium posts

Picture by Ryoji Iwata on Unsplash

On this article, I’ll clarify the best way to retrieve the stats ( claps depend, voter depend, response depend, and studying time) of Medium feed posts utilizing a REST API, the shortage of this function within the Medium RSS feed is what motivated me to create this superior Medium API. That is my forty first Medium article.

I wished to acquire the stats ( claps depend, voter depend, response depend, and studying time) of my Medium posts whereas engaged on my private web site growth. 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 submit. At that second I puzzled how they carried out it? Then I overlook to analysis it however final month I discovered that pixelpoint.io’s widget shouldn’t be working. I seemed all over the 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 supplied then I seemed on the official documentation of the general public REST API by Medium, and nothing was there both. The Medium REST API solely permits you to create a Medium article by HTTP POST methodology(cf part 3.3). You possibly can’t retrieve or edit a Medium article by utilizing the Medium API 😔.

In an effort to get an understanding of how the helpful stats are retrieved within the Medium submit, 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 submit.

Inspecting a Medium submit web page.
curl https://medium.com/p/unique_Id_of_the_post >> submit.html

As a javascript variable, the claps depend, voter depend, response depend, 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 .

Picture by Kike Salazar N on Unsplash

In a medium submit, knowledge comes as JSON in a script tag. By utilizing javascript, these knowledge are picked up and rendered in DOM on the browser-side. Often, knowledge in a script tag takes this way:

Then I scrape Medium submit knowledge 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 attempt block, all of the tag script contents are mapped in an array. Then discover the component which incorporates window.__APOLLO_STATE__ , exchange the project window.__APOLLO_STATE__ = with an empty string. I exchange the /"/g with ‘"’ and parse with JSON.parse . Lastly, get the suitable values from the JSON.

Some great benefits of this method over headless browsers are tremendous quick and takes method much less processing and assets.

You can in a position to make use of the Superior Medium API that I developed as follows:

There are 5 varieties of requests. you would be capable to get all of the responses by utilizing HTTP GETmethodology.

Medium feed in JSON

You can in a position to get the RSS feed of the final 10 Medium posts by utilizing the next hyperlinks (exchange your username as a substitute of @username ).

medium.com/feed/@username 
or
username.medium.com/feed

The next request of the API offers the direct JSON conversion of that RSS Feed.

curl https://advanced-medium-api.herokuapp.com/medium/consumer/{userId}

Medium Superior Knowledge

You can in a position to get the Medium feed in JSON with the lacking a part of the Medium feed resembling clapCount, voterCount, responseCount, readingTime. every lacking knowledge injected in each submit(gadgets) object.

The next request of the API offers the JSON conversion of the RSS feed with the injection of lacking knowledge.

curl https://advanced-medium-api.herokuapp.com/superior/consumer/{userId}

Medium Personalized Knowledge

JSON conversion of the Medium RSS feed is custom-made in response to the classes. Order the Medium submit’s tags in response to their use depend 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 submit among the many different tags of that Medium submit.

The next request of the API offers the custom-made model of the Medium feed in JSON

curl https://advanced-medium-api.herokuapp.com/custom-made/consumer/{userId}

Medium Personalized Superior Knowledge

This response comprises the Medium custom-made knowledge with the injection of lacking stats resembling clapCount, voterCount, responseCount, readingTime.

The next request of the API offers the custom-made model of the Medium feed in JSON with the lacking a part of the Medium feed.

curl https://advanced-medium-api.herokuapp.com/superior/custom-made/consumer/{userId}

Lacking knowledge of a specific submit.

This response comprises solely the lacking half (clapCount, voterCount, responseCount, readingTime) of Medium feed for a specific Medium submit.

Request 💻 ➡ 🌎 :

curl https://advanced-medium-api.herokuapp.com/medium/submit/{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 submit. 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 shouldn’t be working and this incident impacts my private web site. Due to this fact I pushed myself and created this API.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

11 − four =

Most Popular

Recent Comments