csvThing/sync.py

198 lines
5.5 KiB
Python

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
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)
def fill_intercomUsers(email, lastWebVisit):
user = {}
user["Email"] = email
user["LastWebVisit"] = lastWebVisit
add_intercomUser(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("newestSyncro.csv", "w") as csvfile:
filewriter = csv.writer(csvfile, delimiter=";",quotechar="|",quoting=csv.QUOTE_MINIMAL)
filewriter.writerow([
'email',
'OwnerId',
'LicenseName',
'LicenseStatus',
'FlipBooks',
'FlipbookLimit',
'Exceed',
'TotalFlipbookVisitors',
'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=getOwnerIdForeignKey(hubSpotUsers, email)
licenseName = i['LicenseName']
status = i['Status']
flipbooks = i['Flipbooks']
limit = i['FlipbookLimit']
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:
print('somthinw')
if v['Email'] == email:
if not v['LastSync']:
print('hello')
return datetime.date.today().strftime("%d-%m-%Y %H:%M:%S")
else:
print('nay')
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:
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
def read_hubspotUsers():
with open("HubspotUsers.csv", "r") as fw:
reader = csv.reader(fw, delimiter=";")
rowNr = 0
for row in reader:
if rowNr >= 0:
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_oldSyncro()
read_hubspotUsers()
read_intercomUsers()
read_iPaper()
write_hubspot_csv()
init()