Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. # Name = Erik V.
  2. # Student number = 221122
  3. import xml.etree.ElementTree as ET
  4. import urllib.request
  5. from time import sleep
  6. from threading import Thread
  7. count = 0
  8.  
  9. def getXmlTree(url):
  10. """Will return the XML tree made with the url given"""
  11.  
  12. # Getting the data from the url
  13. with urllib.request.urlopen(url) as url:
  14.  
  15. data = url.read()
  16.  
  17. # Making a tree with the data
  18. tree = ET.fromstring(data)
  19. return tree
  20.  
  21. def getXmlInfoInDic():
  22. """Will return the info at the id in a dictionary"""
  23.  
  24. # Leeuwarden = 6270 Groningen 6280
  25. # Getting the XML tree from the url
  26. tree = getXmlTree("https://data.buienradar.nl/1.0/feed/xml")
  27.  
  28. # Getting the info for the current ID (leeuwarden)
  29. node = tree.findall('''.//*[@id='6270']''')[0]
  30.  
  31. # Making a dictionary to store the data in
  32. weatherDic = {}
  33.  
  34. for element in node.iter():
  35.  
  36. weatherDic.update({element.tag : element.text})
  37.  
  38. return weatherDic
  39.  
  40. def saveData(weatherDic):
  41. """Will save the data from the dictionary to the file"""
  42. global count
  43.  
  44. filename = "weatherDataCollection.txt"
  45.  
  46. file = open(filename, "a+")
  47.  
  48. for key, value in weatherDic.items():
  49.  
  50. file.write("{} : {}\n".format(key, value))
  51.  
  52. file.write("\n")
  53.  
  54. count += 1
  55. print("Weather data collection number {} completed".format(count))
  56.  
  57. def main():
  58.  
  59. # Saving the data every hour
  60. while True:
  61.  
  62. # Getting a dictionary filled with the weather data
  63. weatherDic = getXmlInfoInDic()
  64.  
  65. # Saving the data to the file
  66. saveData(weatherDic)
  67.  
  68. # Letting the program sleep for an hour (3600s) before repeating
  69. sleep(1800)
  70.  
  71. if (__name__ == "__main__"):
  72.  
  73. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement