1
0
mirror of https://github.com/oceanprotocol/webtasks synced 2025-01-09 05:14:51 +01:00

rewrite all the things

This commit is contained in:
Matthias Kretschmann 2018-03-20 18:34:32 +01:00
parent 496abf7851
commit a3200f2742
Signed by: m
GPG Key ID: 606EEEF3C479A91F
5 changed files with 92 additions and 48 deletions

View File

@ -0,0 +1,6 @@
{
"extends": "ascribe",
"rules": {
"func-names": 0
}
}

View File

@ -4,7 +4,7 @@ node_js: node
# will run `npm install` automatically # will run `npm install` automatically
script: script:
- npm run test - npm test
notifications: notifications:
email: false email: false

View File

@ -1,13 +1,35 @@
# Webtasks # 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) [![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) [![js ascribe](https://img.shields.io/badge/js-ascribe-39BA91.svg)](https://github.com/ascribe/javascript)
## Tasks ## 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 ## Development
@ -17,7 +39,17 @@ npm start
## Deployment ## 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 ## License

View File

@ -2,15 +2,15 @@
"name": "webtasks", "name": "webtasks",
"version": "0.1.0", "version": "0.1.0",
"private": true, "private": true,
"scripts": {
"start": "node webtask-medium.js",
"test": "eslint ./{src,public}/**/*.js"
},
"dependencies": { "dependencies": {
"express": "^4.16.3", "express": "^4.16.3",
"request": "^2.85.0", "request": "^2.85.0",
"webtask-tools": "^3.2.0" "webtask-tools": "^3.2.0"
}, },
"scripts": {
"start": "node webtask-medium.js",
"test": "eslint ./{src,public}/**/*.js"
},
"devDependencies": { "devDependencies": {
"babel-eslint": "^8.2.2", "babel-eslint": "^8.2.2",
"eslint": "^4.19.0", "eslint": "^4.19.0",

View File

@ -1,53 +1,59 @@
var express = require("express"); 'use strict' // eslint-disable-line
var request = require("request");
var webtask = require("webtask-tools");
var app = express();
app.get('/', function (req, res) { const express = require('express')
res.send('Please enter a username as a parameter'); 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) { app.get('/:username', (req, res) => {
var url = `https://medium.com/${req.params.username}/latest?format=json`; const url = `https://medium.com/${req.params.username}/latest?format=json`
request(url, (error, response) => {
const json = JSON.parse(response.body.replace('])}while(1);</x>', ''))
const posts = json.payload.posts // eslint-disable-line prefer-destructuring
const parsedPosts = []
let holder = {}
request(url, function (error, response, html) {
if (error) { if (error) {
console.log(error); return
return;
} }
var json = JSON.parse(response.body.replace("])}while(1);</x>", ""));
var posts = json.payload.posts; for (let i = 0; i < posts.length; i++) {
var parsedPosts = []; holder.id = posts[i].id
var holder = {}; holder.date = posts[i].firstPublishedAt
for (var i = 0; i < posts.length; i++) { holder.readingTime = posts[i].virtuals.readingTime
holder.id = posts[i].id; holder.title = posts[i].title
holder.date = posts[i].firstPublishedAt; holder.subtitle = posts[i].virtuals.subtitle
holder.readingTime = posts[i].virtuals.readingTime; holder.imageUrl = `https://cdn-images-1.medium.com/${posts[i].virtuals.previewImage.imageId}`
holder.title = posts[i].title; holder.postUrl = `https://medium.com/${req.params.username}/${posts[i].id}`
holder.subtitle = posts[i].virtuals.subtitle; parsedPosts.push(holder)
holder.imageUrl = holder = {}
"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) { res.send(parsedPosts)
var url = `https://medium.com/${req.params.username}/latest?format=json`; })
})
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);</x>', ''))
const posts = json.payload.posts // eslint-disable-line prefer-destructuring
request(url, function (error, response, html) {
if (error) { if (error) {
console.log(error); return
return;
} }
var json = JSON.parse(response.body.replace("])}while(1);</x>", ""));
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)