-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase.py
172 lines (145 loc) · 4.66 KB
/
database.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from datetime import date, timedelta, datetime
from math import ceil
import tinydb
from tinydb.storages import JSONStorage
from tinydb_serialization import SerializationMiddleware
from date_serializer import DateSerializer
DEFAULT_DATABASE = 'bangumi.db'
def opendb():
"""
Open TinyDB database
"""
serialization = SerializationMiddleware()
serialization.register_serializer(DateSerializer(), 'Date Serializer')
return tinydb.TinyDB(DEFAULT_DATABASE, storage=serialization)
def add_bangumis(bangumi_list):
"""
Add bangumis provided by bangumi_list into database
param:
bangumi_list list of bangumis
"""
db = opendb()
db.insert_multiple(bangumi_list)
print(
'{0} bangumi/s has been inserted into database.'.format(
len(bangumi_list)
)
)
db.close()
def set_downloaded_episode(bangumi_name, episode):
"""
Set downloaded episode record of sepcified bangumi
params:
bangumi_name Name of bangumi
episode Episode number
"""
db = opendb()
bangumi = tinydb.Query()
bangumi_info = db.get(bangumi.name == bangumi_name)
bangumi_info['dled_ep'] = episode
db.update(bangumi_info, bangumi.name == bangumi_name)
# IDEA: Change name to get_avail_episodes
def fetch_available_episodes():
"""
Fetch available episodes at the time when the function is called
return:
list of dict of available episode(s)
"""
db = opendb()
animes = db.all()
db.close()
if len(animes) == 0:
print('There is no animes in database')
return []
avail_episodes = []
for anime in animes:
print('anime: {0}'.format(anime['name']))
start_date = anime['start_date']
start_date_datetime = datetime(
start_date.year, start_date.month, start_date.day)
time_interval = datetime.now() - start_date_datetime
time_interval_days = time_interval.total_seconds() \
/ timedelta(days=1).total_seconds()
episode_now = int(ceil(time_interval_days / 7)) - anime['offset']
episode_now = anime['total_ep'] if episode_now > anime['total_ep'] \
else episode_now
print('days between now and start day:{0}'.format(time_interval_days))
print('episode available now:{}'.format(episode_now))
print('downloaded:{}\n\n'.format(anime['dled_ep']))
if episode_now > anime['dled_ep']:
for i in range(anime['dled_ep'] + 1, episode_now + 1):
avail_episodes.append({
'name': anime['name'],
'keyword': anime['keyword'],
'translation_team': anime['translation_team'],
'ep': i,
'folder': anime['folder']
})
return avail_episodes
def update_anime_info(name, new):
"""
Update information of anime with a dict contains new information
params:
name Name of anime you want to update
new A dict contains new infomation, the value whose key name starts
with new_ will replaces the corresponding item
"""
db = opendb()
anime = tinydb.Query()
info = db.get(anime.name == name)
try:
print('\nUpdating {}:'.format(name))
except UnicodeEncodeError:
print('\nUpdating {}:'.format(name.encode('gbk', 'ignore')))
print('Unicode Encode Error raised')
for key in new:
if key.startswith('new_'):
new_key = key[4:]
info[new_key] = new[key]
print('{} is replaced with {}'.format(new_key, new[key]))
db.update(info, anime.name == name)
db.close()
def remove_anime(name):
"""
Remove anime from database
params:
name Name of anime you want to remove
"""
db = opendb()
anime = tinydb.Query()
db.remove(anime.name == name)
db.close()
print('{} is removed from database'.format(name))
def remove_finished_anime():
"""
Remove finished anime in the database
"""
db = opendb()
anime_group = db.all()
count = 0
for anime in anime_group:
if anime['total_ep'] == anime['dled_ep']:
remove_anime(anime['name'])
count += 1
print('{} anime is(are) removed form database'.format(count))
db.close()
def get_anime_by_name(name):
"""
param:
name Name of anime
return:
dict contains all information
"""
db = opendb()
anime = tinydb.Query()
r = db.get(anime.name == name)
db.close()
return r
def get_all_anime():
"""
return:
dict contains all anime in the database
"""
db = opendb()
anime_group = db.all()
return anime_group