Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.07 KB | None | 0 0
  1. import urllib.request, json
  2. from bs4 import BeautifulSoup as soup
  3. import pymysql.cursors
  4.  
  5. cnx = pymysql.connect(user='root',
  6. password='Elisabeth95',
  7. host='127.0.0.1',
  8. database='toiletdb')
  9. cursor = cnx.cursor()
  10.  
  11. postnr_dict = {}
  12. txt_dict = {}
  13. jsoninde_dict = {}
  14. jsonude_dict = {}
  15.  
  16. def get_postnr_aarhus(url):
  17. response = urllib.request.urlopen(url)
  18. read = response.read().decode("utf-8")
  19. info = json.loads(read)
  20. data = info
  21.  
  22. for postnr in data:
  23. postnr_dict.update({postnr["navn"]: postnr["nr"]})
  24. print(postnr_dict)
  25.  
  26. def get_jsondata_midtby(url):
  27. response = urllib.request.urlopen(url)
  28. read = response.read().decode("latin-1")
  29. info = json.loads(read)
  30. data = info["features"]
  31.  
  32. for toilets in data:
  33. jsoninde_dict.update({"Midtby " + str(toilets['properties']['JCD_nr#'][4:7]) : {
  34. "Status:" : toilets['properties']['Status'],
  35. "Adresse:" : toilets['properties']['Adresse'],
  36. "Postnr:" : int(toilets['properties']['Postnr#'])}})
  37.  
  38. print(jsoninde_dict)
  39.  
  40. def get_jsondata_parkskov(url):
  41. response = urllib.request.urlopen(url)
  42. read = response.read().decode("latin-1")
  43. info = json.loads(read)
  44. data = info["features"]
  45.  
  46. for toilets in data:
  47. jsonude_dict.update({toilets['properties']['Navn'] : {
  48. "Status:" : toilets['properties']['Sæson'],
  49. "Adresse:" : toilets['properties']['Adresse'],
  50. "Beskrivelse:" : toilets['properties']['beskrivelse']}})
  51.  
  52. print(jsonude_dict)
  53.  
  54. def read_review_txt(name, mode='r'):
  55. text_file = open(name, mode)
  56. lines = text_file.readlines()
  57. counter = 1001
  58.  
  59. for line in lines:
  60. x = line.split(' - ')
  61. if x[2][4:] == "\n":
  62. x.append("Ingen beskrivelse\n")
  63. txt_dict.update({"Review " + str(counter)[1:] : {
  64. "Rating:" : line[0],
  65. "Adresse:" : x[1].replace('Ã¥', 'å').replace('ø', "ø").replace('æ', "æ"),
  66. "Postnr:" : x[2],
  67. "Beskrivelse:" : x[3].replace('Ã¥', 'å').replace('ø', "ø").replace('æ', "æ")}})
  68. counter += 1
  69.  
  70. print(txt_dict)
  71.  
  72. def toilet_area_db(object):
  73. ## Opretter Company med navn og antal timer
  74. query = ('INSERT INTO toiletinfo (catagory) VALUES (%s);')
  75. cursor.execute(query, ("Midtby"))
  76. cursor.execute(query, ("Park og Skov"))
  77. cursor.execute(query, ("Brugertilføjet"))
  78. cursor.execute(query, ("FastFood"))
  79.  
  80. def århus_zipcode(object):
  81. for i in postnr_dict:
  82. query = ('INSERT INTO zipcodedb (zipcode, cityname) VALUES (%s, %s)')
  83. zipcode = postnr_dict[i]
  84. city = i
  85. cursor.execute(query, (zipcode, city))
  86.  
  87. def toilet_data_db(object):
  88. for i in jsoninde_dict:
  89. query = ('INSERT INTO toiletdata (toilet_name,adress,zipcode, status, catagory_id) VALUES (%s, %s, %s, %s, %s);')
  90. name = i
  91. adress = jsoninde_dict[i]["Adresse:"]
  92. zipcode = jsoninde_dict[i]['Postnr:']
  93. if str(zipcode) in postnr_dict.values():
  94. zipcode = jsoninde_dict[i]['Postnr:']
  95. else:
  96. continue
  97. status = jsoninde_dict[i]['Status:']
  98. cursor.execute(query, (name,adress,zipcode,status, 1))
  99.  
  100.  
  101. for i in jsonude_dict:
  102. query = ('INSERT INTO toiletdata (toilet_name,adress,status, catagory_id) VALUES (%s, %s, %s, %s);')
  103. name = i
  104. adress = jsonude_dict[i]["Adresse:"]
  105. status = jsonude_dict[i]['Status:']
  106. description = jsonude_dict[i]['Beskrivelse:']
  107. cursor.execute(query, (name,adress,status,2))
  108.  
  109.  
  110.  
  111. for i in txt_dict:
  112. query = ('INSERT INTO toiletdata (toilet_name,adress, zipcode, catagory_id) VALUES (%s, %s, %s, %s);')
  113. name = i
  114. adress = txt_dict[i]["Adresse:"]
  115. zipcode = int(txt_dict[i]['Postnr:'])
  116. if str(zipcode) in postnr_dict.values():
  117. zipcode = txt_dict[i]['Postnr:']
  118. else:
  119. continue
  120. cursor.execute(query, (name,adress,zipcode,3))
  121. cnx.commit()
  122.  
  123. def ratedescripe(object):
  124. for i in txt_dict:
  125. query1 = ('INSERT INTO ratingdb (toilet_id, rating, sum_ratings, times_rated) VALUES (%s, %s, %s, %s);')
  126. query2 = ('SELECT toilet_id FROM toiletdata WHERE adress = %s;')
  127. query3 = ('SET FOREIGN_KEY_CHECKS = 0;')
  128. zipcode = int(txt_dict[i]['Postnr:'])
  129. if str(zipcode) in postnr_dict.values():
  130. adress1 = txt_dict[i]["Adresse:"]
  131. rating = int(txt_dict[i]['Rating:'])
  132. count = 1
  133. rate = rating/1
  134. cursor.execute(query3)
  135. cursor.execute(query2, (adress1))
  136. test = cursor.fetchone()
  137. cursor.execute(query1, (test, rate, rating, count))
  138. else:
  139. continue
  140.  
  141. for i in jsonude_dict:
  142. query1 = ('INSERT INTO descriptiondb (toilet_id, suggest_description) VALUES (%s, %s);')
  143. query2 = ('SELECT toilet_id FROM toiletdata WHERE adress = %s;')
  144. query3 = ('SET FOREIGN_KEY_CHECKS = 0;')
  145. adress1 = jsonude_dict[i]["Adresse:"]
  146. description = jsonude_dict[i]["Beskrivelse:"]
  147. cursor.execute(query3)
  148. cursor.execute(query2, (adress1))
  149. test = cursor.fetchone()
  150. cursor.execute(query1, (test, description))
  151.  
  152. for i in txt_dict:
  153. query1 = ('INSERT INTO descriptiondb (toilet_id, suggest_description) VALUES (%s, %s);')
  154. query2 = ('SELECT toilet_id FROM toiletdata WHERE adress = %s;')
  155. query3 = ('SET FOREIGN_KEY_CHECKS = 0;')
  156. zipcode = int(txt_dict[i]['Postnr:'])
  157. if str(zipcode) in postnr_dict.values():
  158. adress1 = txt_dict[i]["Adresse:"]
  159. description = txt_dict[i]['Beskrivelse:']
  160. cursor.execute(query3)
  161. cursor.execute(query2, (adress1))
  162. test = cursor.fetchone()
  163. cursor.execute(query1, (test, description))
  164. else:
  165. continue
  166.  
  167. def reset_database(object):
  168. reset = ('SET FOREIGN_KEY_CHECKS = 0; truncate descriptiondb; truncate ratingdb; truncate zipcodedb; truncate toiletdata; truncate toiletinfo; SET FOREIGN_KEY_CHECKS = 1; commit')
  169. cursor.execute(reset)
  170.  
  171. url = ("http://dawa.aws.dk/postnumre?kommunekode=751")
  172. url1 = ("https://portal.opendata.dk/dataset/cf1c6b95-3d1f-4cb7-a67b-e93e2de5299c/resource/065550c2-44b0-41db-94ce-009c47b0ba2b/download/bytoiletterwgs84.json")
  173. url2 = ("https://portal.opendata.dk/dataset/c637921c-6ad8-4c1a-bd8d-9dac8605c2d4/resource/1bd17c11-f20f-4db6-ab2a-a56baaf62980/download/toiletwgs84.json")
  174. txt = ("Constanze-filerne2.txt")
  175. get_postnr_aarhus(url)
  176. get_jsondata_midtby(url1)
  177. get_jsondata_parkskov(url2)
  178. read_review_txt(txt)
  179.  
  180. reset_database(object)
  181. århus_zipcode(object)
  182. toilet_area_db(object)
  183. toilet_data_db(object)
  184. ratedescripe(object)
  185.  
  186. cnx.commit()
  187. cnx.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement