I'm trying to make this script run every 30 minutes. at the moment it only runs once - a bit confused why it isn't running many more times.
Any idea on where I'm going wrong with my code? Basically this script is taking data from an API, in XML, and then putting it into a csv file.
Trying to use Threading to make it run every so many seconds - running it on pythonanywhere at the moment - and it will only run once. A little bit confused why that would be!
I also have tried using a while loop - put a example of what i've tried near the threading code.
from lxml import etree
import urllib.request
import csv
import threading
#Pickle is not needed
#append to list next
def handleLeg(leg):
# print this leg as text, or save it to file maybe...
text = etree.tostring(leg, pretty_print=True)
# also process individual elements of interest here if we want
tagsOfInterest=["noTrafficTravelTimeInSeconds", "lengthInMeters", "departureTime", "trafficDelayInSeconds"] # whatever
#list to use for data analysis
global data
data = []
#create header dictionary that includes the data to be appended within it. IE, Header = {TrafficDelay[data(0)]...etc
for child in leg:
if 'summary' in child.tag:
for elem in child:
for item in tagsOfInterest:
if item in elem.tag:
data.append(elem.text)
def parseXML(xmlFile):
"""While option
lastTime = time.time() - 600
while time.time() >= lastTime + 600:
lastTime += 600"""
#Parse the xml
threading.Timer(5.0, parseXML).start()
with urllib.request.urlopen("https://api.tomtom.com/routing/1/calculateRoute/-37.79205923474775,145.03010268799338:-37.798883995180496,145.03040309540322:-37.807106781970354,145.02895470253526:-37.80320743019992,145.01021142594075:-37.7999012967757,144.99318476311566:?routeType=shortest&key=xxx&computeTravelTimeFor=all") as fobj:
xml = fobj.read()
root = etree.fromstring(xml)
for child in root:
if 'route' in child.tag:
handleLeg(child)
# Write CSV file
with open('datafile.csv', 'w') as fp:
writer = csv.writer(fp, delimiter=' ')
# writer.writerow(["your", "header", "foo"]) # write header
writer.writerows(data)
"""for elem in child:
if 'leg' in elem.tag:
handleLeg(elem)
"""
if __name__ == "__main__":
parseXML("xmlFile")
with open('datafile.csv', 'r') as fp:
reader = csv.reader(fp, quotechar='"')
# next(reader, None) # skip the headers
data_read = [row for row in reader]
print(data_read)