-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.py
executable file
·195 lines (116 loc) · 5.41 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
__author__ = 'leonardoalbuquerque'
from blitzdb import FileBackend, queryset
from Entity import Channel, Configuration, Video
import time
class Database:
def __init__(self):
self.database = FileBackend("db")
self.database.autocommit = True
return
# name, date, id, unwanted_words
def save_channel(self, data):
data['date'] += "T19:00:00+00:00"
addedChannel = self.database.save(Channel.Channel(data))
return addedChannel
def get_channels_list(self):
return self.database.filter(Channel.Channel, {})
def delete_channel_by_name(self, channel_name):
channel = self.database.filter(Channel.Channel, {'name': channel_name})
channel.delete()
def delete_channel_by_id(self, channel_id):
channel = self.database.filter(Channel.Channel, {'id': channel_id})
channel.delete()
def add_channel_unwanted_word(self, channelName, unwanted_words):
channel = self.channels.get(Channel.Channel, {"name": channelName})
channel.unwanted_words.update(unwanted_words)
channel.save()
def get_channel_unwanted_words(self, channelName):
channel = self.database.get(Channel.Channel, {"name": channelName})
if not channel:
print("Channel not found")
return
return channel.unwanted_words
def remove_channel_unwanted_word(self, channel_name, unwanted_words):
channel = self.database.get(Channel.Channel, {"name": channel_name})
channel.unwanted_words = {k: v for k, v in channel.unwanted_words.iteritems() if v != unwanted_words}
channel.save()
def change_channel_date(self, channel_name, new_date):
channel = self.database.get(Channel.Channel, {'name': channel_name})
channel.date = new_date
channel.save()
# videos
def save_video(self, channel_id, video):
video['channel_id'] = channel_id
video['downloaded'] = False
try:
self.database.get(Video.Video, {"id": video['id']})
except:
video['add_date'] = self.get_current_date()
self.database.save(Video.Video(video))
def set_video_downloaded(self, video_id):
video = self.database.get(Video.Video, {"id": video_id})
video.download_date = self.get_current_date()
video.downloaded = True
video.save()
def set_video_download_data(self, video_id, download_data):
video = self.database.get(Video.Video, {"id": video_id})
video.download_data = download_data
video.save()
# gets all not downloaded videos of a channel by its id or name,
# if no channel is given than finds not downloaded videos from all channels
def get_not_downloaded_videos(self, **kwargs):
if len(kwargs) == 0:
return self.database.filter(Video.Video, {"downloaded": False}).sort('download_date', queryset.QuerySet.ASCENDING)
elif "channel_name" in kwargs:
channel = self.database.filter(Video.Video, {"name": kwargs['channel_name']}).sort('download_date', queryset.QuerySet.ASCENDING)
if not channel:
return False
channel_id = channel['pk']
else:
channel_id = kwargs['channel_id']
return self.database.filter(Video.Video, {"channel_id": channel_id, "downloaded": False}).sort('download_date', queryset.QuerySet.ASCENDING)
def video_was_downloaded(self, video_id):
try:
self.database.get(Video.Video, {"downloaded": True, "id": video_id })
return True
except:
return False
# general config
def set_download_path(self, path):
try:
download_path = self.database.get(Configuration.Configuration, {'name': 'download_path'})
download_path.download_path = path
download_path.save()
except:
self.database.save(Configuration.Configuration({'name': 'download_path', 'download_path': path}))
def get_download_path(self):
try:
download_path = self.database.get(Configuration.Configuration, {'name': 'download_path'})
except Configuration.Configuration.DoesNotExist as err:
return False
if download_path:
return download_path['download_path']
return False
def get_last_downloaded_videos(self):
channels = self.get_channels_list()
videos = []
for channel in channels:
videoFromDb = self.database.filter(Video.Video, {'channel_id': channel.pk}).sort('download_date', queryset.QuerySet.DESCENDING)
if videoFromDb:
videos.append(videoFromDb.next())
return videos
def get_current_date(self):
return time.strftime("%d/%m/%Y-%H:%M:%S")
def set_last_search_date(self):
try:
configuration = self.database.get(Configuration.Configuration, {"name": "last_search_date"})
except:
configuration = self.database.save(Configuration.Configuration({'name': "last_search_date", "last_search_date": self.get_current_date()}))
configuration.last_search_date = self.get_current_date()
configuration.save()
def get_last_search_date(self):
try:
last_search_date = self.database.get(Configuration.Configuration, {"name": "last_search_date"})
except:
return "Never"
return last_search_date['last_search_date']