Skip to content

Instantly share code, notes, and snippets.

@Cristuker
Created September 30, 2024 12:54
Show Gist options
  • Save Cristuker/6ec7e5a2c26b47fe3bebdf7ab8bf19ff to your computer and use it in GitHub Desktop.
Save Cristuker/6ec7e5a2c26b47fe3bebdf7ab8bf19ff to your computer and use it in GitHub Desktop.
list all articles from dev.to and save in md
const { writeFileSync} = require("node:fs");
async function getArticles(page) {
if (!page) page = 1;
const response = await fetch(`https://dev.to/api/articles?username=cristuker&page=${page}&per_page=100`);
if (!response.ok) {
throw new Error('Erro searching for articles');
}
const articles = await response.json()
return articles;
}
async function getArticleById(articleId) {
const response = await fetch(`https://dev.to/api/articles/${articleId}`);
if (!response.ok) {
console.log(response);
throw new Error('Error searching for this article');
}
const article= await response.json();
return article;
}
async function main () {
console.log('listando artigos');
const articles = await getArticles(1)
const articlesFull = [];
console.log('buscando arquivos por id');
for await (const article of articles) {
const articleFull = await getArticleById(article.id)
articlesFull.push(articleFull);
}
console.log('escrevendo artigos localmente');
for await (const article of articlesFull) {
writeFileSync(`./articles/${article.title}.md`, String(article.body_markdown))
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment