import urllib from xml.dom import minidom from xml.dom.minidom import Document, parse xmlns = 'http://www.w3.org/1999/xhtml' # paikkakuntien nimet locationslist = "http://fil.nrk.no/yr/viktigestader/verda.txt" # oletus paikkakunta säätiedotusta varten defaultlocation = "http://www.yr.no/place/Finland/Western_Finland/Jyväskylä/forecast.xml" # flickr feed osoite flickrfeed = "http://api.flickr.com/services/feeds/geo/?tags=nature&lang=en-us&format=rss_200" # google chartin alustus, koko, tyyppi, asteikko yms gquerybegining = "http://chart.apis.google.com/chart?cht=lc&chco=ff0000,000000&chs=600x400&chds=-35,35&chxr=1,-35,35&chxt=x,y,t" def index(req): form = req.form if form.has_key("getweathermenu"): return getWeatherMenu(req) if form.has_key("getweather"): return getWeather(req,form["getweather"]) if form.has_key("getflickr"): return getFlickr(req) def getFlickr(req): """ hakee flickr palvelusta uusimmat kuvat georss muodossa ja muotoilee ne html muotoon""" req.content_type = "text/xml" doc = Document() div = doc.createElementNS(xmlns,"div") div.setAttribute("xmlns",xmlns) div.setAttribute("class", "flickrright") div.setAttribute("id", "flickr") pics = parse(urllib.urlopen(flickrfeed)) nodes = pics.getElementsByTagName("item") # käydään kuvat läpi for node in nodes: sdiv = doc.createElement("div") sdiv.setAttribute("class", "flickr") img = doc.createElement("img") a = doc.createElement('a') # kuvaa koskevat tiedot for child in node.childNodes: if not child.nodeName == "#text": if child.nodeName == "media:title": # kaikilla kuvilla ei ole titleä try: title = child.childNodes[0].nodeValue except: title = "(no title)" img.setAttribute("alt",title) img.setAttribute("name",title) p = doc.createElement('p') p.setAttribute("class","picdes") txt = doc.createTextNode(title) p.appendChild(txt) # osoite thumbnailille, ei tarvita isoa kuvaa if child.nodeName == "media:thumbnail": img.setAttribute("src",child.getAttribute("url")) a.setAttribute("href",child.getAttribute("url")) # koordinaatit if child.nodeName == "georss:point": a.setAttribute("name",child.childNodes[0].nodeValue.replace(" ","|")) # kuvan ottaja if child.nodeName == "media:credit": author = doc.createElement('p') author.setAttribute("class","picauthor") author.appendChild(doc.createTextNode(child.childNodes[0].nodeValue)) # päivämäärä kuvalle if child.nodeName == "dc:date.Taken": pictaken = doc.createElement("p") pictaken.setAttribute("class","pictaken") time = child.childNodes[0].nodeValue.split("T") splitti = time[1].split("-") pictaken.appendChild(doc.createTextNode(time[0]+" "+splitti[0])) # kootaan palat tmpp = doc.createElement('p') a.appendChild(img) tmpp.appendChild(a) sdiv.appendChild(tmpp) sdiv.appendChild(p) sdiv.appendChild(pictaken) sdiv.appendChild(author) div.appendChild(sdiv) doc.appendChild(div) return doc.toprettyxml def getWeatherMenu(req): """ luo ja palauttaa lomakkeen paikkakunnan vaihtamista varten ja hakee oletuspaikkakunnan säätiedotuksen""" req.content_type = "text/xml" doc = Document() div = doc.createElementNS(xmlns,"div") div.setAttribute("xmlns",xmlns) div.setAttribute("class", "in") div.setAttribute("id", "weather") # kaupungin vaihtamiseen käytettävälomake form = doc.createElement("form") form.appendChild(getLocations(req)) div.appendChild(form) wdiv = weatherData(doc,defaultlocation) div.appendChild(wdiv) doc.appendChild(div) return doc.toprettyxml def weatherData(doc,location): """ hakee säätiedot annetulle paikkakunnalle yr.no:n xml tiedostosta ja luo siitä taulukon sekä viiva kaavion lämpötilasta""" # säätiedot wdiv = doc.createElement("div") wdiv.setAttribute("id", "weatherdiv") weather = parse(urllib.urlopen(location)) nodes = weather.getElementsByTagName("time") updatetime = weather.getElementsByTagName("lastupdate") pupdatetd = doc.createElement('p') pupdatetd.appendChild(doc.createTextNode(str("Updated ")+str(updatetime[0].childNodes[0].nodeValue).replace("T"," "))) i = 0 # google charttia varten muuttujia values = "" labelbottom = "" labeltop = "" # käydään säätiedot läpi table = doc.createElement("table") tr = doc.createElement("tr") # taulukon alkuun päivä ja aika td = doc.createElement("th") td.appendChild(doc.createTextNode("From")) tr.appendChild(td) td = doc.createElement("th") td.appendChild(doc.createTextNode("To")) tr.appendChild(td) td = doc.createElement("th") td.appendChild(doc.createTextNode("Wind Direction")) tr.appendChild(td) td = doc.createElement("th") td.appendChild(doc.createTextNode("Wind Speed m/s")) tr.appendChild(td) td = doc.createElement("th") td.appendChild(doc.createTextNode("Temp. 'C")) tr.appendChild(td) td = doc.createElement("th") td.appendChild(doc.createTextNode("Air pressure hPa")) tr.appendChild(td) table.appendChild(tr) for node in nodes: if i == 0: values += str(int(node.childNodes[13].getAttribute("value"))) i = i + 1 else: values += ","+str(int(node.childNodes[13].getAttribute("value"))) tr = doc.createElement("tr") # gcharrtia varten apu muuttujia start = str(node.getAttribute("from")).split("T") end = str(node.getAttribute("to")).split("T") tmpday = start[0].split("-") tmptime = start[1].split(":") labeltop += tmpday[2]+"|" labelbottom += tmptime[0]+"|" endday = end[0].split("-") endtime = end[1].split(":") # taulukon alkuun päivä ja aika td = doc.createElement("th") td.appendChild(doc.createTextNode("%s.%s %s %s:%s"%(tmpday[2],tmpday[1],tmpday[0],tmptime[0],tmptime[1]))) tr.appendChild(td) td = doc.createElement("th") td.appendChild(doc.createTextNode("%s.%s %s %s:%s"%(endday[2],endday[1],endday[0],endtime[0],endtime[1]))) tr.appendChild(td) # säätiedot for child in node.childNodes: td2 = None if child.nodeName == "temperature": td2 = doc.createElement("td") txt = doc.createTextNode(str(child.getAttribute("value"))) td2.appendChild(txt) if child.nodeName == "windSpeed": td2 = doc.createElement("td") txt = doc.createTextNode(str(child.getAttribute("mps"))) td2.appendChild(txt) if child.nodeName == "windDirection": td2 = doc.createElement("td") txt = doc.createTextNode(str(child.getAttribute("name"))) td2.appendChild(txt) if child.nodeName == "pressure": td2 = doc.createElement("td") txt = doc.createTextNode(str(child.getAttribute("value"))) td2.appendChild(txt) if not td2 == None: tr.appendChild(td2) table.appendChild(tr) wdiv.appendChild(pupdatetd) wdiv.appendChild(table) values += "|0,0" # google chartti gchart = gquerybegining+"&chxl=0:|%s 2:|%s&chg=4.5,5&chd=t:%s"%(labelbottom,labeltop,values) # kasataan documentti a = doc.createElement("a") a.setAttribute("href",gchart) img = doc.createElement("img") img.setAttribute("src",gchart) img.setAttribute("width","100%") a.appendChild(img) pimg = doc.createElement('p') pimg.appendChild(a) wdiv.appendChild(pimg) p = doc.createElement('p') txt = doc.createTextNode(str(weather.getElementsByTagName("credit")[0].childNodes[5].getAttribute("text"))) p.appendChild(txt) wdiv.appendChild(p) return wdiv def getWeather(req,xmllocation): """ hakee annetun paikkakunnan säätiedot """ req.content_type = "text/xml" doc = Document() wdiv = weatherData(doc,xmllocation) wdiv.setAttribute("id", "weatherdiv") wdiv.setAttribute("xmlns",xmlns) return wdiv.toprettyxml def getLocations(req): """ Hakee suomalaiset kaupungit fil.nrk.no säälistasta""" doc = Document() p = doc.createElement('p') sel = doc.createElementNS(xmlns,"select") sel.setAttribute("id", "locations") file = urllib.urlopen(locationslist) data = [] for line in file: attributes = line.split(" ") if attributes[0] == "FI": # | nimi | xml | lat | long data.append([attributes[3],attributes[17],attributes[12],attributes[13]]) data.sort() for city,xml,lat,long in data: opt = doc.createElementNS(xmlns,"option") opt.setAttribute('id',lat+"|"+long) opt.setAttribute('name',str(xml)) if city == "Jyväskylä": opt.setAttribute("selected","selected") opt.appendChild(doc.createTextNode(city)) sel.appendChild(opt) p.appendChild(sel) return p