-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
187 lines (162 loc) · 8.28 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import logging
import time
import traceback
from datetime import datetime, timezone
from os import getenv
import gspread
import psycopg2
from dotenv import load_dotenv
# Set up logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
handler = logging.FileHandler("logs.log", encoding='utf-8', mode='a')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s: %(message)s'))
logger.addHandler(handler)
# Load environment variables
load_dotenv()
sheet = getenv("SHEET")
db_host = getenv("DB_HOST")
db_user = getenv("DB_USER")
db_pass = getenv("DB_PASS")
db_name = getenv("DB_NAME")
update_interval = int(getenv("UPDATE_INTERVAL"))
conflict_priority = getenv("CONFLICT_PRIORITY")
# Connect to the Google Sheets API
try:
gc = gspread.service_account(filename="service_account.json")
except FileNotFoundError:
print("Service account file not found")
exit(1)
except:
print("Error loading service account. Please ensure the service account file is valid")
exit(1)
try:
sh = gc.open(sheet)
except gspread.SpreadsheetNotFound:
print(f"Spreadsheet {sheet} not found. Please ensure the spreadsheet exists and the service account has access to it")
exit(1)
# Connect to the database
try:
conn = psycopg2.connect(
host=db_host,
user=db_user,
password=db_pass,
dbname=db_name
)
except psycopg2.OperationalError:
print("Error connecting to the database")
exit(1)
cur = conn.cursor()
# Last updated time of the sheet
last_sheet_update = sh_updated = datetime.strptime("1970-01-01 00:00:00", "%Y-%m-%d %H:%M:%S").replace(tzinfo=timezone.utc)
last_db_update = datetime.strptime("1970-01-01 00:00:00", "%Y-%m-%d %H:%M:%S").replace(tzinfo=timezone.utc)
def check_row(sh_row, db_row):
if sh_row['First Name'] != db_row[1]:
return False
if sh_row['Last Name'] != db_row[2]:
return False
if sh_row['Email'] != db_row[3]:
return False
if sh_row['Phone Number'] != db_row[4]:
return False
return True
# Start syncronisation
logger.info("Starting syncronisation")
try:
while True:
# Get the updated version of the sheet
sh = gc.open(sheet)
sh_updated = max(datetime.strptime(sh.get_lastUpdateTime(), "%Y-%m-%dT%H:%M:%S.%fZ").replace(tzinfo=timezone.utc), sh_updated)
print("Sheet updated at: ", sh_updated)
# Get the updated version of the database
db_updated = cur.execute("SELECT max(last_updated) FROM candidates")
db_updated = cur.fetchone()[0]
db_deleted = cur.execute("SELECT max(last_updated) FROM deleted_candidates")
db_deleted = cur.fetchone()[0]
if db_updated and db_deleted:
db_updated = max(db_updated, db_deleted)
elif db_deleted:
db_updated = db_deleted
db_updated = db_updated.replace(tzinfo=timezone.utc)
print("Database updated at: ", db_updated)
if sh_updated > last_sheet_update and last_db_update >= db_updated: # If only the sheet has been updated since the last update
logger.info("Sheet has been updated")
# Get the data from the sheet and the database
sh_data = sh.sheet1.get_all_records()
db_data = cur.execute("SELECT * FROM candidates order by id")
db_data = cur.fetchall()
for i,row in enumerate(sh_data):
if i < len(db_data) and not check_row(row, db_data[i]):
# Update the database
cur.execute("UPDATE candidates SET first_name = %s, last_name = %s, email = %s, phone = %s, last_updated = %s WHERE id = %s",
(row["First Name"], row["Last Name"], row["Email"], row["Phone Number"], sh_updated, i+1))
elif i >= len(db_data):
cur.execute("INSERT INTO candidates (first_name, last_name, email, phone, last_updated) VALUES (%s, %s, %s, %s, %s)",
(row["First Name"], row["Last Name"], row["Email"], row["Phone Number"], sh_updated))
if len(sh_data) < len(db_data):
for i in range(len(sh_data), len(db_data)):
cur.execute("DELETE FROM candidates WHERE id = %s", (db_data[i][0],))
cur.execute("DELETE FROM deleted_candidates WHERE id = %s", (db_data[i][0],)) # Delete the row from the deleted_candidates table
conn.commit()
db_updated = datetime.now(timezone.utc)
elif sh_updated <= last_sheet_update and last_db_update < db_updated: # If only the database has been updated since the last update
logger.info("Database has been updated")
# Get the data from the sheet and the database
sh_data = sh.sheet1.get_all_records()
db_data = cur.execute("SELECT * FROM candidates order by id")
db_data = cur.fetchall()
min_len = min(len(sh_data), len(db_data))
update_data = [[row[1], row[2], row[3], row[4]] for row in db_data[:min_len]]
sh.sheet1.update(update_data, f"A2:D{min_len+1}")
if len(sh_data) < len(db_data): # If there are more rows in the database than in the sheet
sh.sheet1.insert_rows([[row[1], row[2], row[3], row[4]] for row in db_data[min_len:]], min_len+2)
elif len(sh_data) > len(db_data):
sh.sheet1.delete_rows(len(db_data)+2, len(sh_data)-len(db_data))
db_updated = datetime.now(timezone.utc)
elif sh_updated > last_sheet_update and last_db_update < db_updated: # If both the sheet and the database have been updated since the last update
logger.info("Sheet and database have been updated")
if conflict_priority == "Sheet":
sh_data = sh.sheet1.get_all_records()
db_data = cur.execute("SELECT * FROM candidates order by id")
db_data = cur.fetchall()
for i,row in enumerate(sh_data):
if i < len(db_data) and not check_row(row, db_data[i]):
# Update the database
cur.execute("UPDATE candidates SET first_name = %s, last_name = %s, email = %s, phone = %s, last_updated = %s WHERE id = %s",
(row["First Name"], row["Last Name"], row["Email"], row["Phone Number"], sh_updated, i+1))
elif i >= len(db_data):
cur.execute("INSERT INTO candidates (first_name, last_name, email, phone, last_updated) VALUES (%s, %s, %s, %s, %s)",
(row["First Name"], row["Last Name"], row["Email"], row["Phone Number"], sh_updated))
if len(sh_data) < len(db_data):
for i in range(len(sh_data), len(db_data)):
cur.execute("DELETE FROM candidates WHERE id = %s", (db_data[i][0],))
cur.execute("DELETE FROM deleted_candidates WHERE id = %s", (db_data[i][0],)) # Delete the row from the deleted_candidates table
conn.commit()
else:
sh_data = sh.sheet1.get_all_records()
db_data = cur.execute("SELECT * FROM candidates order by id")
db_data = cur.fetchall()
min_len = min(len(sh_data), len(db_data))
update_data = [[row[1], row[2], row[3], row[4]] for row in db_data[:min_len]]
sh.sheet1.update(update_data, f"A2:D{min_len+1}")
if len(sh_data) < len(db_data):
sh.sheet1.insert_rows([[row[1], row[2], row[3], row[4]] for row in db_data[min_len:]], min_len+2)
elif len(sh_data) > len(db_data):
sh.sheet1.delete_rows(len(db_data)+2, len(sh_data)-len(db_data))
db_updated = datetime.now(timezone.utc)
sh_updated = db_updated
else: # No updates
pass
# Update the last updated times
last_sheet_update = sh_updated
last_db_update = db_updated
# Sleep for the update interval
time.sleep(update_interval)
except KeyboardInterrupt:
logger.info("Stopping syncronisation")
conn.close()
exit(0)
except Exception as e:
logger.error(f"An error occured: {e}\n{traceback.format_exc()}")
conn.close()
exit(1)