-
Notifications
You must be signed in to change notification settings - Fork 0
/
regen.py
70 lines (65 loc) · 3.07 KB
/
regen.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
import time
from conn import get_formatted_spending, verify_skey_integrity, send_twilio_message
from lib import get_transaction_as_text
import database
from threading import Thread
def update_based_on_skey(entry: str, results, index: int):
"""Update the value based on an skey
Made for extend_skey()"""
start = time.perf_counter()
user = database.get_user_with_skey(entry)
if user is None:
return False
user_settings = database.get_user_settings(user.uid)
assert user_settings is not None
if user_settings is not None:
if not user_settings.credential_sync:
return False
if not verify_skey_integrity(entry):
database.remove_user(user.uid)
print (f"Failed to update {user.uid}!")
else:
for plan in database.get_meal_plans(user.uid):
try:
new_data = get_formatted_spending(database.get_session_data(user.uid), plan.plan_id)
if user_settings.receipt_notifications and new_data is not None:
old_data = database.get_purchases(user.uid, plan.plan_id)
print (f"{user.uid} | {plan.plan_id} had {len(new_data) - len(old_data)} new items, ({new_data[0].dt.strftime('%I:%M%p')})")
if old_data is None:
old_data = []
if len(new_data) > len(old_data):
print ("Sending receipt notifications!")
send_twilio_message(
user_settings.phone_number.replace("-", "").replace("(", "").replace(")", "").replace(" ", ""),
get_transaction_as_text(new_data[0])
)
database.safely_add_purchases(user.uid, new_data)
except Exception as e:
print (f"Error updating {user.uid} for plan {plan.plan_id}! {e}")
print (f"Updated {user.uid}!")
results[index] += 1
print (f"Time taken: {time.perf_counter() - start}")
return True
return False
def extend_skey(minutes, num_threads=8):
"""Extends all skey's in the database"""
while True:
skeys = [entry.skey for entry in database.get_all_sessions() if database.get_user_settings(entry.uid).credential_sync]
starting_length = len(skeys)
threads = [Thread() for _ in range(num_threads)]
results = [0 for _ in range(num_threads)]
while True:
if len(skeys) > 0:
for i in range(len(threads)):
if not threads[i].is_alive():
try:
threads[i] = Thread(target=update_based_on_skey, args=(skeys.pop(-1), results, i), name='SkeyUpdate')
threads[i].start()
except IndexError:
break
if not any([thread.is_alive() for thread in threads]):
break
time.sleep(.35)
print(f"Finished Main Thread! {sum(results)} out of {starting_length} updated!")
print(f"Results per thread: {results}")
time.sleep(minutes * 60)