-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathutils.py
31 lines (27 loc) · 1.43 KB
/
utils.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
import json
import os
# ---------------------------------------------------------------------------
# A bunch of utility helper methods
# get_watchlist - Gets a specific watchlist given a name and returns it if it exists
# read_watchlist_file - Reads all the data from a watchlist if it exists and returns the data
# write_watchlist_file - Writes contents to a specified watchlist
# init_watchlistJSON - Creates a new watchlist.json file if it does not exist when the bot starts
# ---------------------------------------------------------------------------
def get_watchlist(watchlist_data, watchlist_name):
for watchlist in watchlist_data["watchlists"]:
if watchlist["name"] == watchlist_name:
return watchlist, None
return None, f"The **{watchlist_name}** watchlist does not exist! \nYou can create it with `/create {watchlist_name}`"
def read_watchlist_file(watchlist_file_name):
watchlist_file = open(watchlist_file_name, 'r')
watchlist_data = json.load(watchlist_file)
watchlist_file.close()
return watchlist_data
def write_watchlist_file(watchlist_file_name, watchlist_data):
watchlist_file = open(watchlist_file_name, 'w')
watchlist_file.write(json.dumps(watchlist_data))
watchlist_file.close()
def init_watchlist_json(watchlist_file_name):
if not os.path.exists(watchlist_file_name):
with open(watchlist_file_name, 'w') as file:
json.dump({"watchlists": []}, file)