-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookmark_handler.py
45 lines (40 loc) · 1.19 KB
/
bookmark_handler.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
from json.decoder import JSONDecodeError
from os import path
import json
def get_bookmarks():
if path.exists('bookmarks.json'):
file = open('bookmarks.json', 'r', encoding='utf-8')
try:
content = json.load(file)
file.close()
return content
except JSONDecodeError:
print('Error decoding bookmarks.json!')
else:
return []
def save_bookmarks(bookmarks):
try:
file = open('bookmarks.json', 'w', encoding='utf-8')
json.dump(bookmarks, file)
file.close()
except:
print('An error occured while trying to save bookmarks!')
def is_bookmarked(title, bookmarks):
for bookmark in bookmarks:
if bookmark['title'] == title:
return True
return False
def add_bookmark(title):
bookmarks = get_bookmarks()
if not is_bookmarked(title, bookmarks):
bookmarks.append({'title': title})
save_bookmarks(bookmarks)
def remove_bookmark(title):
bookmarks = get_bookmarks()
index = 0
for bookmark in bookmarks:
if bookmark['title'] == title:
bookmarks.pop(index)
break
index += 1
save_bookmarks(bookmarks)