Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import psycopg2.extras
- import time
- import json
- from psycopg2.extensions import AsIs
- from methods_grabber import get_district, get_city,\
- get_street, get_house, get_postcode
- time_start = time.time()
- conn = psycopg2.connect("dbname=gym user=gyms")
- def get_data():
- global conn
- dict_cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
- dict_cur.execute("""
- SELECT *
- FROM dblgis_grabber_firm;
- """
- )
- res = dict_cur.fetchall()
- dict_cur.close()
- return res
- def update_data(id_row, row, data):
- global conn
- cur = conn.cursor()
- query = "UPDATE dblgis_grabber_firm SET %s=%s WHERE id=%s;"
- cur.execute(query, (AsIs(row), data, id_row))
- conn.commit()
- cur.close()
- return cur.rowcount
- data = get_data()
- def get_from_contacts(contact_group, row_name, representation, id_row):
- result = []
- for contacts in contact_group:
- for contact in contacts.get('contacts'):
- if contact.get('type') == representation:
- result.append(unicode(contact.get('text')))
- result = '; '.join(result)
- update_data(id_row, row_name, result)
- def fill_data(instance):
- longitude = instance.get('longitude')
- latitude = instance.get('latitude')
- id_row = instance.get('id')
- contact_group = json.loads(instance.get('all_data', {})).get('contact_groups', {})
- if not instance.get('street'):
- street = get_street(longitude, latitude)
- if street:
- update_data(id_row, 'street', street)
- if not instance.get('street_number'):
- street_number = get_house(longitude, latitude)
- if street_number:
- update_data(id_row, 'street_number', street_number)
- if not instance.get('postcode'):
- postcode = get_postcode(longitude, latitude)
- if postcode:
- update_data(id_row, 'postcode', postcode)
- get_from_contacts(contact_group, 'phone_number', 'phone', id_row)
- get_from_contacts(contact_group, 'site', 'website', id_row)
- get_from_contacts(contact_group, 'email', 'email', id_row)
- for d in data:
- fill_data(d)
- conn.close()
- print 'Timeit %s' % (time.time() - time_start)
Advertisement
Add Comment
Please, Sign In to add comment