from mod_python import apache from pysqlite2 import dbapi2 as sqlite from xml.dom.minidom import getDOMImplementation, parse, parseString import os import urllib import sys def loadNewCities(req, con): webFile = urllib.urlopen("http://fil.nrk.no/yr/viktigestader/verda.txt") filename = os.path.join(os.path.dirname(req.filename), 'cities.txt') localFile = open(filename, 'w') localFile.write(webFile.read()) webFile.close() localFile.close() for line in open(filename,'r').readlines(): linecontent = line.split("\t") #following line won't work before You change content_type for text req.write( linecontent[0] + " " + linecontent[3] + " " + linecontent[10]+ " " + linecontent[12]+ " " + linecontent[13]+ " " + linecontent[17] + "
") return 0 #******************************************************************** #************************Main function******************************* #**************This function returns list of cities****************** #******************************************************************** def index(req): con = sqlite.connect( os.path.join(os.path.dirname(req.filename), 'cities')) con.text_factory = str con.row_factory = sqlite.Row req.content_type= 'Content-type: text/html; charset=utf-8' #header('Content-type: text/html; charset=utf-8');header('Content-type: text/html; charset=utf-8'); impl = getDOMImplementation() doc = impl.createDocument("http://www.w3.org/1999/xhtml", "cities", None) doc.documentElement.setAttribute("xmlns", "http://www.w3.org/1999/xhtml") #Do check if file is modified after previous db update. If newer available call loadNewCities #This could be commented because database is updated on get_county.php loadNewCities(req,con) return cur = con.cursor() root = doc.createElement("root") doc.documentElement.appendChild(root) try: cur.execute(""" SELECT ID, Country, CityName, Lat, Lon, Weatherlink FROM cities WHERE Country = :cty ORDER BY CityName ASC """, {"cty":country}) except: returnData = "Tulipa virhe %s" % sys.exc_info()[0] for rivi in cur: # content = ["Jyväskylä", "Tampere", "Helsinki"] # lon = ["25.73333", "23.78712", "24.93417"] # lat = ["62.23333", "61.49911", "60.17556"] # weatherLink = ["http://www.yr.no/place/Finland/Western_Finland/Jyväskylä/forecast.xml", "http://www.yr.no/place/Finland/Häme/Tampere/forecast.xml", "http://www.yr.no/place/Finland/Southern_Finland/Helsinki/forecast.xml"] # root = doc.createElement("root") # doc.documentElement.appendChild(root) # for index in range(len(content)): tr = doc.createElement("city") td = doc.createElement("cityname") tr.appendChild(td) #td.appendChild( doc.createTextNode( content[index].decode("ISO-8859-1") ) ); td.appendChild( doc.createTextNode( rivi['CityName'].decode("UTF-8") ) ); #req.write( rivi['CityName']) td = doc.createElement("lon") tr.appendChild(td) #td.appendChild( doc.createTextNode( lon[index].decode("ISO-8859-1") ) ) td.appendChild( doc.createTextNode( rivi['Lon'].decode("UTF-8") ) ) #req.write( rivi['Lon']) td = doc.createElement("lat") tr.appendChild(td) #td.appendChild( doc.createTextNode( lat[index].decode("ISO-8859-1") ) ) td.appendChild( doc.createTextNode( rivi['Lat'].decode("UTF-8") ) ) #req.write( rivi['Lat']) td = doc.createElement("weatherLink") tr.appendChild(td) #td.appendChild( doc.createTextNode( weatherLink[index].decode("ISO-8859-1") ) ) td.appendChild( doc.createTextNode( rivi['WeatherLink'].decode("UTF-8") ) ) #req.write( rivi['WeatherLink']) root.appendChild(tr) req.write( doc.toxml("UTF-8") )