-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackupTraktForMe.py
227 lines (176 loc) · 9 KB
/
BackupTraktForMe.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
from AppController import AppController
from Utils.Logger import Logger
import Utils.StatusCodes as StatusCodes
import Utils.UI as UI
import Utils.Database as DB
import dearpygui.dearpygui as GUI
import argparse
# Trakt configuration functions
def AuthorizeUser():
id = GUI.get_value(UI.CLIENT_ID)
secret = GUI.get_value(UI.CLIENT_SECRET)
controller.SetTraktConfig(id, secret)
controller.AuthorizeTraktUser()
UpdateUISettings()
def RefreshUserToken():
controller.RefreshTraktToken()
UpdateUISettings()
def DeleteUserToken():
controller.DeleteTraktToken()
UpdateUISettings()
# Application functions
def BackupTrakt():
controller.BackupTrakt()
UpdateUIYearsCombo()
UpdateHistoryTable()
def SelectBackupFolder(sender, app_data, user_data):
controller.SetBackupFolder(app_data['file_path_name'])
UpdateUISettings()
def SelectBackupFolderCancelled():
logger.ShowMessage("Folder was not changed")
def ClearConsole():
logger.Clear()
def ScrollDown():
logger.ScrollToBottom()
# Updates YEAR_COMBO values
def UpdateUIYearsCombo():
yearsDB, statusCode = controller.GetHistoryYears()
comboYears = UI.YEARS_LIST.copy()
if (statusCode == StatusCodes.CONTROLLER_OK):
for year in yearsDB:
comboYears.append(year['year'])
GUI.configure_item(UI.YEAR_COMBO, items=comboYears)
GUI.configure_item(UI.YEAR_COMBO, default_value=comboYears[0])
GUI.set_value
# Refreshes settings in the UI when they are changed
def UpdateUISettings():
GUI.set_value(UI.CLIENT_ID, controller.GetClientID())
GUI.set_value(UI.CLIENT_SECRET, controller.GetClientSecret())
GUI.set_value(UI.ACCES_TOKEN, controller.GetAccessToken())
GUI.set_value(UI.REFRESH_TOKEN, controller.GetRefreshToken())
GUI.set_value(UI.BACKUP_FOLDER, controller.GetBackupFolder())
if (controller.TraktUserAuthorized()):
GUI.configure_item(UI.AUTHORIZE, show=False)
GUI.configure_item(UI.REFRESH, show=True)
GUI.configure_item(UI.DELETE, show=True)
else:
GUI.configure_item(UI.AUTHORIZE, show=True)
GUI.configure_item(UI.REFRESH, show=False)
GUI.configure_item(UI.DELETE, show=False)
# Gets data from database amb populates history table
def UpdateHistoryTable():
GUI.delete_item(UI.HISTORY_TABLE, children_only=True)
media = GUI.get_value(UI.TYPE_COMBO)
year = GUI.get_value(UI.YEAR_COMBO)
movies = True if (media == "All" or media == "Movies") else False
episodes = True if (media =="All" or media == "Episodes") else False
AddColumnsToTable(movies, episodes)
# If All is selected for year, set it to "" so the query gets all records
if (year == "All"):
year = ""
data, statusCode = controller.GetHistoryData(movies, episodes, year)
if (statusCode == StatusCodes.CONTROLLER_OK):
for play in data:
type = play[DB.TYPE_COLUMN]
season = "-" if type == 'movie' else play[DB.SEASON_COLUMN]
episodeNumber = "-" if type == 'movie' else play[DB.NUMBER_COLUMN]
episodeTitle = "-" if type == 'movie' else play[DB.EPISODE_TITLE_COLUMN]
name = play[DB.TITLE_COLUMN]
date = play[DB.DATE_COLUMN]
with GUI.table_row(parent=UI.HISTORY_TABLE):
GUI.add_text(name)
if (episodes or (episodes and movies)):
GUI.add_text(season)
GUI.add_text(episodeNumber)
GUI.add_text(episodeTitle)
GUI.add_text(date)
logger.ShowMessage("Showing {} plays from last database backup".format(len(data)))
def AddColumnsToTable(movies, episodes):
if (movies and not episodes):
GUI.add_table_column(parent=UI.HISTORY_TABLE, label="Movie")
elif (episodes and not movies):
GUI.add_table_column(parent=UI.HISTORY_TABLE, label="Show")
elif (movies and episodes):
GUI.add_table_column(parent=UI.HISTORY_TABLE, label="Show / Movie")
if (episodes):
GUI.add_table_column(parent=UI.HISTORY_TABLE, label="Season")
GUI.add_table_column(parent=UI.HISTORY_TABLE, label="Number")
GUI.add_table_column(parent=UI.HISTORY_TABLE, label="Episode title")
GUI.add_table_column(parent=UI.HISTORY_TABLE, label="Watched at")
GUI.configure_item(UI.HISTORY_TABLE, freeze_rows=1)
def BuildUserInterface():
GUI.create_context()
GUI.create_viewport(title='Back Up Trakt for Me', width=UI.VIEWPORT_MIN_WIDTH, height=UI.VIEWPORT_MIN_HEIGHT)
with GUI.window(tag=UI.MAIN_WINDOW, no_collapse=True, no_move=True, show=True, no_title_bar=True, width=GUI.get_viewport_width(), height=GUI.get_viewport_height()):
with GUI.tab_bar():
with GUI.tab(tag=UI.BACKUP_TAB, label="Backup"):
# Trakt backup
GUI.add_spacer(height=20)
with GUI.group(horizontal=True):
GUI.add_button(tag=UI.BACKUP, label="Back Up Trakt", callback=BackupTrakt)
GUI.add_text()
GUI.add_separator()
# History table filters (queries made to database)
with GUI.group(horizontal=True, horizontal_spacing=50):
GUI.add_combo(UI.TYPES_LIST, default_value=UI.TYPES_LIST[0], tag=UI.TYPE_COMBO, label="Media", height_mode=GUI.mvComboHeight_Small, width=200, callback=UpdateHistoryTable)
GUI.add_combo(tag=UI.YEAR_COMBO, label="Year", height_mode=GUI.mvComboHeight_Small, width=200, callback=UpdateHistoryTable)
GUI.add_separator()
GUI.add_spacer(height=20)
# History table
with GUI.table(tag=UI.HISTORY_TABLE, header_row=True, no_host_extendX=True, delay_search=True,
borders_innerH=True, borders_outerH=True, borders_innerV=True,
borders_outerV=True, context_menu_in_body=True, row_background=True,
policy=GUI.mvTable_SizingStretchProp, height=200,
scrollY=True, scrollX=True, clipper=True):
AddColumnsToTable(True, True)
with GUI.tab(label="Settings"):
# User's Trakt API keys for authorization
GUI.add_spacer(height=20)
GUI.add_input_text(tag=UI.CLIENT_ID, no_spaces=True, default_value="Your client ID", label="Client ID")
GUI.add_separator()
GUI.add_input_text(tag=UI.CLIENT_SECRET, no_spaces=True, default_value="Your client secret", label="Client Secret")
GUI.add_button(tag=UI.AUTHORIZE, label="Authorize User", callback=AuthorizeUser)
# User's Trakt tokens
GUI.add_spacer(height=20)
GUI.add_separator()
GUI.add_input_text(tag=UI.ACCES_TOKEN, no_spaces=True, enabled=False, label="Access Token")
GUI.add_separator()
GUI.add_input_text(tag=UI.REFRESH_TOKEN, no_spaces=True, enabled=False, label="Refresh Token")
with GUI.group(horizontal=True):
GUI.add_button(tag=UI.REFRESH, label="Refresh Tokens", callback=RefreshUserToken)
GUI.add_button(tag=UI.DELETE, label="Delete Tokens", callback=DeleteUserToken)
# Backup folder selector
GUI.add_spacer(height=20)
GUI.add_input_text(tag=UI.BACKUP_FOLDER, default_value='.', enabled=False, label= "Backup Folder")
GUI.add_button(tag=UI.SELECT_FOLDER, label="Select backup folder", callback=lambda: GUI.show_item(UI.FOLDER_DIALOG))
GUI.add_file_dialog(tag=UI.FOLDER_DIALOG, label="Select backup folder...", directory_selector=True, show=False, callback=SelectBackupFolder, cancel_callback=SelectBackupFolderCancelled, width=700, height=400)
# Console buttons
GUI.add_spacer(height=20)
with GUI.group(horizontal=True):
GUI.add_button(tag=UI.SCROLL, label="Scroll to bottom", callback=ScrollDown)
GUI.add_button(tag=UI.CLEAR, label="Clear", callback=ClearConsole)
# Console logs
with GUI.child_window(tag=UI.CONSOLE_WINDOW, label="Log"):
pass
parser = argparse.ArgumentParser(prog="Back Up Trakt for Me")
parser.add_argument('--no_user_interface', action='store_false', help='disables user interface (use this to backup periodically with an automatic task)')
args = parser.parse_args()
useUI = args.no_user_interface
if (useUI):
BuildUserInterface()
logger = Logger(useUI, "UI")
controller = AppController(useUI)
UpdateUISettings()
UpdateUIYearsCombo()
UpdateHistoryTable()
GUI.setup_dearpygui()
GUI.set_viewport_resizable(True)
GUI.set_viewport_min_width(UI.VIEWPORT_MIN_WIDTH)
GUI.set_viewport_min_height(UI.VIEWPORT_MIN_HEIGHT)
GUI.set_primary_window(UI.MAIN_WINDOW, True)
GUI.show_viewport()
GUI.start_dearpygui()
GUI.destroy_context()
else:
controller = AppController(useUI)
controller.BackupTrakt()