-
Notifications
You must be signed in to change notification settings - Fork 0
/
atcommon.py
324 lines (298 loc) · 13.9 KB
/
atcommon.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env python3
''' atcommon - common functions for archivetube scripts '''
import sys
import sqlite3
__prog__ = "archivetube"
__version__ = "0.5.0"
__dbversion__ = 5
__archivedbversion__ = 5
# --------------------------------------------------------------------------- #
def connectDB(path, checkThread=True):
'''Connect to existing db and upgrade it if necessary
:param path: Path at which to store the new database
:type path: string
:param checkThread: Whether sqlite3 should check the thread (default: True)
:type checkThread: boolean
:raises: :class:``sqlite3.Error: Unable to create database
:returns: Connection to database
:rtype: sqlite3.Connection
'''
#Connect to database
dbCon = sqlite3.connect(path, check_same_thread=checkThread)
#Upgrade database
upgradeDB(dbCon)
#Return database connection
return dbCon
# ########################################################################### #
# --------------------------------------------------------------------------- #
def createDB(path, checkThread=True):
'''Create database with the required tables
:param path: Path at which to store the new database
:type path: string
:param checkThread: Whether sqlite3 should check the thread (default: True)
:type checkThread: boolean
:raises: :class:``sqlite3.Error: Unable to create database
:returns: Connection to the newly created database
:rtype: sqlite3.Connection
'''
infoCmd = """ CREATE TABLE IF NOT EXISTS info (
id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL,
lastupdate INTEGER NOT NULL,
channels INTEGER NOT NULL,
videos INTEGER NOT NULL,
dbversion INTEGER NOT NULL,
statisticsupdated INTEGER
); """
archivesCmd = """ CREATE TABLE IF NOT EXISTS archives (
id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL,
relpath TEXT NOT NULL,
abspath TEXT NOT NULL,
recursive BOOLEAN NOT NULL
); """
channelsCmd = """ CREATE TABLE IF NOT EXISTS channels (
id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL,
relpath TEXT UNIQUE NOT NULL,
abspath TEXT UNIQUE NOT NULL,
name TEXT NOT NULL,
url TEXT NOT NULL,
language TEXT NOT NULL,
description TEXT,
location TEXT,
joined TEXT,
links TEXT,
profile BLOB,
profileformat TEXT,
banner BLOB,
bannerformat TEXT,
videos INTEGER NOT NULL,
lastupdate INTEGER NOT NULL,
active INTEGER NOT NULL DEFAULT 0
); """
videosCmd = """ CREATE TABLE IF NOT EXISTS videos (
id TEXT PRIMARY KEY UNIQUE NOT NULL,
channelID INTEGER NOT NULL,
title TEXT NOT NULL,
timestamp INTEGER NOT NULL,
description TEXT,
subtitles TEXT,
filepath TEXT NOT NULL,
thumb BLOB,
thumbformat TEXT,
duration INTEGER,
tags TEXT,
language TEXT NOT NULL,
width INTEGER NOT NULL,
height INTEGER NOT NULL,
resolution TEXT NOT NULL,
viewcount INTEGER,
likecount INTEGER,
dislikecount INTEGER,
statisticsupdated INTEGER NOT NULL,
active INTEGER NOT NULL DEFAULT 0,
chapters TEXT
); """
overallCmd = """ CREATE TABLE IF NOT EXISTS statsOverall (
id INTEGER PRIMARY KEY UNIQUE NOT NULL,
timestamp INTEGER NOT NULL,
videos INTEGER NOT NULL,
v8k INTEGER NOT NULL,
fraction8k REAL NOT NULL,
v4k INTEGER NOT NULL,
fraction4k REAL NOT NULL,
vFullHD INTEGER NOT NULL,
fractionFullHD REAL NOT NULL,
vHD INTEGER NOT NULL,
fractionHD REAL NOT NULL,
vSD INTEGER NOT NULL,
fractionSD REAL NOT NULL,
vLD INTEGER NOT NULL,
fractionLD REAL NOT NULL,
subtitles INTEGER NOT NULL,
fractionSubtitles REAL NOT NULL,
chapters INTEGER NOT NULL,
fractionChapters REAL NOT NULL,
totalDuration INTEGER NOT NULL,
avgDuration REAL NOT NULL
); """
weeklyCmd = """ CREATE TABLE IF NOT EXISTS statsWeekly (
id INTEGER PRIMARY KEY UNIQUE NOT NULL,
timestamp INTEGER NOT NULL,
year INTEGER NOT NULL,
week INTEGER NOT NULL,
mondaydate TEXT NOT NULL,
sundaydate TEXT NOT NULL,
videos INTEGER NOT NULL,
v8k INTEGER NOT NULL,
fraction8k REAL NOT NULL,
v4k INTEGER NOT NULL,
fraction4k REAL NOT NULL,
vFullHD INTEGER NOT NULL,
fractionFullHD REAL NOT NULL,
vHD INTEGER NOT NULL,
fractionHD REAL NOT NULL,
vSD INTEGER NOT NULL,
fractionSD REAL NOT NULL,
vLD INTEGER NOT NULL,
fractionLD REAL NOT NULL,
subtitles INTEGER NOT NULL,
fractionSubtitles REAL NOT NULL,
chapters INTEGER NOT NULL,
fractionChapters REAL NOT NULL,
totalDuration INTEGER NOT NULL,
avgDuration REAL NOT NULL
); """
plotCmd = """ CREATE TABLE IF NOT EXISTS statsPlots (
name TEXT PRIMARY KEY UNIQUE NOT NULL,
timestamp INTEGER NOT NULL,
png BLOB,
svgLight BLOB,
svgDark BLOB
); """
#Create database
dbCon = sqlite3.connect(path, check_same_thread=checkThread)
db = dbCon.cursor()
#Set encoding
db.execute("pragma encoding=UTF8")
#Create tables
db.execute(infoCmd)
db.execute("INSERT INTO info(lastupdate, channels, videos, dbversion) VALUES(?,?,?,?)", (0, 0, 0, __dbversion__))
db.execute(archivesCmd)
db.execute(channelsCmd)
db.execute(videosCmd)
db.execute(overallCmd)
db.execute(weeklyCmd)
db.execute(plotCmd)
dbCon.commit()
#Return database connection
return dbCon
# ########################################################################### #
# --------------------------------------------------------------------------- #
def upgradeDB(dbCon):
'''Create database with the required tables
:param dbCon: Connection to the database
:type dbCon: sqlite3.Connection
:raises: :class:``sqlite3.Error: Unable to upgrade database
'''
db = dbCon.cursor()
#Get database version
try:
r = db.execute("SELECT dbversion FROM info ORDER BY id DESC LIMIT 1;")
version = r.fetchone()[0]
del r
except (sqlite3.Error, TypeError):
sys.exit("ERROR: Unsupported database!")
#Check if not up to date
if version < __dbversion__:
print("Upgrading database")
try:
#Perform upgrade to version 2
if version < 2:
#Clear video database
db.execute("DELETE FROM videos")
#Add new columns
db.execute('ALTER TABLE videos ADD COLUMN language TEXT NOT NULL;')
db.execute('ALTER TABLE videos ADD COLUMN width INTEGER NOT NULL;')
db.execute('ALTER TABLE videos ADD COLUMN height INTEGER NOT NULL;')
db.execute('ALTER TABLE videos ADD COLUMN resolution TEXT NOT NULL;')
db.execute('ALTER TABLE videos ADD COLUMN viewcount INTEGER;')
db.execute('ALTER TABLE videos ADD COLUMN likecount INTEGER;')
db.execute('ALTER TABLE videos ADD COLUMN dislikecount INTEGER;')
db.execute('ALTER TABLE videos ADD COLUMN statisticsupdated INTEGER NOT NULL;')
#Update db version
version = 2
db.execute("UPDATE info SET dbversion = ? WHERE id = 1", (version,))
dbCon.commit()
#Perform upgrade to version 3
if version < 3:
#Add active channel and video column
db.execute('ALTER TABLE channels ADD COLUMN active INTEGER NOT NULL DEFAULT 0;')
db.execute('ALTER TABLE videos ADD COLUMN active INTEGER NOT NULL DEFAULT 0;')
#Update db version
version = 3
db.execute("UPDATE info SET dbversion = ? WHERE id = 1", (version,))
dbCon.commit()
#Perform upgrade to version 4
if version < 4:
#Add active channel and video column
db.execute('ALTER TABLE videos ADD COLUMN chapters TEXT;')
#Update db version
version = 4
db.execute("UPDATE info SET dbversion = ? WHERE id = 1", (version,))
dbCon.commit()
#Perform upgrade to version 5
if version < 5:
#Add statistics updated to info
db.execute('ALTER TABLE info ADD COLUMN statisticsupdated INTEGER;')
#Add overall statistics table
overallCmd = """ CREATE TABLE IF NOT EXISTS statsOverall (
id INTEGER PRIMARY KEY UNIQUE NOT NULL,
timestamp INTEGER NOT NULL,
videos INTEGER NOT NULL,
v8k INTEGER NOT NULL,
fraction8k REAL NOT NULL,
v4k INTEGER NOT NULL,
fraction4k REAL NOT NULL,
vFullHD INTEGER NOT NULL,
fractionFullHD REAL NOT NULL,
vHD INTEGER NOT NULL,
fractionHD REAL NOT NULL,
vSD INTEGER NOT NULL,
fractionSD REAL NOT NULL,
vLD INTEGER NOT NULL,
fractionLD REAL NOT NULL,
subtitles INTEGER NOT NULL,
fractionSubtitles REAL NOT NULL,
chapters INTEGER NOT NULL,
fractionChapters REAL NOT NULL,
totalDuration INTEGER NOT NULL,
avgDuration REAL NOT NULL
); """
db.execute(overallCmd)
#Add weekly statistics table
weeklyCmd = """ CREATE TABLE IF NOT EXISTS statsWeekly (
id INTEGER PRIMARY KEY UNIQUE NOT NULL,
timestamp INTEGER NOT NULL,
year INTEGER NOT NULL,
week INTEGER NOT NULL,
mondaydate TEXT NOT NULL,
sundaydate TEXT NOT NULL,
videos INTEGER NOT NULL,
v8k INTEGER NOT NULL,
fraction8k REAL NOT NULL,
v4k INTEGER NOT NULL,
fraction4k REAL NOT NULL,
vFullHD INTEGER NOT NULL,
fractionFullHD REAL NOT NULL,
vHD INTEGER NOT NULL,
fractionHD REAL NOT NULL,
vSD INTEGER NOT NULL,
fractionSD REAL NOT NULL,
vLD INTEGER NOT NULL,
fractionLD REAL NOT NULL,
subtitles INTEGER NOT NULL,
fractionSubtitles REAL NOT NULL,
chapters INTEGER NOT NULL,
fractionChapters REAL NOT NULL,
totalDuration INTEGER NOT NULL,
avgDuration REAL NOT NULL
); """
db.execute(weeklyCmd)
#Add plots table
plotCmd = """ CREATE TABLE IF NOT EXISTS statsPlots (
name TEXT PRIMARY KEY UNIQUE NOT NULL,
timestamp INTEGER NOT NULL,
png BLOB,
svgLight BLOB,
svgDark BLOB
); """
db.execute(plotCmd)
#Update db version
version = 5
db.execute("UPDATE info SET dbversion = ? WHERE id = 1", (version,))
dbCon.commit()
except sqlite3.Error as e:
dbCon.rollback()
dbCon.close()
sys.exit("ERROR: Unable to upgrade database (\"{}\")".format(e))
dbCon.commit()
# ########################################################################### #