dotfiles

Obligatory dotfiles repo
git clone git://git.danielmoch.com/dotfiles.git
Log | Files | Refs

commit ac00c870363dfe7268d5d0cbe04ddc9a545fb1d6
parent 54019d944b776ca3ce6cd60a8609752f6728cfa1
Author: Daniel Moch <daniel@danielmoch.com>
Date:   Thu,  1 Feb 2018 06:27:44 -0500

Refactor weather forecast

Diffstat:
D.config/forecastio/weather.py | 44--------------------------------------------
M.local/bin/my | 2+-
M.local/bin/my-init | 1+
M.local/lib/cron/weather.sh | 2+-
R.config/forecastio/org.djmoch.forecastio.plist -> .local/lib/forecastio/org.djmoch.forecastio.plist | 0
A.local/lib/forecastio/weather.py | 46++++++++++++++++++++++++++++++++++++++++++++++
M.tmux.conf | 2+-
7 files changed, 50 insertions(+), 47 deletions(-)

diff --git a/.config/forecastio/weather.py b/.config/forecastio/weather.py @@ -1,44 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -from datetime import datetime -import math -from os import environ -import sys -import forecastio -import requests - -def generate_forecast(): - error = False - - try: - # I keep my api key for forecast.io in a separate text file so I don't accidentally publish it anywhere. - with open('forecastio_api.txt', 'r') as apifile: - api_key = apifile.read() - api_key = api_key.strip() - - # Resolving location from IP is always going to be a best guess, but it's better than hardcoding location data - latitude, longitude = requests.get('https://ipinfo.io').json()['loc'].split(',') - - # Now we're ready to actually get the weather - forecast = forecastio.load_forecast(api_key, latitude, longitude) - - current_temp = forecast.currently() - day = forecast.daily() - - temp = str(current_temp.temperature) + "\u00b0 F" - high = str(int(math.ceil(day.data[0].temperatureMax))) - low = str(int(math.ceil(day.data[0].temperatureMin))) - - with open('current_forecast.txt', 'w') as outfile: - outfile.write(temp + ", " + str(current_temp.summary) + ", " + high +"\u00b0/"+low + "\u00b0 F") - except requests.packages.urllib3.exceptions.NewConnectionError: - error = True - sys.stdout.writelines(datetime.now().strftime("%m/%d/%Y %I:%M:%S:") + " Could not connect. Will try again \ - in 5 minutes.\n") - - if not error: - sys.stdout.writelines(datetime.now().strftime("%m/%d/%Y %I:%M:%S:") + " Successfully updated weather\n") - -if __name__ == '__main__': - sys.stdout.writelines(datetime.now().strftime("%m/%d/%Y %I:%M:%S:") + " Updating weather\n") - generate_forecast() diff --git a/.local/bin/my b/.local/bin/my @@ -267,7 +267,7 @@ case $command in ;; status) echo -n "Batt: `my battery percent`% | " - cat $HOME/.config/forecastio/current_forecast.txt + cat $HOME/.local/var/forecastio/current_forecast.txt echo -n " | " [ -z "$1" ] && date +'%l:%M %p' exit 0 diff --git a/.local/bin/my-init b/.local/bin/my-init @@ -119,6 +119,7 @@ then fi [ ! -d "$HOME/.local/var/log" ] && mkdir "$HOME/.local/var/log" +[ ! -d "$HOME/.local/var/forecastio" ] && mkdir "$HOME/.local/var/forecastio" if [ ! -f "$HOME/._.djmoch" ] then diff --git a/.local/lib/cron/weather.sh b/.local/lib/cron/weather.sh @@ -7,6 +7,6 @@ else exit 1 fi -cd "$HOME/.config/forecastio/" +cd "$HOME/.local/lib/forecastio/" ./weather.py diff --git a/.config/forecastio/org.djmoch.forecastio.plist b/.local/lib/forecastio/org.djmoch.forecastio.plist diff --git a/.local/lib/forecastio/weather.py b/.local/lib/forecastio/weather.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +from datetime import datetime +import math +import os +import sys +import forecastio +import requests + +def generate_forecast(): + error = False + + try: + # I keep my api key for forecast.io in a separate text file so I don't accidentally publish it anywhere. + with open('forecastio_api.txt', 'r') as apifile: + api_key = apifile.read() + api_key = api_key.strip() + + # Resolving location from IP is always going to be a best guess, but it's better than hardcoding location data + latitude, longitude = requests.get('https://ipinfo.io').json()['loc'].split(',') + + # Now we're ready to actually get the weather + forecast = forecastio.load_forecast(api_key, latitude, longitude) + + current_temp = forecast.currently() + day = forecast.daily() + + temp = str(current_temp.temperature) + "\u00b0 F" + high = str(int(math.ceil(day.data[0].temperatureMax))) + low = str(int(math.ceil(day.data[0].temperatureMin))) + + out_filename = os.path.join(os.environ['HOME'], '.local/var/forecastio/current_forecast.txt') + + with open(out_filename, 'w') as outfile: + outfile.write(temp + ", " + str(current_temp.summary) + ", " + high +"\u00b0/"+low + "\u00b0 F") + except requests.packages.urllib3.exceptions.NewConnectionError: + error = True + sys.stdout.writelines(datetime.now().strftime("%m/%d/%Y %I:%M:%S:") + " Could not connect. Will try again \ + in 5 minutes.\n") + + if not error: + sys.stdout.writelines(datetime.now().strftime("%m/%d/%Y %I:%M:%S:") + " Successfully updated weather\n") + +if __name__ == '__main__': + sys.stdout.writelines(datetime.now().strftime("%m/%d/%Y %I:%M:%S:") + " Updating weather\n") + generate_forecast() diff --git a/.tmux.conf b/.tmux.conf @@ -109,7 +109,7 @@ set-option -g display-panes-colour $tm_color_inactive # clock set-window-option -g clock-mode-colour $tm_color_active -tm_weather="#[fg=$tm_color_feature]#(cat ~/.config/forecastio/current_forecast.txt)" +tm_weather="#[fg=$tm_color_feature]#(cat ~/.local/var/forecastio/current_forecast.txt)" tm_battery="#(~/.local/lib/tmux/battery_indicator.sh)" tm_date="#[fg=$tm_color_inactive] %R %d %b"