csvThing/sync.py

242 lines
6.9 KiB
Python
Raw Permalink Normal View History

2018-07-19 18:33:31 +00:00
import csv
2018-07-19 20:23:50 +00:00
from datetime import datetime
2018-07-19 18:33:31 +00:00
HubSpotOwners = { "owner": [] }
HubSpotUsers = { "user": [] }
iPaperUsers = { "user": [] }
2018-07-19 20:23:50 +00:00
IntercomUsers = { "user": [] }
2018-07-19 18:33:31 +00:00
2018-07-19 20:23:50 +00:00
OldSyncData = { 'user': [] }
2018-07-19 18:33:31 +00:00
2018-07-19 20:23:50 +00:00
# read in data, built data structures
2018-07-19 18:33:31 +00:00
def fill_iPaperUsers(email, license, status, lastlogin, flipbooks, limit, session):
user = {}
user["email"] = email
user["LicenseName"] = license
user["Status"] = status
user["LastLoginDate"] = lastlogin
user["Flipbooks"] = flipbooks
user["FlipbookLimit"] = limit
user["FlipbookSession"] = session
add_iPaperUser(user)
def add_iPaperUser(user):
iPaperUsers["user"].append(user)
def fill_hubSpotOwners(ownerId, name):
owner = {}
owner["OwnerId"] = ownerId
owner["name"] = name
add_hubSpotOwner(owner)
def add_hubspotOwner(owner):
HubSpotOwners["owner"].append(owner)
def fill_hubSpotUsers(email, ownerId):
user = {}
user["Email"] = email
user["OwnerId"] = ownerId
add_hubSpotUser(user)
def add_hubSpotUser(user):
HubSpotUsers["user"].append(user)
2018-07-19 20:23:50 +00:00
def fill_intercomUsers(email, lastWebVisit):
2018-07-19 18:33:31 +00:00
user = {}
user["Email"] = email
2018-07-19 20:23:50 +00:00
user["LastWebVisit"] = lastWebVisit
add_intercomUser(user)
2018-07-19 18:33:31 +00:00
2018-07-19 20:23:50 +00:00
def add_intercomUser(user):
IntercomUsers["user"].append(user)
2018-07-19 18:33:31 +00:00
2018-07-19 20:23:50 +00:00
def fill_oldSyncData(email, lastSync, totalVisits):
user = {}
user["Email"] = email
user["LastSync"] = lastSync
user["TotalVisits"] = totalVisits
add_oldSyncData(user)
def add_oldSyncData(user):
OldSyncData["user"].append(user)
2018-07-19 18:33:31 +00:00
2018-07-19 20:23:50 +00:00
### write hubspot
2018-07-19 18:33:31 +00:00
def write_hubspot_csv():
2018-07-19 20:23:50 +00:00
with open("newestSyncro.csv", "w") as csvfile:
2018-07-19 18:33:31 +00:00
filewriter = csv.writer(csvfile, delimiter=";",quotechar="|",quoting=csv.QUOTE_MINIMAL)
filewriter.writerow([
'email',
'OwnerId',
'LicenseName',
'LicenseStatus',
'FlipBooks',
'FlipbookLimit',
'Exceed',
'TotalFlipbookVisitors',
'LastSyncDate',
'LastSeen'])
2018-07-19 20:23:50 +00:00
hubSpotUsers = list(HubSpotUsers.items())[0][1]
intercomUsers = list(IntercomUsers.items())[0][1]
2018-07-19 20:34:46 +00:00
oldSyncData = list(OldSyncData.items())[0][1]
for key, value in iPaperUsers.items():
2018-07-19 18:33:31 +00:00
for i in value:
email = i["email"]
2018-07-19 20:23:50 +00:00
ownerId=getOwnerIdForeignKey(hubSpotUsers, email)
2018-07-19 18:33:31 +00:00
licenseName = i['LicenseName']
status = i['Status']
flipbooks = i['Flipbooks']
limit = i['FlipbookLimit']
2018-07-19 20:23:50 +00:00
if flipbooks > limit:
exceed = 'true'
else:
exceed = 'false'
2018-07-19 20:45:38 +00:00
# It doesn't really make sense to write last sync, without also saving the date of this sync
# because if we then sync again next month, then we get the second to last sync, since we
# didn't store today's sync.
2018-07-19 20:23:50 +00:00
lastSyncDate = getLastSyncDate(oldSyncData, email)
totalFlipbookVisitors = i['FlipbookSession']
lastSeen = getLastSeen(intercomUsers, email, i['LastLoginDate'])
filewriter.writerow([email,ownerId,licenseName,status,flipbooks,limit,exceed, totalFlipbookVisitors, lastSyncDate, lastSeen])
2018-07-19 18:33:31 +00:00
2018-07-19 20:23:50 +00:00
2018-07-19 20:45:38 +00:00
### write intercom
def write_intercom():
with open("newestIntercomSyncro.csv", "w") as csvfile:
filewriter = csv.writer(csvfile, delimiter=";",quotechar="|",quoting=csv.QUOTE_MINIMAL)
filewriter.writerow([
'email',
'lastWebVisit'
'LicenseName',
'LicenseStatus',
'HubSpotOwner'
'FlipBooksUsed',
'FlipbookLimit',
'FlipbookAvailable',
'LastSyncDate'
])
intercomUsers = list(IntercomUsers.items())[0][1]
for key, value in iPaperUsers.items():
for i in value:
email = i["email"]
ownerId=getOwnerIdForeignKey(hubSpotUsers, email)
licenseName = i['LicenseName']
# not gonna get around to this, but the idea is to get the foreign id in hubspot users, and then get the matching consultant name in hubspotowners
HubSpotOwner = '...'
status = i['Status']
used = i['Flipbooks']
limit = i['FlipbookLimit']
available = limit - used
lastWebVisit = getLastWebVisit(intercomUsers, email)
lastSyncDate = getLastSyncDate(oldSyncData, email)
#lastWebVisit
filewriter.writerow([email,licenseName,HubSpotOwner,status, used, limit, available, lastSyncDate, lastWebVisit])
2018-07-19 20:23:50 +00:00
def getLastSyncDate(users, email):
2018-07-19 20:34:46 +00:00
for v in users:
if v['Email'] == email:
if not v['LastSync']:
return datetime.date.today().strftime("%d-%m-%Y %H:%M:%S")
else:
return v['LastSync']
2018-07-19 20:23:50 +00:00
def getLastSeen (users, email, date):
for k in users:
if k['Email'] == email:
lastLogin = datetime.strptime(date,"%d-%m-%Y %H:%M:%S" )
lastVisit = datetime.strptime(k['LastWebVisit'], "%d-%m-%Y %H:%M:%S")
if lastLogin < lastVisit:
return lastVisit
else:
return lastLogin
def getOwnerIdForeignKey (users, email):
for j in users:
if j['Email'] == email:
return j['OwnerId']
### read the csv files
2018-07-19 18:33:31 +00:00
def read_iPaper():
with open("iPaperUsers.csv", "r") as fw:
reader = csv.reader(fw, delimiter=";")
rowNr = 0
for row in reader:
2018-07-19 20:23:50 +00:00
if rowNr > 0:
2018-07-19 18:33:31 +00:00
fill_iPaperUsers(row[0],row[1],row[2],row[3],row[4],row[5],row[6])
rowNr += 1
def read_hubSpotOwner():
with open("HubSpotOwners.csv", "r") as fw:
reader = csv.reader(fw, delimiter=";")
rowNr = 0
for row in reader:
if rowNr >= 0:
fill_hubSpotOwners(row[0],row[1])
rowNr += 1
2018-07-19 20:23:50 +00:00
def read_hubspotUsers():
with open("HubspotUsers.csv", "r") as fw:
2018-07-19 18:33:31 +00:00
reader = csv.reader(fw, delimiter=";")
rowNr = 0
for row in reader:
if rowNr >= 0:
fill_hubSpotUsers(row[0],row[1])
rowNr += 1
2018-07-19 20:23:50 +00:00
def read_intercomUsers():
with open("IntercomUsers.csv", "r") as fw:
reader = csv.reader(fw, delimiter=";")
rowNr = 0
for row in reader:
if rowNr >= 0:
fill_intercomUsers(row[0],row[1])
rowNr += 1
def read_oldSyncro():
with open("newestSyncro.csv", "r") as fw:
reader = csv.reader(fw, delimiter=";")
rowNr = 0
for row in reader:
if rowNr >= 0:
fill_oldSyncData(row[0], row[7], row[8])
rowNr += 1
fw.close()
### run the program
2018-07-19 18:33:31 +00:00
def init():
2018-07-19 20:23:50 +00:00
read_oldSyncro()
read_hubspotUsers()
read_intercomUsers()
2018-07-19 18:33:31 +00:00
read_iPaper()
write_hubspot_csv()
init()