1
0
mirror of https://github.com/oceanprotocol/webtasks synced 2024-11-24 02:55:13 +01:00
webtasks/webtask-medium.js

58 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-03-20 18:34:32 +01:00
'use strict' // eslint-disable-line
2018-03-20 18:12:48 +01:00
2018-03-20 18:34:32 +01:00
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')
2018-03-20 18:12:48 +01:00
})
2018-03-20 18:34:32 +01:00
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);</x>', ''))
2018-06-28 19:22:37 +02:00
const { posts } = json.payload
2018-03-20 18:34:32 +01:00
const parsedPosts = []
let holder = {}
2018-03-20 18:12:48 +01:00
if (error) {
2018-03-20 18:34:32 +01:00
return
2018-03-20 18:12:48 +01:00
}
2018-03-20 18:34:32 +01:00
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 = {}
2018-03-20 18:12:48 +01:00
}
2018-03-20 18:34:32 +01:00
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);</x>', ''))
2018-06-28 19:22:37 +02:00
const { posts } = json.payload
2018-03-20 18:12:48 +01:00
if (error) {
2018-03-20 18:34:32 +01:00
return
2018-03-20 18:12:48 +01:00
}
2018-03-20 18:34:32 +01:00
res.send(posts)
})
})
module.exports = webtask.fromExpress(app)