-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddon.py
More file actions
196 lines (191 loc) · 10.1 KB
/
addon.py
File metadata and controls
196 lines (191 loc) · 10.1 KB
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
#/*
# * Copyright (C) 2013 Joost Kop
# *
# *
# * This Program is free software; you can redistribute it and/or modify
# * it under the terms of the GNU General Public License as published by
# * the Free Software Foundation; either version 2, or (at your option)
# * any later version.
# *
# * This Program is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# * GNU General Public License for more details.
# *
# * You should have received a copy of the GNU General Public License
# * along with this program; see the file COPYING. If not, write to
# * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
# * http://www.gnu.org/copyleft/gpl.html
# *
# */
import xbmcplugin
import xbmcgui
import time
from resources.lib.utils import *
from resources.lib.dropboxclient import XBMCDropBoxClient, Downloader
from resources.lib.dropboxfilebrowser import DropboxFileBrowser
from resources.lib.sync.notifysync import NotifySyncClient
import resources.lib.login as login
if ( __name__ == "__main__" ):
log_debug('Argument List: %s' % str(sys.argv))
runAsScript, params = parse_argv()
if not runAsScript:
if int(sys.argv[1]) < 0:
#handle action of a file (or a "Show me more..." item)
if 'media_items' in params:
#Loading more media items requested...
path = sys.argv[0] + sys.argv[2]
#xbmc.executebuiltin('container.update(%s, replace)'%path) # don't use replace because that removes the content_type from the path...
xbmc.executebuiltin('container.update(%s)'%path)
elif 'module' in params: # plugin (module) to run
path = sys.argv[0] + sys.argv[2]
xbmc.executebuiltin('container.update(%s)'%path)
else:
if 'module' in params: # Module chosen, load and execute module
module = params['module']
__import__(module)
current_module = sys.modules[module]
current_module.run(params)
elif 'action' in params and params['action'] == 'play':
account_name = urllib.parse.unquote( params.get('account', '') )
account_settings = login.get_account(account_name)
if account_settings:
client = XBMCDropBoxClient(access_token=account_settings.access_token)
item = urllib.parse.unquote( urllib.parse.unquote( params['path'] ) )
url = client.getMediaUrl(item)
log_debug('MediaUrl: %s'%url)
listItem = xbmcgui.ListItem(item)
listItem.select(True)
listItem.setPath(url)
xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=listItem)
else:
log_error("Action play: no account name provided!")
xbmcplugin.endOfDirectory(int(sys.argv[1]), succeeded=False)
else: # No module chosen
#Run the browse_folder module
module = 'browse_account'
params['module'] = module
__import__(module)
current_module = sys.modules[module]
current_module.run(params)
else: # run as script
account_name = urllib.parse.unquote( params.get('account', '') )
account_settings = login.get_account(account_name)
if account_settings:
#All actions below require an XBMCDropBoxClient
client = XBMCDropBoxClient(access_token=account_settings.access_token)
action = params.get('action', '')
if action == 'delete':
if 'path' in params:
path = urllib.parse.unquote( params['path'] )
dialog = xbmcgui.Dialog()
if dialog.yesno(ADDON_NAME, LANGUAGE_STRING(30023), path ) == True:
success = client.delete(path)
if success:
log('File removed: %s' % path)
NotifySyncClient().sync_path(account_settings, path)
else:
log_error('File removed Failed: %s' % path)
xbmc.executebuiltin('container.Refresh()')
elif action == 'copy':
if 'path' in params:
path = urllib.parse.unquote( params['path'] )
dialog = DropboxFileBrowser("FileBrowser.xml", ADDON_PATH)
dialog.setDBClient(client)
dialog.setHeading(LANGUAGE_STRING(30025) + LANGUAGE_STRING(30026))
dialog.doModal()
if dialog.selectedFolder:
#dropbox path -> don't use os.path.join()!
toPath = dialog.selectedFolder
if dialog.selectedFolder[-1:] != DROPBOX_SEP: toPath += DROPBOX_SEP
toPath += os.path.basename(path)
success = client.copy(path, toPath)
if success:
log('File copied: %s to %s' % (path, toPath) )
NotifySyncClient().sync_path(account_settings, toPath)
else:
log_error('File copy Failed: %s to %s' % (path, toPath) )
del dialog
elif action == 'move':
if 'path' in params:
path = urllib.parse.unquote( params['path'] )
dialog = DropboxFileBrowser("FileBrowser.xml", ADDON_PATH)
dialog.setDBClient(client)
dialog.setHeading(LANGUAGE_STRING(30025) + LANGUAGE_STRING(30028))
dialog.doModal()
if dialog.selectedFolder:
#dropbox path -> don't use os.path.join()!
toPath = dialog.selectedFolder
if dialog.selectedFolder[-1:] != DROPBOX_SEP: toPath += DROPBOX_SEP
toPath += os.path.basename(path)
success = client.move(path, toPath)
if success:
log('File moved: from %s to %s' % (path, toPath) )
xbmc.executebuiltin('container.Refresh()')
NotifySyncClient().sync_path(account_settings, path)
NotifySyncClient().sync_path(account_settings, toPath)
else:
log_error('File move Failed: from %s to %s' % (path, toPath) )
del dialog
elif action == 'create_folder':
if 'path' in params:
path = urllib.parse.unquote( params['path'] )
keyboard = xbmc.Keyboard('', LANGUAGE_STRING(30030))
keyboard.doModal()
if keyboard.isConfirmed():
newFolder = path
if path[-1:] != DROPBOX_SEP: newFolder += DROPBOX_SEP
newFolder += str(keyboard.getText(), "utf-8")
success = client.createFolder(newFolder)
if success:
log('New folder created: %s' % newFolder)
xbmc.executebuiltin('container.Refresh()')
NotifySyncClient().sync_path(account_settings, newFolder)
else:
log_error('Creating new folder Failed: %s' % newFolder)
elif action == 'upload':
if 'to_path' in params:
toPath = urllib.parse.unquote( params['to_path'] )
dialog = xbmcgui.Dialog()
fileName = dialog.browse(1, LANGUAGE_STRING(30032), 'files')
if fileName:
success = client.upload(fileName, toPath, dialog=True)
if success:
log('File uploaded: %s to %s' % (fileName, toPath) )
xbmc.executebuiltin('container.Refresh()')
NotifySyncClient().sync_path(account_settings, toPath)
else:
log_error('File uploading Failed: %s to %s' % (fileName, toPath))
elif action == 'download':
if 'path' in params:
path = urllib.parse.unquote( params['path'] )
isDir = ('true' == params['isDir'].lower())
dialog = xbmcgui.Dialog()
location = dialog.browse(3, LANGUAGE_STRING(30025) + LANGUAGE_STRING(30038), 'files')
if location:
success = True
downloader = Downloader(client, path, location, isDir)
downloader.start()
#now wait for the FileLoader
downloader.stopWhenFinished = True
while downloader.isAlive():
xbmc.sleep(100)
#Wait for the thread
downloader.join()
if downloader.canceled:
log('Downloading canceled')
else:
log('Downloading finished')
dialog = xbmcgui.Dialog()
dialog.ok(ADDON_NAME, LANGUAGE_STRING(30040), location)
elif action == 'sync_now':
path = urllib.parse.unquote( params['path'] )
NotifySyncClient().sync_path(account_settings, path)
else:
log_error('Unknown action received: %s' % (action))
else:
log_error("Run as script: no account name provided!")
dialog = xbmcgui.Dialog()
dialog.ok(ADDON_NAME, LANGUAGE_STRING(30203))
#script So sys.argv[1] id not a handle... Don't execute the next!
#xbmcplugin.endOfDirectory(int(sys.argv[1]), succeeded=False)