-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppController.py
323 lines (261 loc) · 15 KB
/
AppController.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
from Trakt.TraktAPI import TraktAPI
from Database.DatabaseManager import DatabaseManager
from Exporter.ExporterJSON import ExporterJSON
from Utils.Logger import Logger
import Utils.Exporting as Folders
import Utils.Database as DB
import Utils.UI as UI
import Utils.Settings as Settings
import Utils.StatusCodes as StatusCodes
from datetime import datetime
from dateutil import tz
import json
formatFrom="%Y-%m-%dT%H:%M:%S.000Z"
formatTo="%Y-%m-%d %H:%M:%S"
class AppController():
def __init__(self, showLog=True):
self.logger = Logger(showLog, "CONTROLLER")
self.database = DatabaseManager(showLog)
self.settings = self.__LoadConfig()
self.trakt = TraktAPI(showLog)
self.exporter = ExporterJSON(showLog, self.settings[Settings.BACKUP_FOLDER])
def AuthorizeTraktUser(self):
if not self.TraktUserAuthorized():
result = self.trakt.AuthorizeUser(self.settings[Settings.CLIENT_ID], self.settings[Settings.CLIENT_SECRET])
if (result['code'] == StatusCodes.TRAKT_SUCCESS):
self.settings[Settings.ACCESS_TOKEN] = result[Settings.ACCESS_TOKEN]
self.settings[Settings.REFRESH_TOKEN] = result[Settings.REFRESH_TOKEN]
self.__SaveConfig()
return result['code']
else:
self.logger.ShowMessage("User already authorized", UI.SUCCESS_LOG)
return StatusCodes.TRAKT_SUCCESS
def BackupTrakt(self):
if (self.TraktUserAuthorized()):
self.logger.ShowMessage("Starting backup process...")
self.exporter.CreateExportFolders()
self.__BackupHistory()
self.__BackupTraktItem("Ratings", Folders.RATINGS_FOLDER)
self.__BackupTraktItem("Collection", Folders.COLLECTION_FOLDER)
self.__BackupTraktItem("Watched", Folders.WATCHED_FOLDER)
self.__BackupTraktItem("Watchlist", Folders.WATCHLIST_FOLDER)
self.__BackupLists()
self.logger.ShowMessage("Backup finished. Check log for errors")
else:
self.logger.ShowMessage("User is not authorized (missing trakt settings). Error: {} {}".format(StatusCodes.TRAKT_UNAUTHORIZED, StatusCodes.statusMessages[StatusCodes.TRAKT_UNAUTHORIZED]), UI.ERROR_LOG)
def __BackupHistory(self):
self.logger.ShowMessage("Starting history backup...")
playsFound = 0
playsProcessed = 0
plays, statusCode = self.trakt.GetHistory(self.settings[Settings.CLIENT_ID], self.settings[Settings.ACCESS_TOKEN])
# Backup history data to database
if (statusCode == StatusCodes.TRAKT_SUCCESS):
playsFound = len(plays)
playsProcessed = self.__ProcessTraktPlays(plays)
if (playsProcessed != playsFound):
self.logger.ShowMessage("History backup to database finished with errors. Plays found: {}. Plays successfully stored: {}. Check previous logs for more info".format(playsFound, playsProcessed), UI.ERROR_LOG)
else:
self.logger.ShowMessage("History backup to database finished. Plays found: {} Plays successfully stored: {}".format(playsFound, playsProcessed), UI.SUCCESS_LOG)
statusCode = StatusCodes.CONTROLLER_OK
self.logger.ShowMessage("Exporting history data to json...")
# Backup history data to json file
statusCode = self.exporter.ExportData(plays, Folders.WATCHED_FOLDER, 'history')
if (statusCode != StatusCodes.EXPORTER_OK):
self.logger.ShowMessage("Exporting history data to json file failed. Check previous logs", UI.ERROR_LOG)
else:
self.logger.ShowMessage("History data successfully exported to json file", UI.SUCCESS_LOG)
def __BackupLists(self):
self.logger.ShowMessage("Starting backup of lists...")
clientID = self.settings[Settings.CLIENT_ID]
accessToken = self.settings[Settings.ACCESS_TOKEN]
lists, statusCode = self.trakt.GetLists(clientID, accessToken)
if (statusCode == StatusCodes.TRAKT_SUCCESS):
statusCode = self.exporter.ExportData(lists, Folders.LISTS_FOLDER, "lists")
if (statusCode == StatusCodes.EXPORTER_OK):
self.logger.ShowMessage("Lists (summary) successfully exported to json file", UI.SUCCESS_LOG)
else:
self.logger.ShowMessage("Error exporting your lists (summary) to json file. Check previous logs", UI.ERROR_LOG)
self.logger.ShowMessage("Processing details of all lists...")
error = False
for list in lists:
listName = list['name']
listID = list['ids']['trakt']
listDetails, statusCode = self.trakt.GetListDetails(listID, listName, clientID, accessToken)
if (statusCode == StatusCodes.TRAKT_SUCCESS):
statusCode = self.exporter.ExportData(listDetails, Folders.LISTS_FOLDER, "{}-list".format(listName))
if (statusCode == StatusCodes.EXPORTER_OK):
self.logger.ShowMessage("{} list successfully exported to json file".format(listName), UI.SUCCESS_LOG)
else:
self.logger.ShowMessage("Error exporting {} list to json file. Check previous logs".format(listName), UI.ERROR_LOG)
error = True
if (error):
self.logger.ShowMessage("Backup of lists finished with errors. Check previous logs", UI.ERROR_LOG)
else:
self.logger.ShowMessage("Lists data successfully exported to json file", UI.SUCCESS_LOG)
else:
self.logger.ShowMessage("Error getting lists from trakt.", UI.ERROR_LOG)
self.logger.ShowMessage("Backup of lists finished with errors. Check previous logs", UI.ERROR_LOG)
def __BackupTraktItem(self, item, folder):
self.logger.ShowMessage("Starting backup of {}...".format(item))
statusCodeMovies = self.__BackupTraktItemsByType("movies", folder)
statusCodeShows = self.__BackupTraktItemsByType("shows", folder)
# Only export seasons and episodes type for ratings and watchlist.
# Collection and Watched do not have seasons and they already include the information.
if (folder == Folders.RATINGS_FOLDER or folder == Folders.WATCHLIST_FOLDER):
statusCodeSeasons = self.__BackupTraktItemsByType("seasons", folder)
statusCodeEpisodes = self.__BackupTraktItemsByType("episodes", folder)
else:
# Set to EXPORTER_OK if item has no seasons or episodes type
statusCodeSeasons = StatusCodes.EXPORTER_OK
statusCodeEpisodes = StatusCodes.EXPORTER_OK
if (statusCodeMovies != StatusCodes.EXPORTER_OK or statusCodeEpisodes != StatusCodes.EXPORTER_OK
or statusCodeSeasons != StatusCodes.EXPORTER_OK or statusCodeShows != StatusCodes.EXPORTER_OK):
self.logger.ShowMessage("{} backup finished with errors. Check previous logs".format(item), UI.ERROR_LOG)
else:
self.logger.ShowMessage("{} data successfully exported to json file".format(item), UI.SUCCESS_LOG)
def __BackupTraktItemsByType(self, type, folder):
clientID = self.settings[Settings.CLIENT_ID]
accessToken = self.settings[Settings.ACCESS_TOKEN]
if (folder == Folders.COLLECTION_FOLDER):
data, statusCode = self.trakt.GetCollection(type, clientID, accessToken)
elif (folder == Folders.RATINGS_FOLDER):
data, statusCode = self.trakt.GetRatings(type, clientID, accessToken)
elif (folder == Folders.WATCHED_FOLDER):
data, statusCode = self.trakt.GetWatched(type, clientID, accessToken)
elif (folder == Folders.WATCHLIST_FOLDER):
data, statusCode = self.trakt.GetWatchlist(type, clientID, accessToken)
if (statusCode == StatusCodes.TRAKT_SUCCESS):
self.logger.ShowMessage("Exporting data to json...")
statusCode = self.exporter.ExportData(data, folder, type)
if (statusCode != StatusCodes.EXPORTER_OK):
self.logger.ShowMessage("Error exporting {} {} to json file. Check previous logs".format(type, folder), UI.ERROR_LOG)
else:
self.logger.ShowMessage("{} {} successfully exported to json file".format(type, folder), UI.SUCCESS_LOG)
else:
self.logger.ShowMessage("Error getting {} {} from trakt. Check previous logs".format(type, folder), UI.ERROR_LOG)
return statusCode
def GetAccessToken(self):
return self.settings[Settings.ACCESS_TOKEN]
def GetRefreshToken(self):
return self.settings[Settings.REFRESH_TOKEN]
def GetClientID(self):
return self.settings[Settings.CLIENT_ID]
def GetClientSecret(self):
return self.settings[Settings.CLIENT_SECRET]
def GetBackupFolder(self):
return self.settings[Settings.BACKUP_FOLDER]
def SetTraktConfig(self, clientID, clientSecret):
self.settings[Settings.CLIENT_ID] = clientID
self.settings[Settings.CLIENT_SECRET] = clientSecret
self.__SaveConfig()
def SetBackupFolder(self, folder):
self.settings[Settings.BACKUP_FOLDER] = folder
self.exporter.SetBackupFolder(folder)
self.__SaveConfig()
def GetHistoryData(self, movies, episodes, year):
self.logger.ShowMessage("Querying database for plays...")
plays = []
if (movies and episodes):
plays, statusCode = self.database.GetHistoryByYear(year)
elif (episodes):
plays, statusCode = self.database.GetEpisodesByYear(year)
else:
plays, statusCode = self.database.GetMoviesByYear(year)
if (statusCode != StatusCodes.DATABASE_OK):
self.logger.ShowMessage("Request finished with an error. Check previous logs", UI.ERROR_LOG)
else:
statusCode = StatusCodes.CONTROLLER_OK
return plays, statusCode
def GetHistoryYears(self):
years, statusCode = self.database.GetHistoryYears()
if (statusCode != StatusCodes.DATABASE_OK):
self.logger.ShowMessage("Could not get history years. Check previous logs", UI.ERROR_LOG)
else:
statusCode = StatusCodes.CONTROLLER_OK
return years, statusCode
def RefreshTraktToken(self):
self.logger.ShowMessage("Refreshing access token...")
accessToken, refreshToken, statusCode = self.trakt.RefreshToken(self.settings[Settings.CLIENT_ID], self.settings[Settings.CLIENT_SECRET], self.settings[Settings.REFRESH_TOKEN])
if (statusCode == StatusCodes.TRAKT_SUCCESS):
self.settings[Settings.ACCESS_TOKEN] = accessToken
self.settings[Settings.REFRESH_TOKEN] = refreshToken
self.__SaveConfig()
else:
self.logger.ShowMessage("Could not refresh token. Check previous logs", UI.ERROR_LOG)
def DeleteTraktToken(self):
self.logger.ShowMessage("Deleting tokens...")
statusCode = self.database.DeleteTokens()
if (statusCode == StatusCodes.DATABASE_OK):
self.settings[Settings.ACCESS_TOKEN] = ""
self.settings[Settings.REFRESH_TOKEN] = ""
self.logger.ShowMessage("Tokens successfully deleted", UI.SUCCESS_LOG)
else:
self.logger.ShowMessage("Could not delete tokens. Check previous logs", UI.ERROR_LOG)
def __ProcessTraktPlays(self, plays):
episodes = []
movies = []
# Parse json objects
for play in plays:
playID = play['id']
type = play['type']
watchedAt = play['watched_at']
watchedAtLocal = self.__ConvertToLocalTime(watchedAt)
if (type == 'episode'):
season = play['episode']['season']
number = play['episode']['number']
episodeTitle = play['episode']['title']
runtime = play['episode']['runtime']
episodeIDs = play['episode']['ids']
showTitle = play['show']['title']
showYear = play['show']['year']
showIDs = play['show']['ids']
# Order matches the episodes table columns
episodes.append([playID, watchedAt, watchedAtLocal, type, season, number, episodeTitle,
json.dumps(episodeIDs), runtime, showTitle, showYear, json.dumps(showIDs)])
else:
title = play['movie']['title']
year = play['movie']['year']
runtime = play['movie']['runtime']
movieIDs = play['movie']['ids']
# Order matches movies table columns
movies.append([playID, watchedAt, watchedAtLocal, type, title, year, runtime, json.dumps(movieIDs)])
totalInserted = 0
self.logger.ShowMessage("Processing episodes...")
totalInserted += self.database.AddPlays(episodes, 'episode')
self.logger.ShowMessage("Processing movies...")
totalInserted += self.database.AddPlays(movies, 'movie')
return totalInserted
def __ConvertToLocalTime(self, watchedAt):
watchedAt = datetime.strptime(watchedAt, formatFrom)
watchedAt = watchedAt.replace(tzinfo=tz.tzutc())
watchedAt = watchedAt.astimezone(tz.tzlocal()).strftime(formatTo)
return watchedAt
def TraktUserAuthorized(self):
if (len(self.settings[Settings.ACCESS_TOKEN]) == 64):
return True
else:
return False
def __LoadConfig(self):
data, statusCode = self.database.GetSettings()
settings = {Settings.CLIENT_ID : "", Settings.CLIENT_SECRET : "", Settings.ACCESS_TOKEN : "", Settings.REFRESH_TOKEN : "", Settings.BACKUP_FOLDER : ""}
if (statusCode == StatusCodes.DATABASE_OK):
if (len(data) != 0):
settings = {
'clientID' : data[0][DB.CLIENT_ID_COLUMN],
'clientSecret' : data[0][DB.CLIENT_SECRET_COLUMN],
'accessToken' : data[0][DB.ACCESS_TOKEN_COLUMN],
'refreshToken' : data[0][DB.REFRESH_TOKEN_COLUMN],
'backupFolder' : data[0][DB.BACKUP_FOLDER_COLUMN]
}
self.logger.ShowMessage("Settings loaded from database")
else:
self.logger.ShowMessage("No settings found")
else:
self.logger.ShowMessage("An error occurred when loading settings. Check previous logs", UI.ERROR_LOG)
return settings
def __SaveConfig(self):
statusCode = self.database.SaveSettings(self.settings[Settings.CLIENT_ID], self.settings[Settings.CLIENT_SECRET], self.settings[Settings.ACCESS_TOKEN], self.settings[Settings.REFRESH_TOKEN], self.settings[Settings.BACKUP_FOLDER])
if (statusCode == StatusCodes.DATABASE_OK):
self.logger.ShowMessage("Settings saved to database", UI.SUCCESS_LOG)
else:
self.logger.ShowMessage("Could not save settings to database. Check previous logs", UI.ERROR_LOG)