Advertisement
Festelo

Untitled

Mar 19th, 2017
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.40 KB | None | 0 0
  1. import requests
  2. import pymysql
  3. import json
  4. from urllib.parse import quote
  5.  
  6. # Подключение к MySQL DB
  7. import time
  8.  
  9. import sys
  10.  
  11. connection = pymysql.connect(host='localhost',
  12.                              user='testuser',
  13.                              password='something',
  14.                              db='mapdb',
  15.                              charset='utf8mb4',
  16.                              cursorclass=pymysql.cursors.DictCursor)
  17. results_count = 100
  18.  
  19. #Название колонок в таблице
  20. tablename = 'places'
  21. name_column = 'name'
  22. location_lat_column = 'lat'
  23. location_lng_column = 'lng'
  24. address_column = 'address'
  25. #Название колонок для расшифрованного адреса, подробнее см. https://developers.google.com/maps/documentation/geocoding/intro#Types
  26. addressEncode_columns = {
  27.         'street_address': 'street_address',
  28.         'route': 'route',
  29.         'intersection': 'intersection',
  30.         'political': 'political',
  31.         'country': 'country',
  32.         'administrative_area_level_1': 'administrative_area_level_1',
  33.         'administrative_area_level_2': 'administrative_area_level_2',
  34.         'administrative_area_level_3': 'administrative_area_level_3',
  35.         'administrative_area_level_4': 'administrative_area_level_4',
  36.         'administrative_area_level_5': 'administrative_area_level_5',
  37.         'locality': 'locality',
  38.         'ward': 'ward',
  39.         'sublocality': 'sublocality',
  40.         'premise': 'premise',
  41.         'subpremise': 'subpremise',
  42.         'natural_feature': 'natural_feature',
  43.         'airport': 'airport',
  44.         'park': 'park',
  45.     }
  46.  
  47. def getAllAddressColumnString():
  48.     list = []
  49.     for k in addressEncode_columns.values():
  50.         list.append('`' + k + '`')
  51.     return ', '.join(list)
  52.  
  53. def getJson(url):
  54.     req = requests.get(url)
  55.     out = req.content.decode()
  56.     return json.loads(out)
  57.  
  58. text = sys.argv[1]
  59. encoded_text = quote(text)
  60. key = "AIzaSyCMjAD1sCZUOXoAleXVEaULAf1q1M5mJgE"
  61. url = f'https://maps.googleapis.com/maps/api/place/textsearch/json?query={encoded_text}&key={key}'
  62. res = getJson(url)
  63.  
  64. pagetoken = res.get('next_page_token')
  65.  
  66. while pagetoken is not None and len(res['results']) <= results_count - 20:
  67.     time.sleep(0.1)
  68.     u = f'https://maps.googleapis.com/maps/api/place/textsearch/json?pagetoken={pagetoken}&key={key}'
  69.     j = getJson(u)
  70.     if j['status'] == 'INVALID_REQUEST':
  71.         continue
  72.     res['results'].extend(j['results'])
  73.     pagetoken = j.get('next_page_token')
  74.  
  75. allAddressColumnString = getAllAddressColumnString()
  76. VERY_NEEDED_VARIABLE = ', '.join(['%s'] * len(addressEncode_columns))
  77. try:
  78.     with connection.cursor() as cursor:
  79.         for o in res['results']:
  80.             u = f"https://maps.googleapis.com/maps/api/geocode/json?address={quote(o['formatted_address'])}&key={key}"
  81.             a = getJson(u)
  82.             address = {}
  83.             if len(a['results']) >= 1:
  84.                 for c in a['results'][0]['address_components']:
  85.                     for t in c['types']:
  86.                         address[t] = c['long_name']
  87.  
  88.             sql = f"INSERT INTO `{tablename}` (`{name_column}`, `{address_column}`, `{location_lat_column}`, " \
  89.                   f"`{location_lng_column}`, {allAddressColumnString}) VALUES (%s, %s, %s, %s, {VERY_NEEDED_VARIABLE}) "
  90.  
  91.             cursor.execute(sql, (o['name'], o['formatted_address'],
  92.                                  o['geometry']['location']['lat'],
  93.                                  o['geometry']['location']['lng'],
  94.                                 address.get('street_address', ''), address.get('route', ''), address.get('intersection', ''), address.get('political', ''),
  95.                                 address.get('country', ''), address.get('administrative_area_level_1', ''), address.get('administrative_area_level_2', ''),
  96.                                 address.get('administrative_area_level_3', ''), address.get('administrative_area_level_4', ''),
  97.                                 address.get('administrative_area_level_5', ''), address.get('locality', ''), address.get('ward', ''), address.get('sublocality', ''),
  98.                                 address.get('premise', ''), address.get('subpremise', ''), address.get('natural_feature', ''), address.get('airport', ''), address.get('park', '')))
  99.     connection.commit()
  100. finally:
  101.     connection.close()
  102.  
  103. print('Founded ' + str(len(res['results'])))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement