Last active
August 29, 2015 14:19
-
-
Save ishankhare07/1bafa04a4fd3391ce7f9 to your computer and use it in GitHub Desktop.
weather notification
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from gi.repository import Notify | |
import requests | |
import time | |
import os | |
import sys | |
class notification: | |
""" | |
To run this script, simply type: | |
python3 weather.py [time_interval] city & | |
& -> does not block your terminal, you can close it after the above command | |
city -> name if city, like san francisco, ca | |
to use it inside your script: | |
from weather import notification | |
notification() | |
""" | |
def __init__(self, city): | |
self.interval = 300 | |
self.query = 'http://api.openweathermap.org/data/2.5/weather?q=' + city | |
self.icon_img = os.getcwd() + '/weather_icons' | |
if not os.path.isdir(self.icon_img): #check if icon directory exists | |
os.makedirs(self.icon_img) #create if not | |
if Notify.init("weather"): | |
self.update() | |
self.run() | |
else : | |
print('notification init returned false') | |
def run(self): | |
count = 0 | |
while 1: | |
time.sleep(float(self.interval)) | |
self.show() | |
count += 1 | |
if count == 3: | |
self.update() | |
count = 0 | |
def show(self): | |
#self.temp = self.weather['main']['temp_max'] - 273 | |
icon = self.weather['weather'][0]['icon'] | |
if not os.path.exists(self.icon_img + '/' + icon + '.png'): #check if icon downloaded | |
open(self.icon_img + '/' + icon + '.png', 'wb').write(requests.get('http://openweathermap.org/img/w/' + icon + '.png').content) #download and save | |
n = Notify.Notification.new( | |
"Weather Update", | |
"Temperature : %.2f\nDescription : %s" %(self.weather['main']['temp_max'] -273, self.weather['weather'][0]['description']), | |
self.icon_img + '/' + icon + '.png') | |
n.show() | |
print("Weather Update", "Temperature : %.2f" %(self.weather['main']['temp_max'] - 273), "Description : %s" %self.weather['weather'][0]['description'], sep='\n') | |
def update(self): | |
try: | |
if self.weather: | |
weather = self.weather | |
except: pass | |
try: | |
self.weather = requests.get(self.query).json() | |
except: | |
print('could not get the update') | |
self.weather = weather | |
if __name__ == "__main__": | |
try: | |
city = sys.argv[1] | |
except: | |
print('please provide a city name') | |
exit() | |
notification(city) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment