|
12 | 12 | session = scratch3.login("Boss_1sALT", passwrd) |
13 | 13 | cloud = session.connect_cloud("895107188") #<- this is the real project |
14 | 14 |
|
| 15 | +def print2(str): |
| 16 | + print(str) |
| 17 | + os.system(str) |
| 18 | + |
15 | 19 | #db |
16 | 20 | storage = cloud.storage(used_cloud_vars=["3","4"]) |
17 | 21 |
|
18 | | -db1 = db("chat", json_file_path="chat_db.json", save_interval=60) |
19 | | -db2 = db("history", json_file_path="chat_history_db.json", save_interval=60) |
| 22 | +db1 = db("chat", json_file_path="chat_db.json", save_interval=5) |
| 23 | +db2 = db("history", json_file_path="chat_history_db.json", save_interval=5) |
20 | 24 |
|
21 | 25 | storage.add_database(db1) |
22 | 26 | storage.add_database(db2) |
23 | 27 |
|
24 | 28 | @storage.request(response_priority=1) |
25 | 29 | def ping2(): |
26 | 30 | return "pong" |
27 | | - print("Database handler pinged") |
| 31 | + print2("Database handler pinged") |
28 | 32 |
|
29 | 33 | @storage.request(name="delete") |
30 | 34 | def delete_request(db_name, key): |
31 | 35 | del storage.get_database(db_name).data[key] |
32 | 36 |
|
33 | 37 | @storage.request(name="delete_all") |
34 | 38 | def delete_all(db_name): |
35 | | - # write an empty dictionary to the file |
36 | | - if db_name == "chat": |
37 | | - with open("chat_db.json", "w") as f: |
38 | | - json.dump({}, f) |
39 | | - elif db_name == "history": |
40 | | - with open("chat_history_db.json", "w") as f: |
41 | | - json.dump({}, f) |
42 | | - print(f"All data deleted from database {db_name}.") |
43 | | - return "" |
| 39 | + try: |
| 40 | + # write an empty dictionary to the file |
| 41 | + if db_name == "chat": |
| 42 | + with open("chat_db.json", "w") as f: |
| 43 | + json.dump({}, f) |
| 44 | + elif db_name == "history": |
| 45 | + with open("chat_history_db.json", "w") as f: |
| 46 | + json.dump({}, f) |
| 47 | + print2(f"All data deleted from database {db_name}.") |
| 48 | + return "go delete" |
| 49 | + except Exception as e: |
| 50 | + print2(f"Encountered exception {e}, data may have not been deleted") |
| 51 | + return "no-go delete" |
44 | 52 |
|
45 | 53 | @db1.event |
46 | 54 | def on_save(): |
47 | | - print("Data was saved to db chat") |
| 55 | + print2("Data was saved to db chat") |
48 | 56 |
|
49 | 57 | @db1.event |
50 | 58 | def on_set(key, value): |
51 | | - print("Key", key, "was set to value", value, "in db chat") |
| 59 | + print2(f"Key {key} was set to value {value} in db chat") |
52 | 60 |
|
53 | 61 | @db2.event |
54 | 62 | def on_save(): |
55 | | - print("Data was saved to db history") |
| 63 | + print2("Data was saved to db history") |
56 | 64 |
|
57 | 65 | @db2.event |
58 | 66 | def on_set(key, value): |
59 | | - print("Key", key, "was set to value", value, "in db history") |
| 67 | + print2(f"Key {key} was set to value {value} in db history") |
60 | 68 |
|
61 | 69 | storage.start() |
0 commit comments