-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
264 lines (235 loc) · 11.7 KB
/
main.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
from getpass import getuser
from os import listdir
from os.path import exists
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget, QCheckBox, QTextEdit, QPushButton, QMessageBox, QFileDialog
from PyQt5.QtCore import QSize, QTimer
from multiprocessing import active_children, Queue, freeze_support
import sqlite3
import re
from workers import VideoWorker
class Main(QMainWindow):
def __init__(self):
# Initialize the super class
super().__init__()
# self.setMinimumSize(QSize(640, 480))
self.setMinimumWidth(480)
self.setWindowTitle("pyJulieScanner - A Kodi Database Scanner")
centralWidget = QWidget(self)
self.setCentralWidget(centralWidget)
gridLayout = QGridLayout(self)
centralWidget.setLayout(gridLayout)
self.allOptions = {}
# title.setAlignment(QtCore.Qt.AlignCenter)
# gridLayout.addWidget(title, 0, 0)
# Setup checkboxes for possible tasks
checkboxLabel = QLabel("Perform the following actions:", self)
option1 = QCheckBox("Produce a file containing all movies Kodi does not see", self)
option2 = QCheckBox("Produce a file containing all movies Kodi sees", self)
option3 = QCheckBox("Check TV shows for missing seasons", self)
self.fileOutputDir = "./"
self.fileOutputDirText = "Output Files to: "
self.fileOutputDirButton = QPushButton(self.fileOutputDirText, self)
self.fileOutputDirLabel = QLabel(self.fileOutputDir, self)
# Connect it to setting the outputdir
self.fileOutputDirButton.clicked.connect(self.setOutputDir)
self.allOptions["option1"] = option1
self.allOptions["option2"] = option2
self.allOptions["option3"] = option3
# The log area
self.logText = QTextEdit("",self)
self.logText.setReadOnly(True)
# Action button!
self.doButton = QPushButton("Do Checked Tasks", self)
# Connect a function to the button
self.doButton.clicked.connect(self.doAllPossibleActions)
self.option1ProgressLabel = QLabel("", self)
self.option1ProgressLabel.hide()
gridLayout.addWidget(checkboxLabel, 0, 0, 1, 3)
gridLayout.addWidget(option1, 1, 0, 1, 3)
gridLayout.addWidget(option2, 2, 0, 1, 3)
gridLayout.addWidget(option3, 3, 0, 1, 3)
gridLayout.addWidget(self.fileOutputDirButton, 4, 0, 1, 1)
gridLayout.addWidget(self.fileOutputDirLabel, 4, 1, 1, 3)
# gridLayout.addWidget(self.fileOutputDirText, 4, 1)
gridLayout.addWidget(QLabel("Log:"), 5, 0)
gridLayout.addWidget(self.logText, 6,0,1,4)
gridLayout.addWidget(self.doButton, 5, 3)
gridLayout.addWidget(self.option1ProgressLabel,7,0,1,4)
# Find Kodi database
# self.databases = self.getDatabases()
self.getDatabases()
self.activeProcess = None
self.latest = None
self.option1Timer = None
# Find all media that Kodi doesn't see
# Find all media that Kodi sees
# Find missing TV Show seasons
def doAllPossibleActions(self):
if not self.fileOutputDir:
# Get a file output location
self.setOutputDir()
if self.fileOutputDir is None or not self.fileOutputDir:
msg = QMessageBox()
msg.setWindowTitle("Error")
msg.setText("No Output Directory Selected!")
msg.exec()
self.updateText("No actions selected!")
return None
p = 0
r = re.compile("[\\:\\w&.\\-\\/]+MyVideos\\d+.db$")
videoDatabase = list(filter(r.match, self.databases))[0]
if self.allOptions['option1'].isChecked():
# If a current job is running, kill it!
if self.option1Timer is not None and self.option1Timer.isActive():
self.option1Timer.stop()
if self.activeProcess is not None and self.activeProcess.is_alive():
self.updateText("Killing active task...")
self.activeProcess.terminate()
self.updateText("Success! Starting new requested tasks...")
# Establish a connection to the movie database
connection = sqlite3.connect(videoDatabase)
cursor = connection.cursor()
# Get the movie name, movie path, movie filename
cursor.execute("""SELECT DISTINCT strFilename,strPath FROM files
INNER JOIN movie
ON files.idFile = movie.idFile
INNER JOIN path
ON files.idPath = path.idPath;""")
allKnownMovies = cursor.fetchall()
self.updateText("Found {} files in Kodi...".format(len(allKnownMovies)))
connection.close()
p += 1
mediaDirectory = QFileDialog.getExistingDirectory(self, "Select Media Directory To Scan")
if mediaDirectory == "":
self.updateText("Media directory cannot be ignored for option 1! Skipping...")
else:
# Definitely broken!
# Start a thread safe queue
self.queue = Queue()
# Setup a timer
self.option1Timer = QTimer(self)
self.option1Timer.setInterval(1000)
self.option1Timer.timeout.connect(self.updateMovieDirectoriesScanned)
# Spin up a process
self.activeProcess = VideoWorker(self.queue, mediaDirectory, allKnownMovies,
self.fileOutputDir + "/Missing Movies.csv")
# Start a timer for checking and updating the files found marker
self.activeProcess.start()
self.option1Timer.start()
if self.allOptions['option2'].isChecked():
# Establish a connection to the movie database
connection = sqlite3.connect(videoDatabase)
cursor = connection.cursor()
# Get the movie name, movie path, movie filename
cursor.execute("SELECT DISTINCT c00 FROM movie;")
allKnownMovies = sorted(cursor.fetchall())
allKnownMovies = list(map(lambda nameTup: '"{}"'.format(nameTup[0]), allKnownMovies))
# Point out possible duplicates?
cursor.execute("""SELECT movie.c00,cnt,strPath FROM movie
INNER JOIN files
ON files.idFile = movie.idFile
INNER JOIN path
ON path.idPath = files.idPath
LEFT JOIN (
SELECT c00, COUNT(c00) AS cnt FROM movie
GROUP BY c00
) AS temptable
ON movie.c00 = temptable.c00
WHERE cnt > 1;""")
duplicates = sorted(cursor.fetchall())
duplicates = list(map(lambda entry: ",".join(
list(map(lambda item: '"{}"'.format(item), entry))), duplicates))
self.updateText("Writing out a CSV of movie names from Kodi...")
with open("{}/All Movies.csv".format(self.fileOutputDir), "w") as outfile:
outfile.write("Title\n")
outfile.write("\n".join(allKnownMovies))
self.updateText("{}/All Movies.csv".format(self.fileOutputDir))
with open("{}/All Duplicate Movies.csv".format(self.fileOutputDir), "w") as outfile:
outfile.write("Title,Total,Path\n")
outfile.write("\n".join(duplicates))
self.updateText("{}/All Duplicate Movies.csv".format(self.fileOutputDir))
# Get the movie name, movie path, movie filename
p += 1
# self.updateText("Found {} movies logged by Kodi...".format(numFound))
# Write out the file
if self.allOptions['option3'].isChecked():
connection = sqlite3.connect(videoDatabase)
cursor = connection.cursor()
# cursor.execute("SELECT idMovie, idFile, c00 FROM movie")
# allKnownShows = cursor.fetchall()
connection.close()
self.updateText("Option 3 is not yet available")
p += 1
# self.updateText("Found {} missing seasons".format(numMissing))
# Write out the file
if p == 0:
msg = QMessageBox()
msg.setWindowTitle("Error")
msg.setText("No Actions Selected!")
msg.exec()
self.updateText("No actions selected!")
return None
# Possibly broken
def updateMovieDirectoriesScanned(self):
# Check if active
qsize = self.queue.qsize()
totalActive = len(active_children())
print("Active children: {}".format(totalActive))
# Check the queue
for num in range(qsize):
self.latest = self.queue.get()
if totalActive != 0:
if self.latest is not None:
self.option1ProgressLabel.show()
self.option1ProgressLabel.setText("Currently in {}...".format(str(self.latest)))
else:
self.updateText("Cleaning up...")
if self.latest is not None:
self.option1ProgressLabel.hide()
self.updateText("Safe finish: {} files not imported into Kodi".format(self.latest))
self.updateText("{}Missing Movies.csv".format(self.fileOutputDir))
self.option1Timer.stop()
print("Tick!")
def setOutputDir(self):
self.fileOutputDir = QFileDialog.getExistingDirectory(self, "Select Media Location")
self.fileOutputDirLabel.setText(self.fileOutputDir)
self.updateText("Output directory selected as:\n{}".format(self.fileOutputDir))
return None
def updateText(self, moreText):
self.logText.setText("{}{}{}".format(self.logText.toPlainText(), moreText, "\n"))
self.logText.verticalScrollBar().setValue(self.logText.verticalScrollBar().maximum())
return None
def getDatabases(self, databaseRoot=None):
# For windows only
if databaseRoot is None:
databaseRoot = "C:/Users/{}/AppData/Roaming/Kodi/userdata/Database".format(getuser())
databases = []
if exists(databaseRoot):
allfiles = listdir(databaseRoot)
for afile in allfiles:
if afile[-3:] == ".db":
databases.append(afile)
if databases == []:
self.updateText("Directory exists but no databases found? Program will crash if ran further")
else:
databases = list(map(lambda database: "{}/{}".format(databaseRoot, database), databases))
self.updateText("Databases found: {}".format(databases))
self.databases = databases
else:
# Need a better work around
if databaseRoot == "" or databaseRoot != "C:/Users/{}/AppData/Roaming/Kodi/userdata/Database".format(getuser()):
exit()
self.updateText("No databases found at {}".format(databaseRoot))
self.updateText("A new location must be selected and must contain the userdata folder for kodi or the application will crash")
# Allow the user to pick a directory but see files
# databaseRoot = QFileDialog.getExistingDirectory(self, "Select Database Location",QFileDialog.)
databaseRoot = QFileDialog.getExistingDirectory(self, "Select Database Location")
self.getDatabases(databaseRoot=databaseRoot)
if __name__ == "__main__":
freeze_support()
from sys import argv, exit
app = QtWidgets.QApplication(argv)
mainWin = Main()
mainWin.show()
exit(app.exec_())