kAldown

review

Feb 5th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import psycopg2.extras
  4. import time
  5. import json
  6. from psycopg2.extensions import AsIs
  7. from methods_grabber import get_district, get_city,\
  8.                             get_street, get_house, get_postcode
  9. time_start = time.time()
  10.  
  11. conn = psycopg2.connect("dbname=gym user=gyms")
  12.  
  13. def get_data():
  14.  
  15.     global conn
  16.  
  17.     dict_cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  18.     dict_cur.execute("""
  19.                    SELECT *
  20.                    FROM dblgis_grabber_firm;
  21.                    """
  22.                     )
  23.  
  24.     res = dict_cur.fetchall()
  25.     dict_cur.close()
  26.     return res
  27.  
  28. def update_data(id_row, row, data):
  29.     global conn
  30.  
  31.     cur = conn.cursor()
  32.  
  33.     query = "UPDATE dblgis_grabber_firm SET %s=%s WHERE id=%s;"
  34.  
  35.     cur.execute(query, (AsIs(row), data, id_row))
  36.  
  37.     conn.commit()
  38.     cur.close()
  39.     return cur.rowcount
  40.  
  41. data = get_data()
  42.  
  43. def get_from_contacts(contact_group, row_name, representation, id_row):
  44.     result = []
  45.     for contacts in contact_group:
  46.         for contact in contacts.get('contacts'):
  47.             if contact.get('type') == representation:
  48.                 result.append(unicode(contact.get('text')))
  49.     result = '; '.join(result)
  50.     update_data(id_row, row_name, result)
  51.  
  52. def fill_data(instance):
  53.  
  54.     longitude = instance.get('longitude')
  55.     latitude = instance.get('latitude')
  56.  
  57.     id_row = instance.get('id')
  58.  
  59.     contact_group = json.loads(instance.get('all_data', {})).get('contact_groups', {})
  60.  
  61.     if not instance.get('street'):
  62.         street = get_street(longitude, latitude)
  63.         if street:
  64.             update_data(id_row, 'street', street)
  65.        
  66.     if not instance.get('street_number'):
  67.         street_number = get_house(longitude, latitude)
  68.         if street_number:
  69.             update_data(id_row, 'street_number', street_number)
  70.  
  71.     if not instance.get('postcode'):
  72.         postcode = get_postcode(longitude, latitude)
  73.         if postcode:
  74.             update_data(id_row, 'postcode', postcode)
  75.  
  76.     get_from_contacts(contact_group, 'phone_number', 'phone', id_row)
  77.     get_from_contacts(contact_group, 'site', 'website', id_row)
  78.     get_from_contacts(contact_group, 'email', 'email', id_row)
  79.  
  80. for d in data:
  81.     fill_data(d)
  82.  
  83. conn.close()
  84.  
  85. print 'Timeit %s' % (time.time() - time_start)
Advertisement
Add Comment
Please, Sign In to add comment