File size: 6,813 Bytes
27867f1 |
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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
"""
MIT License
Copyright (C) 2023 ROCKY4546
https://github.com/rocky4546
This file is part of Cabernet
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
"""
import json
import datetime
from lib.db.db import DB
from lib.common.decorators import Backup
from lib.common.decorators import Restore
DB_EPG_TABLE = 'epg'
DB_CONFIG_NAME = 'db_files-epg_db'
sqlcmds = {
'ct': [
"""
CREATE TABLE IF NOT EXISTS epg (
namespace VARCHAR(255) NOT NULL,
instance VARCHAR(255) NOT NULL,
day DATE NOT NULL,
last_update TIMESTAMP,
file VARCHAR(255) NOT NULL,
UNIQUE(namespace, instance, day)
)
"""
],
'dt': [
"""
DROP TABLE IF EXISTS epg
"""
],
'epg_column_names_get':
"""
SELECT name FROM pragma_table_info('epg')
""",
'epg_add':
"""
INSERT OR REPLACE INTO epg (
namespace, instance, day, last_update, file
) VALUES ( ?, ?, ?, ?, ? )
""",
'epg_by_day_del':
"""
DELETE FROM epg WHERE namespace LIKE ? AND instance LIKE ? AND day < DATE('now',?)
""",
'epg_by_day_get':
"""
SELECT file FROM epg WHERE namespace LIKE ? AND instance LIKE ? AND day < DATE('now',?)
""",
'epg_instance_del':
"""
DELETE FROM epg WHERE namespace=? AND instance LIKE ?
""",
'epg_instance_get':
"""
SELECT file FROM epg WHERE namespace=? AND instance LIKE ?
""",
'epg_last_update_get':
"""
SELECT datetime(last_update, 'localtime') FROM epg WHERE
namespace=? AND instance LIKE ? and day=?
""",
'epg_last_update_update':
"""
UPDATE epg SET
last_update=? WHERE namespace LIKE ? AND instance LIKE ?
""",
'epg_get':
"""
SELECT * FROM epg WHERE
namespace LIKE ? AND instance LIKE ? ORDER BY day LIMIT ? OFFSET ?
""",
'epg_one_get':
"""
SELECT * FROM epg WHERE
namespace=? AND instance=? AND day=?
""",
'epg_name_get':
"""
SELECT DISTINCT namespace FROM epg
""",
'epg_instances_get':
"""
SELECT DISTINCT namespace, instance FROM epg
"""
}
class DBepg(DB):
def __init__(self, _config):
super().__init__(_config, _config['datamgmt'][DB_CONFIG_NAME], sqlcmds)
def get_col_names(self):
return self.get(DB_EPG_TABLE + '_column_names')
def save_program_list(self, _namespace, _instance, _day, _prog_list):
filepath = self.save_file((DB_EPG_TABLE, _namespace, _instance, _day), json.dumps(_prog_list))
if filepath:
self.add(DB_EPG_TABLE, (
_namespace,
_instance,
_day,
datetime.datetime.utcnow(),
str(filepath),))
def del_old_programs(self, _namespace, _instance, _days='-2 day'):
"""
Removes all records for this namespace/instance that are over 2 day old
"""
if not _namespace:
_namespace = '%'
if not _instance:
_instance = '%'
files = self.get(DB_EPG_TABLE + '_by_day', (_namespace, _instance, _days,))
files = [x[0] for x in files]
for f in files:
self.delete_file(f)
self.delete(DB_EPG_TABLE + '_by_day', (_namespace, _instance, _days,))
def del_instance(self, _namespace, _instance):
"""
Removes all records for this namespace/instance
"""
if not _instance:
_instance = '%'
files = self.get(DB_EPG_TABLE + '_instance', (_namespace, _instance,))
files = [x[0] for x in files]
for f in files:
self.delete_file(f)
return self.delete(DB_EPG_TABLE + '_instance', (_namespace, _instance,))
def set_last_update(self, _namespace=None, _instance=None, _day=None):
if not _namespace:
_namespace = '%'
if not _instance:
_instance = '%'
self.update(DB_EPG_TABLE + '_last_update', (
_day,
_namespace,
_instance,
))
def get_last_update(self, _namespace, _instance, _day):
if not _instance:
_instance = '%'
result = self.get(DB_EPG_TABLE + '_last_update', (_namespace, _instance, _day,))
if result is None or len(result) == 0:
return None
else:
last_update = result[0][0]
if last_update is not None:
return datetime.datetime.fromisoformat(last_update)
else:
return None
def get_epg_names(self):
return self.get_dict(DB_EPG_TABLE + '_name')
def get_epg_instances(self):
return self.get_dict(DB_EPG_TABLE + '_instances')
def get_epg_one(self, _namespace, _instance, _day):
row = self.get_dict(DB_EPG_TABLE + '_one', (_namespace, _instance, _day))
if len(row):
blob = self.get_file_by_key((_namespace, _instance, _day,))
if blob:
row[0]['json'] = json.loads(blob)
return row
return []
def init_get_query(self, _namespace, _instance):
if not _namespace:
_namespace = '%'
if not _instance:
_instance = '%'
self.get_init(DB_EPG_TABLE, (_namespace, _instance,))
def get_next_row(self):
row = self.get_dict_next()
namespace = None
instance = None
day = None
if row:
namespace = row['namespace']
instance = row['instance']
day = row['day']
file = row['file']
blob = self.get_file(file)
if blob:
json_data = json.loads(blob)
else:
json_data = []
row = json_data
return row, namespace, instance, day
def close_query(self):
self.cur.close()
@Backup(DB_CONFIG_NAME)
def backup(self, backup_folder):
self.export_sql(backup_folder)
@Restore(DB_CONFIG_NAME)
def restore(self, backup_folder):
return self.import_sql(backup_folder)
|