diff --git a/getLatestArticles.js b/getLatestArticles.js index b724085..c0f4121 100644 --- a/getLatestArticles.js +++ b/getLatestArticles.js @@ -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}`, + }, + })); +}; \ No newline at end of file