-
Notifications
You must be signed in to change notification settings - Fork 2
/
getLatestArticles.js
52 lines (46 loc) · 1.27 KB
/
getLatestArticles.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const Xray = require("x-ray")
const URL = require("url").URL
const X = Xray({
filters: {
trim: value => value.trim(),
parseName: value => value.split("・")[0]
}
})
const getLatestArticles = async tag => {
const articles = await X(
`https://dev.to/t/${tag}/latest`,
"#substories .single-article",
[
{
title: ".index-article-link .content h3 | trim",
link: ".index-article-link@href",
tags: [".tags .tag"],
author: {
name: "h4 a | parseName",
link: ".small-pic-link-wrapper@href"
}
}
]
).then(articles => articles.filter(article => article.title))
// clean tags out of title
for (article of articles) {
article.tags.forEach(tag => {
if (article.title.indexOf(tag) > -1) {
article.title = article.title.split(tag)[1].trim()
}
})
}
// get twitter handle
for (article of articles) {
const socialLinks = await X(article.author.link, [
".profile-details .social a@href"
])
const twitter = socialLinks.find(url => url.includes("twitter.com/"))
if (twitter) {
const twitterURL = new URL(twitter)
article.author.twitterHandle = `@${twitterURL.pathname.substring(1)}`
}
}
return articles
}
module.exports = getLatestArticles