Saturday, November 2, 2019

An Introduction

I’m Aaron Jacobs, a Data Scientist and Emacs user based in Toronto.

I’ve been using Emacs for about five years, originally because of org-mode.

But… why?

In 2015 I Had a Problem…

I fried the GPU on my only computer, and even though I was able to get it partially fixed, I was warned I should avoid ever running my computer “hot” again.

You know what made that computer run hot? My habit of livestreaming eSports on Twitch.tv.


It turns out that it was pretty easy to circumvent a browser for the streaming aspect – the Livestreamer program (now Streamlink) could take a Twitch.tv url and stream it to VLC or mpv. These programs are way more efficient at decoding video and didn’t make my computer run hot:

$ livestreamer www.twitch.tv/rush 480p --player mpv
[cli][info] Found matching plugin twitch...
[cli][info] Available streams: audio_only, 160p (worst), ...
[cli][info] Opening stream: 480p (hls)
[cli][info] Starting player: mpv

But that deprived me of all the search and discovery features of Twitch.tv – e.g. who is streaming right now?

I needed a way to emulate this search and discovery. Probably with the public Twitch.tv APIs.

Wait, Emacs can make HTTP requests, right? And parse JSON?

Yes We Can

I was able to get Emacs talking to the APIs and parsing the JSON responses that I wanted into some cl-struct objects:

ELISP> (twitch-api-search-streams)
(#s(twitch-api-stream "nightblue3" 8554 ...)
 #s(twitch-api-stream "mandiocaa1" 7896 "soloq challenger" ...)
 #s(twitch-api-stream "disguisedtoast" 5841 ...)
 #s(twitch-api-stream "karasmai" 1889 ...)
 ...)

(Largely thanks to Kris Jenkin’s famous Writing A Spotify Client in 16 Minutes video.)

But I needed a more ergonomic way to display this information than dumping it into the *scratch* buffer.

It Turns out Emacs has All These UI Tools

Ever used list-packages or list-processes? These are powered by tabulated-list-mode, which is easily extensible with your own list-like data:

Combined with modes and keymaps this can make for pretty simple and Emacs-y user interfaces.

(Plus there is a full suite of buttons and other GUI elements available in widgets.el.)

In the broader community, Helm and Ivy are fast and feature-rich search and selection frameworks that allow you to write mini applications:

(I also added Twitch chat support via Emacs’s built-in IRC clients.)

Roadbumps

The url.el package bundled with Emacs was buggy as hell at the time (though it has since improved). Even with careful coding sometimes the request would just hang until killed with C-g.

I adopted the same approach as Elfeed and just called out to curl directly instead.

I’m not sure if the story on HTTP requests has improved in the Emacs community in the past few years…

Plus, software needs love to last:

  • Streamlink/Livestreamer have occasionally been broken or required that I add new functionality to get things working again (e.g. Oauth support).

  • The Twitch.tv API I was using (v3) was deprecated a few years ago and finally turned off right after I submitted this talk (on September 12th). So I had to fix some code in a hurry!

Thanks!

I have been using this little bit of Elisp almost every day for more than four years.

The project is on GitHub as atheriel/helm-twitch.


(demo)



You can find me as @atheriel on GitHub and @unconj1 on Twitter.

I also blog at unconj.ca, mostly about R but occasionally about Emacs.