diff --git a/.eslintrc b/.eslintrc index e69de29..f713a59 100644 --- a/.eslintrc +++ b/.eslintrc @@ -0,0 +1,6 @@ +{ + "extends": "ascribe", + "rules": { + "func-names": 0 + } +} diff --git a/.travis.yml b/.travis.yml index 2653af8..69521fc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ node_js: node # will run `npm install` automatically script: - - npm run test + - npm test notifications: email: false diff --git a/README.md b/README.md index 50ab7c6..3905b84 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,35 @@ # Webtasks -> Ocean Protocol's webtasks +> Ocean Protocol's webtasks doing automatic things for us via webtask.io + +![giphy](https://user-images.githubusercontent.com/90316/37671913-0eb2f70a-2c6d-11e8-809e-04d3b40ef1c9.gif) [![Build Status](https://travis-ci.com/oceanprotocol/webtasks.svg?token=3psqw6c8KMDqfdGQ2x6d&branch=master)](https://travis-ci.com/oceanprotocol/webtasks) [![js ascribe](https://img.shields.io/badge/js-ascribe-39BA91.svg)](https://github.com/ascribe/javascript) ## Tasks -- `webtask-medium.js`: Generic task to fetch and and reconstruct items from any medium publication. +**`webtask-medium.js`**: Generic task to fetch and reconstruct items from any medium publication. + +Requires the Medium username appended at the end of the url, e.g. locally: + +```js +fetch('http://localhost:4000/oceanprotocol') + .then(res => res.json()) + .then(posts => { + const lastPosts = posts.slice(0, 3) + }) +``` + +When published as a web task, append the taskname followed by the Medium username at the end: + +```js +fetch('https://wt-bfc3ae9804422f8a4ea114dc7c403296-0.run.webtask.io/medium/oceanprotocol') + .then(res => res.json()) + .then(posts => { + const lastPosts = posts.slice(0, 3) + }) +``` ## Development @@ -17,7 +39,17 @@ npm start ## Deployment +All tasks are running serverless on webtask.io where you can login with your GitHub account. Then interact with it locally with the `wt-cli`: +```bash +npm install wt-cli -g +wt init YOUR_GITHUB_EMAIL + +wt create webtask-medium.js --name medium + +# make sure it's there and get url +wt ls +``` ## License diff --git a/package.json b/package.json index 3dbee53..9fc937a 100644 --- a/package.json +++ b/package.json @@ -2,15 +2,15 @@ "name": "webtasks", "version": "0.1.0", "private": true, + "scripts": { + "start": "node webtask-medium.js", + "test": "eslint ./{src,public}/**/*.js" + }, "dependencies": { "express": "^4.16.3", "request": "^2.85.0", "webtask-tools": "^3.2.0" }, - "scripts": { - "start": "node webtask-medium.js", - "test": "eslint ./{src,public}/**/*.js" - }, "devDependencies": { "babel-eslint": "^8.2.2", "eslint": "^4.19.0", diff --git a/webtask-medium.js b/webtask-medium.js index b11aeaa..c511f7c 100644 --- a/webtask-medium.js +++ b/webtask-medium.js @@ -1,53 +1,59 @@ -var express = require("express"); -var request = require("request"); -var webtask = require("webtask-tools"); -var app = express(); +'use strict' // eslint-disable-line -app.get('/', function (req, res) { - res.send('Please enter a username as a parameter'); +const express = require('express') +const request = require('request') +const webtask = require('webtask-tools') + +const app = express() + +app.get('/', (req, res) => { + res.send('Please enter a username as a parameter') }) -app.get("/:username", function (req, res) { - var url = `https://medium.com/${req.params.username}/latest?format=json`; +app.get('/:username', (req, res) => { + const url = `https://medium.com/${req.params.username}/latest?format=json` + + request(url, (error, response) => { + const json = JSON.parse(response.body.replace('])}while(1);', '')) + const posts = json.payload.posts // eslint-disable-line prefer-destructuring + const parsedPosts = [] + let holder = {} - request(url, function (error, response, html) { if (error) { - console.log(error); - return; + return } - var json = JSON.parse(response.body.replace("])}while(1);", "")); - var posts = json.payload.posts; - var parsedPosts = []; - var holder = {}; - for (var i = 0; i < posts.length; i++) { - holder.id = posts[i].id; - holder.date = posts[i].firstPublishedAt; - holder.readingTime = posts[i].virtuals.readingTime; - holder.title = posts[i].title; - holder.subtitle = posts[i].virtuals.subtitle; - holder.imageUrl = - "https://cdn-images-1.medium.com/" + - posts[i].virtuals.previewImage.imageId; - holder.postUrl = "https://medium.com/" + req.params.username + "/" + posts[i].id; - parsedPosts.push(holder); - holder = {}; + + for (let i = 0; i < posts.length; i++) { + holder.id = posts[i].id + holder.date = posts[i].firstPublishedAt + holder.readingTime = posts[i].virtuals.readingTime + holder.title = posts[i].title + holder.subtitle = posts[i].virtuals.subtitle + holder.imageUrl = `https://cdn-images-1.medium.com/${posts[i].virtuals.previewImage.imageId}` + holder.postUrl = `https://medium.com/${req.params.username}/${posts[i].id}` + parsedPosts.push(holder) + holder = {} } - res.send(parsedPosts); - }); -}); -app.get("/:username/raw", function (req, res) { - var url = `https://medium.com/${req.params.username}/latest?format=json`; + res.send(parsedPosts) + }) +}) + +app.get('/:username/raw', (req, res) => { + const url = `https://medium.com/${req.params.username}/latest?format=json` + + request(url, (error, response) => { + const json = JSON.parse(response.body.replace('])}while(1);', '')) + const posts = json.payload.posts // eslint-disable-line prefer-destructuring - request(url, function (error, response, html) { if (error) { - console.log(error); - return; + return } - var json = JSON.parse(response.body.replace("])}while(1);", "")); - var posts = json.payload.posts; - res.send(posts); - }); -}); -module.exports = webtask.fromExpress(app); + res.send(posts) + }) +}) + +app.listen(4000, () => console.log('Example app listening on localhost:4000!')) // eslint-disable-line no-console + +module.exports = webtask.fromExpress(app)