-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
29 lines (26 loc) · 1013 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
var request = require('request');
var cheerio = require('cheerio');
var Promise = require('promise');
function getPosts(medium) {
return new Promise(function (resolve, reject) {
request(`https://medium.com/${medium.user}/latest`, function (error, response, html) {
if (error) reject(error);
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
var posts = [];
$('.streamItem--postPreview').each(function(index, article) {
var image = $(article).find('.graf-image').first().data('image-id');
posts[index] = {
title: $(article).find('.graf--leading').text(),
time: $(article).find('time').text(),
image: image ? `https://cdn-images-1.medium.com/max/900/${image}` : null,
url: $(article).find('.postArticle-readMore').find('a').attr('href').split('?')[0]
}
})
posts.success = true;
resolve(posts);
}
});
});
}
module.exports = {getPosts};