Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 23 additions & 51 deletions getLatestArticles.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,24 @@
const Xray = require("x-ray")
const URL = require("url").URL
const axios = require("axios");

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
module.exports = async (tag) => {
const articles = (await axios.get(`https://dev.to/api/articles/?tag=${tag}`)).data;
// map articles to get the same object as in the previous version
return articles.map(({
title,
tag_list,
url,
user: {
name,
twitter_username,
}
}) => ({
title,
tags: tag_list.map(t => `#${t}`),
link: url,
author: {
name,
link: `https://dev.to/${name}`,
twitterHandle: `@${twitter_username}`,
},
}));
};