DEV Community

Detecting DEV peak hours via API (bash study)

Rodion Gorkovenko on February 15, 2020

It may be interesting, when there are more people reading DEV - for example, perfect timing is important for those who is obsessed with idea to get...
Collapse
 
mzaini30 profile image
Zen

Wow. I motivated to build app that get random posts from Dev.

I see in docs.dev.to/api/#operation/getArti..., JSON only get 30 posts. Can I reach more than 30?

Collapse
 
rodiongork profile image
Rodion Gorkovenko • Edited

Hi, I think yes, though it is not documented. Source code contains per_page parameter which governs amount of articles in the response.

For example:

dev.to/api/articles/?per_page=3

However, probably it is not good idea to load very large responses (don't know if there is limit). Rather use page parameter to send several requests to different pages. E.g.

dev.to/api/articles/?page=333

Collapse
 
mzaini30 profile image
Zen • Edited

Then, how to get 10 posts from Dev randomly? 😂

Oh. I got idea:

  • Get number of total posts (eg: 1000)
  • Get 10 number randomly from 0 to 1000
Thread Thread
 
rodiongork profile image
Rodion Gorkovenko

Yep. Good idea! Just exactly what is described in this article above :)

Thread Thread
 
mzaini30 profile image
Zen

😂 thanks

Collapse
 
rhymes profile image
rhymes

As I couldn't use:

curl https://dev.to/api/articles/ | grep -oP '(?<=\"id\"\:)\d+' | head -n 1

as -P doesn't work in macOS I replaced it with the utility I use to pretty print JSON, jq:

> curl https://dev.to/api/articles | jq '.[0].id'
266498

or even better, as we don't need the entire page:

> total=$(curl "https://dev.to/api/articles?per_page=1" | jq '.[0].id')
> echo $total
266498

this doesn't work in macOS either:

curl https://dev.to/api/articles/246755 | sed -r 's/.*created_at\"\:\".{11}(..).*/\1/'

I replaced it with:

> curl https://dev.to/api/articles/246755 | jq '.created_at' | sed 's/^.*T\(.*\)\:.*\:.*/\1/'
20

or even better:

> curl https://dev.to/api/articles/246755 | jq '.created_at[11:13]' | sed 's/"//g'
20

jq is truly awesome :D stedolan.github.io/jq/manual/