From 6ba91ed109efd39488a5b6347afd6782f6ef0241 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Wed, 5 Jun 2019 23:19:05 +0200 Subject: [PATCH] fetch nav categories from Medium --- README.md | 38 ++++++++++++++++++++++++++++++++++++++ package.json | 2 +- webtask-medium.js | 22 ++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d607d0c..4bc4634 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ - [Medium](#medium) - [Get Latest Posts](#get-latest-posts) - [Get Followers Count](#get-followers-count) + - [Get Categories](#get-categories) - [YouTube](#youtube) - [MailChimp](#mailchimp) - [Zoho](#zoho) @@ -59,6 +60,43 @@ https://TASK_URL/TASK_NAME/:medium_username/followers } ``` +#### Get Categories + +Reflecting the navigation categories used in the blog. + +```bash +http://localhost:8080/:medium_username/categories + +# when published on webtask.io +https://TASK_URL/TASK_NAME/:medium_username/categories +``` + +**Response** + +```json +[ + { + "title": "Product", + "url": "https://blog.oceanprotocol.com/product/home" + }, { + "title": "Community", + "url": "https://blog.oceanprotocol.com/community/home" + }, { + "title": "Deep Tech", + "url": "https://blog.oceanprotocol.com/deep-tech/home" + }, { + "title": "People", + "url": "https://blog.oceanprotocol.com/people/home" + }, { + "title": "Token Launch", + "url": "https://blog.oceanprotocol.com/token/home" + }, { + "title": "Use Cases", + "url": "https://blog.oceanprotocol.com/usecases/home" + } +] +``` + ### 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/package.json b/package.json index 5853775..aa203eb 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "0.1.0", "private": true, "scripts": { - "start": "wt serve webtask-youtube.js", + "start": "wt serve webtask-medium.js", "test": "eslint ./*.js" }, "dependencies": { diff --git a/webtask-medium.js b/webtask-medium.js index 20c1498..49dfe15 100644 --- a/webtask-medium.js +++ b/webtask-medium.js @@ -63,4 +63,26 @@ app.get('/:username/followers', (req, res) => { }) }) +app.get('/:username/categories', (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 { navItems } = json.payload.collection + const parsedCategories = [] + let holder = {} + + if (error) return + + for (let i = 0; i < navItems.length; i++) { + holder.title = navItems[i].title + holder.url = navItems[i].url + parsedCategories.push(holder) + holder = {} + } + + res.send(parsedCategories) + }) +}) + module.exports = webtask.fromExpress(app)