-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsavedSubscriptionConfig.py
44 lines (38 loc) · 1.56 KB
/
savedSubscriptionConfig.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
import json
def add_subscription(num, varID, period, token):
try:
with open('Subscriptions.json', 'r') as subDocument:
subData = json.load(subDocument)
except FileNotFoundError:
# If the file doesn't exist, initialize an empty list
subData = []
newSubscription = {"servercount": num, "id": varID, "period": period, "assignmentToken": token}
subData.append(newSubscription)
with open('Subscriptions.json', 'w') as subDocument:
json.dump(subData, subDocument, indent=2)
def delete_subscription(num, varID):
try:
with open('Subscriptions.json', 'r') as subDocument:
subData = json.load(subDocument)
except FileNotFoundError:
# If the file doesn't exist, nothing to delete
return
index = next((index for index, obj in enumerate(subData) if obj.get("id") == varID), None)
if index is not None:
subData.pop(index)
print("Item popped out!")
with open('Subscriptions.json', 'w') as subDocument:
json.dump(subData, subDocument, indent=2)
def remove_all_server_subscriptions(num):
try:
with open('Subscriptions.json', 'r') as subDocument:
subData = json.load(subDocument)
except FileNotFoundError:
# If the file doesn't exist, initialize an empty list
subData = []
if subData:
for item in subData:
if item["servercount"] == num:
subData.remove(item)
with open('Subscriptions.json', 'w') as subDocument:
json.dump(subData, subDocument, indent=2)