Skip to content

Instantly share code, notes, and snippets.

@felipeandradebezerra
Created February 1, 2020 13:26
Show Gist options
  • Save felipeandradebezerra/188e83efd191f069a5fbce6229ec76a1 to your computer and use it in GitHub Desktop.
Save felipeandradebezerra/188e83efd191f069a5fbce6229ec76a1 to your computer and use it in GitHub Desktop.
Read, process, and parse CSV file to upload data to Parse Server
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: Felipe Andrade
# @Date: 01-02-2020 00:00:00
# felipe@i2mobile.com.br
# Lib used to read CSV files
import csv
# Regex lib to allow only numbers
import re
# Geocoding web services to locate the coordinates of addresses
from geopy.geocoders import Nominatim
from geopy.geocoders import GoogleV3
from geopy.geocoders import Bing
# Connection libs to send data to parse server
import io
import json
import urllib2
import httplib
import urllib
# You can use any arbitrary string as your application id and master key.
# These will be used by your clients to authenticate with the Parse Server.
SERVER_URL = 'api.SITE.com'
APP_ID = 'PARSE_APP_ID'
MASTER_APP_KEY = 'MASTER_APP_KEY'
ENDPOINT = '/1/CLASS_NAME'
GOOGLE_APP_KEY = '' # you can generate a new key at https://console.cloud.google.com/apis
BING_APP_KEY = '' # you can generate a new key at https://www.bingmapsportal.com/Application
def create(post_fields, post_headers):
connection = httplib.HTTPSConnection(SERVER_URL, 443)
connection.connect()
connection.request('POST', ENDPOINT, post_fields, post_headers)
results = json.loads(connection.getresponse().read())
print results
geolocator = Nominatim(user_agent="SITE_NAME")
geolocator_google = GoogleV3(GOOGLE_APP_KEY)
geolocator_bing = Bing(BING_APP_KEY)
with open('file_name.csv', 'rb') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL, skipinitialspace=True)
for row in spamreader:
name = row[0]
uf = row[1]
cidade = row[2]
bairro = row[3]
address = row[4]
cep = row[5]
ddd = row[6]
tel = row[7]
tel2 = row[8]
email = row[9]
site = row[10]
# allow only numbers
if len(cep) > 0:
cep = str(int(''.join(filter(str.isdigit, cep))))
if len(tel) > 0:
tel = str(int(''.join(filter(str.isdigit, tel))))
if len(tel2) > 0:
tel2 = str(int(''.join(filter(str.isdigit, tel2))))
#
if len(site) > 7:
if (site.startswith("http")) == False:
site = "http://" + site
short_address = address + "," + cidade + "," + uf
full_address = address + "," + cidade + "," + uf + "," + cep
location = {"__type": "GeoPoint", "latitude": 0.0, "longitude": 0.0}
# Locate the coordinates of addresses with OpenStreetMaps
try:
locationFinder = geolocator.geocode(full_address)
if locationFinder:
location['latitude'] = locationFinder.latitude
location['longitude'] = locationFinder.longitude
except:
print "Error OpenStreetMaps"
# Locate the coordinates of addresses with Google
if location[ 'latitude'] == 0.0:
try:
locationFinder = geolocator_google.geocode(full_address)
if locationFinder:
location['latitude'] = locationFinder.latitude
location['longitude'] = locationFinder.longitude
except:
print "Error Google"
# Locate the coordinates of addresses with Bing
if location[ 'latitude'] == 0.0:
try:
locationFinder = geolocator_bing.geocode(short_address)
if locationFinder:
location['latitude'] = locationFinder.latitude
location['longitude'] = locationFinder.longitude
except:
print "Error Bing"
phones = []
if len(ddd) > 0:
ddd = "(" + ddd + ")"
if len(tel) > 0:
phones.append(ddd + tel)
if len(tel2) > 0:
phones.append(ddd + tel2)
post_fields = json.dumps({
'cidade': cidade,
'estado': uf,
'site': site,
'telefones': phones,
'localizacao': address,
'cep': cep,
'coordenadas': location,
'nome': nome_fantasia,
'email': email
})
post_headers = {
'X-Parse-Application-Id': APP_ID,
'X-Parse-Master-Key': MASTER_APP_KEY,
'Content-Type': 'application/json'
}
create(post_fields, post_headers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment