#Adding weather to your tmux status bar.
##Python file that writes the weather to a text file.
#!/Users/jhwhite/anaconda/bin/python
# -*- coding: utf-8 -*-
import forecastio
import math
from geopy.geocoders import Nominatim
geolocator = Nominatim()
# I keep my api key for forecast.io in a separate text file so I don't accidentally publish it anywhere.
with open('/Users/jhwhite/.config/forecast_api/api.txt', 'r') as f:
api_key = f.read()
weather_file = open('/Users/jhwhite/.weather.txt', 'w')
# forecast api requires the lat and long coords and I could look that up on Google Maps, but this is more fun!
zipCode = "22911"
location = geolocator.geocode(zipCode)
forecast = forecastio.load_forecast(api_key, location.latitude, location.longitude)
current_temp = forecast.currently()
day = forecast.daily()
temp = str(current_temp.temperature) + "° F"
high = str(int(math.ceil(day.data[0].temperatureMax)))
low = str(int(math.ceil(day.data[0].temperatureMin)))
weather_file.write(temp + ", " + str(current_temp.summary) + ", " + high +"/"+low + " F")
weather_file.close()
##LaunchAgent to run above script every 5 minutes
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.jhwhite.tmux_weather</string>
<key>ProgramArguments</key>
<array>
<string>/Users/jhwhite/tmux_weather.py</string>
</array>
<key>StartInterval</key>
<integer>300</integer>
</dict>
</plist>
##Line to add to your .tmux.conf file to display the weather and date
set -g status-right-length 150
set -g status-right '#(cat ~/.weather.txt) | %b %d %Y'