|
|
|
import json
|
|
|
|
import datetime
|
|
|
|
from config import youtube_prefix
|
|
|
|
|
|
|
|
class Database:
|
|
|
|
def __init__(self, base_path):
|
|
|
|
self.base_path = base_path
|
|
|
|
self.file_path = f'{base_path}/index.json'
|
|
|
|
try:
|
|
|
|
with open(self.file_path, 'r') as file:
|
|
|
|
self.data = json.load(file)
|
|
|
|
except FileNotFoundError:
|
|
|
|
self.data = []
|
|
|
|
|
|
|
|
def add_entry(self, entry):
|
|
|
|
entry['timestamp'] = datetime.datetime.now().strftime('%d/%m/%Y')
|
|
|
|
self.data.append(entry)
|
|
|
|
with open(self.file_path, 'w') as file:
|
|
|
|
json.dump(self.data, file, indent=4)
|
|
|
|
|
|
|
|
def downloadable(self, url, title=None):
|
|
|
|
idx = url.split(youtube_prefix)[-1]
|
|
|
|
for entry in self.data:
|
|
|
|
if entry['id'] == idx or (title is not None and entry['title'] == title):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_all_entries(self):
|
|
|
|
return self.data
|
|
|
|
|
|
|
|
def update_entry(self, entry_id, new_data):
|
|
|
|
for entry in self.data:
|
|
|
|
if entry['id'] == entry_id:
|
|
|
|
entry.update(new_data)
|
|
|
|
with open(self.file_path, 'w') as file:
|
|
|
|
json.dump(self.data, file, indent=4)
|
|
|
|
return
|