Image by Gratisography, as usual.

Because I got bored this weekend, I decided to waste some time adding a current events listing to the dodgy file host that I definitely don't run's warrant canary. Prepare yourself now for some terrifyingly bad code.

So the existing canary is/was a trashcan bash script that had a DATE variable that output 'today's' date in a somewhat standard format of YYYY-mm-dd (date +%Y-%m-%d), then informed the reader that as of that date, no one had issued any warrants or requests for anything. Just for fun, I GPG clearsign this message to "prove" it was legitimately automatically generated by me. I'll talk about that later.

So to do my news junk, I grabbed an API key from newsapi.org, who read the news so you don't have to generate a JSON dump of some 70 different news site feeds. Clearly we should curl their API to get some news titles, right? So to use the API, you toss your API key at their endpoint, along with a news source and maybe a sorting strategy (we're using latest because that's how we roll), and also let's just grab some USA Today because the same reason as before. So curl -s https://newsapi.org/v1/articles?source=usa-today&apiKey=someseriousapikeyfuntimes&sortBy=latest is what we are going to use to get some exactly 1 giant line of JSON, which is super not useful to me, I don't really need 5000+ characters of unformatted text.

So... we go out and use a nice thing like jq, which "is a lightweight and flexible command-line JSON processor". I'm sure there was a better way to do what I'm about to type out, buuuut... KAHBAM we're using jq. jq will make everything pretty and funky-fresh (it formats things in what normally would be described as pretty-print or something, what's important here is that everything gets output onto new lines).

Alright! Time for whatever I did next: egrep. egrep: when regular grep just doesn't global regular expression print hard enough. Fun fact (fun insomuch as I didn't care enough until I wrote this to find out), the appended e stands for Extended! Anyway, we actually only need 2 things from that newsapi call, the page title and the date it was published. We're gonna get that by using egrep -m 6 'title|publishedAt', also I only wanted 3 stories so that's what the -m 6 is about (6 because 2 lines each for title & published, right? ok, cool)

So that got us the title & publishing date, but they're formatted kinda... uselessly. EXAMPLE TIME:

        "title": "Man arrested near White House after claiming he had bomb",
        "publishedAt": "2017-03-19T14:38:29Z"
        "title": "The day in pictures",
        "publishedAt": "2017-03-19T14:53:48Z"
        "title": "Corrections & Clarifications",
        "publishedAt": "2013-01-10T01:25:39Z"

Fortunately for us, we can use awk. awk, for any of those who forget, is pretty swell at doing whatever you want. For values of whatever. GOOD NEWS: you can easily print just the stuff you need with awk -F\" '{print $4}', but that leaves the title and publishing date on separate lines. Guess what you can use to fix that? (yea, so it's definitely awk) Get ready, here we go: awk 'ORS=NR%2?" ":"\n"! Right, so the first one uses double quotes as the demarcation mark instead of spaces, then grabs the fourth thing, which is either the title or the purchase date/time, depending on the line. The second awk either eats or leaves the newline \n, on every other line. Do not ask me how or why, just accept it.

OK! So we have each thing on its own line with the relevant date & time, right?! (Yep.) Only thing, I want a few spaces to pad my news junk. Let's pipe that news junk into sed! Do some sed 's/^/ /' and you'll have a solid 5 spaces for padding! The ^ tells sed to focus on the beginning of the line, and then the / / (it's literally 5 spaces) adds spaces!

So here is the entire command in one conveniently hard to read line:

curl -s "https://newsapi.org/v1/articles?source=usa-today&apiKey=APIKEY&sortBy=latest" | jq | egrep -m 6 'title|published' | awk -F\" '{print $4}' | awk 'ORS=NR%2?" ":"\n"' | sed 's/^/     /'

Good news, it worked. Promise. Here's the output:

     Man arrested near White House after claiming he had bomb 2017-03-19T14:38:29Z
     The day in pictures 2017-03-19T14:53:48Z
     Corrections & Clarifications 2013-01-10T01:25:39Z

FYI, I then add it to a bunch of other text and use keybase.io's command line software to clearsign it to "prove" I did the thing, instead of some other not-me.

Hope you enjoyed that hot mess, have a graph that functions as the background for my book blog junk as a consolation prize. [GRAPH] (that URL is almost as long as the JSON file I linked somewhere up there ↑. Fun fact.)