Skip to content

Instantly share code, notes, and snippets.

@jvanz
Last active January 12, 2017 00:49
Show Gist options
  • Save jvanz/b02e61147a7f05a2a308b3d5b1d0d382 to your computer and use it in GitHub Desktop.
Save jvanz/b02e61147a7f05a2a308b3d5b1d0d382 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import vim
from requests import get
OPEN_WEATHER_API_KEY = "your Open Weather API key"
def get_weather():
"""
Get weather from each line in the buffer
"""
buff = vim.current.buffer
for idx, line in enumerate(buff):
buff[idx] = line + "\t" + get_openweather(line)["weather"][0]["main"] # append the weather in the line
def get_weather_selection():
"""
Use the selected text to get the weather and append it in the selection row
"""
buf = vim.current.buffer # get current vim buffer
start = buf.mark("<") # get the begin of the selection
end = buf.mark(">") # get the end of the selection
location = get_text(start, end) # get the selection text with the city name
line = buf[end[0]-1] + "\t" + get_openweather(location)["weather"][0]["main"] # append the weather in the line
buf[end[0]-1] = line # update line in the buffer
def get_text(start, end):
"""
Get text between start and end delimiters
"""
buf = vim.current.buffer
text = ""
for line in buf[start[0]-1:end[0]]:
text += line
last_index = (len(text) - len(buf[end[0]-1])) + end[1] + 1
return text[start[1]:last_index]
def get_openweather(city):
url="http://api.openweathermap.org/data/2.5/weather" # open weather endpoint
payload = {"q": city, "APPID": OPEN_WEATHER_API_KEY} # prepare the request params
request = get(url, payload) # send request to the Open Weather API
return request.json()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment