diff --git a/README.md b/README.md index d90d74a..0fb66f1 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ - [Tasks](#tasks) - [Medium](#medium) + - [Get Latest Posts](#get-latest-posts) + - [Get Followers Count](#get-followers-count) - [YouTube](#youtube) - [Bounties](#bounties) - [MailChimp](#mailchimp) @@ -30,6 +32,8 @@ **`webtask-medium.js`**: Generic task to fetch and reconstruct items from any medium publication. +#### Get Latest Posts + Requires the Medium username appended at the end of the url: ```bash @@ -39,6 +43,23 @@ http://localhost:8080/:medium_username https://TASK_URL/TASK_NAME/:medium_username ``` +#### Get Followers Count + +```bash +http://localhost:8080/:medium_username/followers + +# when published on webtask.io +https://TASK_URL/TASK_NAME/:medium_username/followers +``` + +**Response** + +```json +{ + "followers": 2528 +} +``` + ### YouTube **`webtask-youtube.js`**: Generic task to fetch and reconstruct items from any YouTube account. YouTube API key is provided via [secret environment variable](https://webtask.io/docs/issue_parameters) `YOUTUBE_API_KEY` setup in web editor of webtask.io. diff --git a/webtask-medium.js b/webtask-medium.js index b73564d..20c1498 100644 --- a/webtask-medium.js +++ b/webtask-medium.js @@ -50,4 +50,17 @@ app.get('/:username/raw', (req, res) => { }) }) +app.get('/:username/followers', (req, res) => { + const url = `https://medium.com/${req.params.username}?format=json` + + request(url, (error, response) => { + const json = JSON.parse(response.body.replace('])}while(1);', '')) + const { collection } = json.payload + + if (error) return + + res.send(`{ "followers": ${collection.metadata.followerCount} }`) + }) +}) + module.exports = webtask.fromExpress(app)