DEV Community

Discussion on: Detecting DEV peak hours via API (bash study)

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/