DEV Community

Cover image for Covid 19 Updates from Vim, with Python!
Paul
Paul

Posted on

Covid 19 Updates from Vim, with Python!

Alt Text
This was just a fun (and morbid) way to experiment with hacking on vim with Python.

If you have Vim compiled with Python and the Airline plugin installed, you should be able to just plop this into your vimrc:

💡 There is an updated version that works asynchronously below.

function! CovidUpdate()
python3 << EOF
from urllib import request
import json
import vim
def getCases():
  country = "US"
  res = request.urlopen("https://covid2019-api.herokuapp.com/country/%s" % country)
  string = res.read().decode()
  info = json.loads(string)[country]
  vim.vars["response"] = "Country: %s | Confimred:  %i | Deaths: %i | Recovered: %i " %  (country, info["confirmed"],  info["deaths"], info["recovered"])
getCases()
EOF

endfunction

call CovidUpdate()


" call airline#parts#define_function('foo', "CovidUpdate")
let g:airline_section_y = airline#section#create_right(['ffenc', response])

Enter fullscreen mode Exit fullscreen mode

Thanks to this project for providing the API.

🚨 UPDATE 🚨

Threading a bit of python via the vim module is actually really easy!

Updated version:

function! CovidUpdate()
python3 << EOF
from urllib import request
import threading
import json
import vim
def getCases():
  country = "US"
  res = request.urlopen("https://covid2019-api.herokuapp.com/country/%s" % country)
  string = res.read().decode()
  info = json.loads(string)[country]
  vim.vars["airline_section_y"] = "Country: %s | Confimred:  %i | Deaths: %i | Recovered: %i " %  (country, info["confirmed"],  info["deaths"], info["recovered"])
vim.async_call(getCases)
EOF

endfunction

call CovidUpdate()
Enter fullscreen mode Exit fullscreen mode

Note, the last line in the previous section:
let g:airline_section_y = airline#section#create_right(['ffenc', response])
is no longer necessary since that variable is set in the Python code. 🧠

Top comments (1)

Collapse
 
pcvonz profile image
Paul

The API seems to be a little buggy. It's returning zero cases in the US. Kinda nice :)