Skip to content

Instantly share code, notes, and snippets.

@jhwhite
Last active August 29, 2015 14:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhwhite/2df093eff1f9bab74144 to your computer and use it in GitHub Desktop.
Save jhwhite/2df093eff1f9bab74144 to your computer and use it in GitHub Desktop.

#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'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment