Files
wisforvedur/w.py

140 lines
3.4 KiB
Python
Raw Normal View History

2015-11-27 20:38:15 +00:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
2017-07-16 11:57:05 +00:00
# TODO, fetch the ipgeolocation, and basically compare against databse from xml on vedur - fetch the data and make an array
2015-11-27 20:38:15 +00:00
# FIXME, there is a bug with NULL/None values
2017-07-16 11:57:05 +00:00
import requests, os, sys, re, json
2015-11-27 20:38:15 +00:00
from xml.etree import ElementTree
import urllib
from datetime import date, datetime, time, timedelta
from pprint import pprint
import platform
2017-07-16 11:57:05 +00:00
from urllib2 import urlopen
from pprint import pprint
def getLocation():
url = 'http://ipinfo.io/json'
response = urlopen(url)
data = json.load(response)
IP=data['ip']
org=data['org']
city = data['city']
country=data['country']
region=data['region']
return data
if len(sys.argv) < 2:
locationOf = getLocation()
s2s = locationOf['city']
else:
2017-07-16 11:57:05 +00:00
s2s = sys.argv[1]
2017-07-16 11:57:05 +00:00
with open('wstations.json') as data_file:
data = json.load(data_file)
2017-07-16 11:57:05 +00:00
2015-11-27 20:38:15 +00:00
for wstation, value in data.iteritems():
if wstation == s2s:
pprint(value)
wstation_int = value
2015-11-27 20:38:15 +00:00
2015-11-27 20:38:15 +00:00
os.system('clear')
location = wstation_int
2017-07-16 11:57:05 +00:00
2015-11-27 20:38:15 +00:00
now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
justnow = datetime.strptime(now, "%Y-%m-%d %H:%M:%S")
mytime = datetime.strptime(now, "%Y-%m-%d %H:%M:%S")
# set the time range for the upcoming forecast
mytime += timedelta(hours=6)
xmlurl = 'http://xmlweather.vedur.is'
url = xmlurl + '?op_w=xml&type=obs&lang=en&view=xml&ids=' + str(location)
response = requests.get(url, stream=True)
response.raw.decode_content = True
events = ElementTree.iterparse(response.raw)
print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
for elem, event in events:
if event.tag == 'name':
print "Location: ", event.text
name = event.text
if event.tag == 'time':
print "Time: ", event.text, "UTC"
if event.tag == 'T':
print "Temperature: ", event.text, "°C"
temperature = event.text
if event.tag == 'W':
print "Conditions: ", event.text
conditions = event.text
if event.tag == 'F':
print "Wind: ", event.text, "m/s", " » " ,int(event.text) * 3.6, "km/h"
wind = event.text
url_update = xmlurl + '?op_w=xml&type=forec&lang=en&view=xml&ids=' + str(location)
response_update = requests.get(url_update, stream=True)
response_update.raw.decode_content = True
events_update = ElementTree.iterparse(response_update.raw)
print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
2015-11-30 20:33:03 +00:00
print "\n>>>> Upcoming: \n"
2015-11-27 20:38:15 +00:00
for elem, even in events_update:
if even.tag == 'T':
temp = even.text
if even.tag == 'W':
condi = even.text
if even.tag == 'F':
2015-11-30 20:33:03 +00:00
windu = even.text
2015-11-27 20:38:15 +00:00
if even.tag == 'ftime':
time = datetime.strptime(even.text,'%Y-%m-%d %H:%M:%S')
if time >= justnow and time < mytime:
print "Time:", time, "UTC"
print "Temperature: ", temp, "°C"
2015-11-30 20:33:03 +00:00
print "Wind: ", windu, "m/s", " » " ,int(windu) * 3.6, "km/h"
2015-11-27 20:38:15 +00:00
print "Conditions:", condi, "\n"
#break # the fall
print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
# the part for notification on linux, extra check up for mac
if os.name is "posix" and platform.system() == "Linux":
from gi.repository import Notify
2015-11-30 20:33:03 +00:00
notification_text = (name + "\n" + temperature + " C\n" + str(int(wind) * 3.6) + " km/h\n Conditions: " + str(conditions))
2015-11-27 20:38:15 +00:00
Notify.init("Weather")
Hello = Notify.Notification.new("Vedur.is", notification_text)
Hello.show()
2017-07-16 11:57:05 +00:00
sys.exit()