2

How to get dev.to posts from the API

 2 years ago
source link: https://dev.to/melvincarvalho/how-to-get-devto-posts-for-the-api-552g
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
How to get dev.to posts from the API

I've decided to download my dev.to posts so that I can store them in git. That means that if the site ever goes down, I have a copy of my blog content. The API is documented here.

The following endpoint will give a list of articles: https://dev.to/api/articles?username=melvincarvalho

It supports pagination, each page will contain 30 articles by default.

Replace melvincarvalho with your own username

So I wrote a JavaScript script that will pull out my articles.

#!/usr/bin/env node

// requires
const argv = require('minimist')(process.argv.slice(2))
const $ = require('child_process').execSync
const fs = require('fs')

// data
globalThis.data = {
  user: 'melvincarvalho',
  api: 'https://dev.to/api/articles/',
  datadir: '.'
}

// init
data.user = argv._[0] || data.user
data.api = argv.api || data.api
data.datadir = argv.datadir || data.datadir
console.log('data', data)

// main
const useruri = `${data.api}?username=${data.user}`
const cmd = `curl ${useruri}`
console.log('cmd', cmd)

const json = JSON.parse($(cmd).toString())
const output = JSON.stringify(json, null, 2)
const outfile = `${data.datadir}/posts.json`
console.log('output', output)

fs.writeFileSync(outfile, output)
Enter fullscreen modeExit fullscreen mode

Usage: ./getposts.js [username]

First we initialize the endpoint and username. Then we run some curl to get the result, and finally we format it and write it to a file.

The JSON output can be seen here and the current script is here

Now that I have a list of articles, it should be possible to download the markdown from individual articles too. I will hopefully cover that in a future post.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK