Last active
August 11, 2022 13:36
-
-
Save sabesansathananthan/e81736fcbdea73f763d670e8b4f8d71e to your computer and use it in GitHub Desktop.
The Missing Part of Medium RSS Feed
This file contains hidden or 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
const request = require("request"); | |
const cheerio = require("cheerio"); | |
const url = "https://medium.com/p/d5b52f24d53c"; | |
const element = url.split("/"); | |
const postId = element[element.length - 1]; | |
request(url, async (error, response, body) => { | |
if (error) { | |
console.log(error); | |
} else if (body) { | |
var $ = cheerio.load(body, { xmlMode: true }); | |
try { | |
const scriptTagElements = $("script") | |
.map((idx, el) => $(el).html()) | |
.toArray(); | |
const findState = scriptTagElements.find((element) => { | |
if (element.includes("window.__APOLLO_STATE__")) { | |
return true; | |
} | |
}); | |
const apolloState = findState.replace("window.__APOLLO_STATE__ = ", ""); | |
const jsonOfApolloState = JSON.parse(apolloState.replace(/"/g, '"')); | |
const clapCount = await jsonOfApolloState[`Post:${postId}`].clapCount; | |
const responseCount = await jsonOfApolloState[`Post:${postId}`] | |
.postResponses.count; | |
const readingTime = await jsonOfApolloState[`Post:${postId}`].readingTime; | |
const voterCount = await jsonOfApolloState[`Post:${postId}`].voterCount; | |
const scrapJson = { | |
clapCount: clapCount, | |
responseCount: responseCount, | |
voterCount: voterCount, | |
readingTime: Math.round(readingTime), | |
}; | |
console.log(scrapJson); | |
} catch (e) { | |
console.log("Error parsing profile", e); | |
} | |
} else { | |
console.log("No body"); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
🙌🏻