almost mostly done

This commit is contained in:
Jacob 2018-07-19 22:23:50 +02:00
parent 673a6f9db1
commit 823f610a4b

116
sync.py
View File

@ -1,12 +1,14 @@
import csv import csv
from datetime import datetime
HubSpotOwners = { "owner": [] } HubSpotOwners = { "owner": [] }
HubSpotUsers = { "user": [] } HubSpotUsers = { "user": [] }
iPaperUsers = { "user": [] } iPaperUsers = { "user": [] }
IntercomUsers = { "user": [] }
OldSyncData = { 'user': [] }
# read in data, built data structures # read in data, built data structures
def fill_iPaperUsers(email, license, status, lastlogin, flipbooks, limit, session): def fill_iPaperUsers(email, license, status, lastlogin, flipbooks, limit, session):
user = {} user = {}
user["email"] = email user["email"] = email
@ -39,20 +41,29 @@ def fill_hubSpotUsers(email, ownerId):
def add_hubSpotUser(user): def add_hubSpotUser(user):
HubSpotUsers["user"].append(user) HubSpotUsers["user"].append(user)
def fill_hubSpotUsers(email, ownerId): def fill_intercomUsers(email, lastWebVisit):
user = {} user = {}
user["Email"] = email user["Email"] = email
user["OwnerId"] = ownerId user["LastWebVisit"] = lastWebVisit
add_hubSpotUser(user) add_intercomUser(user)
def add_hubSpotUser(user): def add_intercomUser(user):
HubSpotUsers["user"].append(user) IntercomUsers["user"].append(user)
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)
### write hubspot ### write hubspot
def write_hubspot_csv(): def write_hubspot_csv():
with open("test.csv", "w") as csvfile: with open("newestSyncro.csv", "w") as csvfile:
filewriter = csv.writer(csvfile, delimiter=";",quotechar="|",quoting=csv.QUOTE_MINIMAL) filewriter = csv.writer(csvfile, delimiter=";",quotechar="|",quoting=csv.QUOTE_MINIMAL)
filewriter.writerow([ filewriter.writerow([
@ -67,34 +78,69 @@ def write_hubspot_csv():
'LastSyncDate', 'LastSyncDate',
'LastSeen']) 'LastSeen'])
hubSpotUsers = list(HubSpotUsers.items())[0][1]
intercomUsers = list(IntercomUsers.items())[0][1]
oldSyncData = list(OldSyncData.items())[0][1]
for key, value in iPaperUsers.items(): for key, value in iPaperUsers.items():
for i in value: for i in value:
email = i["email"] email = i["email"]
ownerId='' ownerId=getOwnerIdForeignKey(hubSpotUsers, email)
for key, value in HubSpotUsers.items():
for j in value:
if j['Email'] == email:
ownerId = j['OwnerId']
licenseName = i['LicenseName'] licenseName = i['LicenseName']
status = i['Status'] status = i['Status']
flipbooks = i['Flipbooks'] flipbooks = i['Flipbooks']
limit = i['FlipbookLimit'] limit = i['FlipbookLimit']
filewriter.writerow([email,ownerId,licenseName,status,flipbooks,limit, if flipbooks > limit:
#i['...'], exceed = 'true'
#i['...'], else:
#i['...'], exceed = 'false'
i['LastLoginDate']
])
# 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.
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])
def getLastSyncDate(users, email):
for v in users:
if v['Email'] == email:
if v['LastSync'] == ''
return datetime.date.today().strftime("%d-%m-%Y %H:%M:%S")
else:
return v['LastSync']
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
def read_iPaper(): def read_iPaper():
with open("iPaperUsers.csv", "r") as fw: with open("iPaperUsers.csv", "r") as fw:
reader = csv.reader(fw, delimiter=";") reader = csv.reader(fw, delimiter=";")
rowNr = 0 rowNr = 0
for row in reader: for row in reader:
if rowNr >= 0: if rowNr > 0:
fill_iPaperUsers(row[0],row[1],row[2],row[3],row[4],row[5],row[6]) fill_iPaperUsers(row[0],row[1],row[2],row[3],row[4],row[5],row[6])
rowNr += 1 rowNr += 1
@ -107,8 +153,8 @@ def read_hubSpotOwner():
fill_hubSpotOwners(row[0],row[1]) fill_hubSpotOwners(row[0],row[1])
rowNr += 1 rowNr += 1
def read_hubSpotUser(): def read_hubspotUsers():
with open("HubSpotUsers.csv", "r") as fw: with open("HubspotUsers.csv", "r") as fw:
reader = csv.reader(fw, delimiter=";") reader = csv.reader(fw, delimiter=";")
rowNr = 0 rowNr = 0
for row in reader: for row in reader:
@ -116,8 +162,30 @@ def read_hubSpotUser():
fill_hubSpotUsers(row[0],row[1]) fill_hubSpotUsers(row[0],row[1])
rowNr += 1 rowNr += 1
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
def init(): def init():
read_hubSpotUser() read_oldSyncro()
read_hubspotUsers()
read_intercomUsers()
read_iPaper() read_iPaper()
write_hubspot_csv() write_hubspot_csv()