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
from datetime import datetime
HubSpotOwners = { "owner": [] }
HubSpotUsers = { "user": [] }
iPaperUsers = { "user": [] }
IntercomUsers = { "user": [] }
OldSyncData = { 'user': [] }
# read in data, built data structures
def fill_iPaperUsers(email, license, status, lastlogin, flipbooks, limit, session):
user = {}
user["email"] = email
@ -39,20 +41,29 @@ def fill_hubSpotUsers(email, ownerId):
def add_hubSpotUser(user):
HubSpotUsers["user"].append(user)
def fill_hubSpotUsers(email, ownerId):
def fill_intercomUsers(email, lastWebVisit):
user = {}
user["Email"] = email
user["OwnerId"] = ownerId
add_hubSpotUser(user)
user["LastWebVisit"] = lastWebVisit
add_intercomUser(user)
def add_hubSpotUser(user):
HubSpotUsers["user"].append(user)
def add_intercomUser(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
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.writerow([
@ -67,34 +78,69 @@ def write_hubspot_csv():
'LastSyncDate',
'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 i in value:
email = i["email"]
ownerId=''
for key, value in HubSpotUsers.items():
for j in value:
if j['Email'] == email:
ownerId = j['OwnerId']
ownerId=getOwnerIdForeignKey(hubSpotUsers, email)
licenseName = i['LicenseName']
status = i['Status']
flipbooks = i['Flipbooks']
limit = i['FlipbookLimit']
filewriter.writerow([email,ownerId,licenseName,status,flipbooks,limit,
#i['...'],
#i['...'],
#i['...'],
i['LastLoginDate']
])
if flipbooks > limit:
exceed = 'true'
else:
exceed = 'false'
# 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():
with open("iPaperUsers.csv", "r") as fw:
reader = csv.reader(fw, delimiter=";")
rowNr = 0
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])
rowNr += 1
@ -107,8 +153,8 @@ def read_hubSpotOwner():
fill_hubSpotOwners(row[0],row[1])
rowNr += 1
def read_hubSpotUser():
with open("HubSpotUsers.csv", "r") as fw:
def read_hubspotUsers():
with open("HubspotUsers.csv", "r") as fw:
reader = csv.reader(fw, delimiter=";")
rowNr = 0
for row in reader:
@ -116,8 +162,30 @@ def read_hubSpotUser():
fill_hubSpotUsers(row[0],row[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():
read_hubSpotUser()
read_oldSyncro()
read_hubspotUsers()
read_intercomUsers()
read_iPaper()
write_hubspot_csv()