Code: Select all
import time
import urllib2, time
from xml.dom import minidom
import datetime
import codecs
import webbrowser
import easygui as eg
import Tkinter
#title = "Start"
#msg = "Touch Ok to begin"
#eg.msgbox(msg, title)
weather_xml = urllib2.urlopen('http://graphical.weather.gov/xml/SOAP_server/ndfdSOAPclientByDay.php?whichClient=NDFDgenByDay&lat=42.0228&lon=-93.4522&format=24+hourly&numDays=4&Unit=e').read() #if i try and define the lat and lon with string formating it wil fail i dont know why
dom = minidom.parseString(weather_xml)
xml_temperatures = dom.getElementsByTagName('temperature')
highs = [None]*4
lows = [None]*4
for item in xml_temperatures:
if item.getAttribute('type') == 'maximum':
values = item.getElementsByTagName('value')
for i in range(len(values)):
highs[i] = int(values[i].firstChild.nodeValue)
if item.getAttribute('type') == 'minimum':
values = item.getElementsByTagName('value')
for i in range(len(values)):
lows[i] = int(values[i].firstChild.nodeValue)
xml_icons = dom.getElementsByTagName('icon-link')
icons = [None]*4
for i in range(len(xml_icons)):
icons[i] = xml_icons[i].firstChild.nodeValue.split('/')[-1].split('.')[0].rstrip('0123456789')
xml_day_one = dom.getElementsByTagName('start-valid-time')[0].firstChild.nodeValue[0:10]
day_one = datetime.datetime.strptime(xml_day_one, '%Y-%m-%d')
output = codecs.open('weather-script-preprocess.svg', 'r', encoding='utf-8').read()
output = output.replace('ICON_ONE',icons[0]).replace('ICON_TWO',icons[1]).replace('ICON_THREE',icons[2]).replace('ICON_FOUR',icons[3])
output = output.replace('HIGH_ONE',str(highs[0])).replace('HIGH_TWO',str(highs[1])).replace('HIGH_THREE',str(highs[2])).replace('HIGH_FOUR',str(highs[3]))
output = output.replace('LOW_ONE',str(lows[0])).replace('LOW_TWO',str(lows[1])).replace('LOW_THREE',str(lows[2])).replace('LOW_FOUR',str(lows[3]))
one_day = datetime.timedelta(days=1)
days_of_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
output = output.replace('DAY_THREE',days_of_week[(day_one + 2*one_day).weekday()]).replace('DAY_FOUR',days_of_week[(day_one + 3*one_day).weekday()])
codecs.open('weather-script-output.svg', 'w', encoding='utf-8').write(output)
#eg.msgbox("Working")
from subprocess import call
call(["inkscape.exe", "weather-script-output.svg", "--export-png=file.gif.png" ])
title = 'Done'
msg = 'Press Ok to get weather'
eg.msgbox(msg, title)
from PIL import Image, ImageTk
from Tkinter import Tk, Label, BOTH
from ttk import Frame, Style
from Tkinter import *
import Image
import PIL
filename = "file.gif.png"
basewidth = 300
img = Image.open(filename)
wpercent = (basewidth / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
img.save('resize.jpg')
import urllib2
import json
request = urllib2.urlopen("http://api.aerisapi.com/observations/Nevada,IA?client_id=QD2ToJ2o7MKAX47vrBcsC&client_secret=0968kxX4DWybMkA9GksQREwBlBlC4njZw9jQNqdO")
response = request.read().decode("utf-8")
json = json.loads(response)
ob = json['response']['ob']
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def update_timeText():
current = time.strftime("%H:%M:%S")
timeText.configure(text=current)
root.after(1000, update_timeText)
def initUI(self):
self.parent.title("Weather")
self.pack(fill=BOTH, expand=1)
Style().configure("TFrame", background="#333")
bard = Image.open("resize.jpg")
bardejov = ImageTk.PhotoImage(bard)
label1 = Label(self, image=bardejov)
if ob['tempF'] >= '80':
label2 = Label(self, text="The current temp in Nevada is %r" % (ob['tempF']), bg="white")
if ob['tempF'] <= '90':
label2 = Label(self, text="With the heat index the temp in Nevada is %r" % (ob['heatindexF']), bg="white")
#label3 = Label(self, text=if ob(['tempF']) =<90
label1.image = bardejov
label1.place(x=0, y=0)
label2.place(x=20, y=196)
def main():
root = Tk()
root.geometry("295x400")
app = Example(root)
root.mainloop()
if __name__ == '__main__':
main()