-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress_handler.py
More file actions
37 lines (33 loc) · 938 Bytes
/
progress_handler.py
File metadata and controls
37 lines (33 loc) · 938 Bytes
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
from json.decoder import JSONDecodeError
from os import path
import json
def save_progress(data):
f = open('progress.json', 'w')
json.dump(data, f)
f.close()
def get_progress():
if path.exists('progress.json'):
try:
file = open('progress.json', 'r')
data = json.load(file)
file.close()
return data
except JSONDecodeError:
print('Error decoding progress.json!')
else:
return {}
def get_read(title):
progress = get_progress()
if progress.get(title):
return progress[title]['chapter']
return 0
def read_chapter(title, number):
progress = get_progress()
if progress.get(title):
data = progress[title]
if number > data['chapter']:
data['chapter'] = number
save_progress(progress)
else:
progress[title] = {'chapter': number}
save_progress(progress)