forked from jmooremcc/plugin.program.super.favourites
/
player.py
216 lines (156 loc) · 5.81 KB
/
player.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
#
# Copyright (C) 2014-
# Sean Poyser (seanpoyser@gmail.com)
#
# 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 XBMC; 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 xbmc
import xbmcgui
import xbmcaddon
import favourite
import utils
ADDON = utils.ADDON
ADDONID = utils.ADDONID
FRODO = utils.FRODO
PLAYMEDIA_MODE = utils.PLAYMEDIA_MODE
ACTIVATEWINDOW_MODE = utils.ACTIVATEWINDOW_MODE
RUNPLUGIN_MODE = utils.RUNPLUGIN_MODE
ACTION_MODE = utils.ACTION_MODE
SHOWPICTURE_MODE = utils.SHOWPICTURE_MODE
PLAY_PLAYLISTS = ADDON.getSetting('PLAY_PLAYLISTS') == 'true'
def getParentCommand(cmd):
parents = []
import re
try:
plugin = re.compile('plugin://(.+?)/').search(cmd.replace('?', '/')).group(1)
md5 = utils.generateMD5(plugin)
if md5 not in parents:
return None
if xbmc.getCondVisibility('System.HasAddon(%s)' % plugin) == 1:
return 'plugin://%s' % plugin
except Exception as e:
pass
return None
def processParentCommand(cmd):
parent = getParentCommand(cmd)
if not parent:
return
xbmc.executebuiltin('Container.Update(%s)' % parent)
while not xbmc.getInfoLabel('Container.FolderPath').startswith(parent):
xbmc.sleep(50)
def playCommand(originalCmd, contentMode=False):
try:
xbmc.executebuiltin('Dialog.Close(busydialog)') #Isengard fix
cmd = favourite.tidy(originalCmd)
if cmd.lower().startswith('executebuiltin'):
cmd = cmd.replace('"', '')
cmd = cmd.lower()
cmd = cmd.replace('"', '')
cmd = cmd.replace('executebuiltin(', '')
if cmd.endswith('))'):
cmd = cmd[:-1]
if cmd.endswith(')') and '(' not in cmd:
cmd = cmd[:-1]
#if a 'Super Favourite' favourite just do it
#if ADDONID in cmd:
# return xbmc.executebuiltin(cmd)
#if in contentMode just do it
if contentMode:
xbmc.executebuiltin('ActivateWindow(Home)') #some items don't play nicely if launched from wrong window
if cmd.lower().startswith('activatewindow'):
cmd = cmd.replace('")', '",return)') #just in case return is missing
return xbmc.executebuiltin(cmd)
if cmd.startswith('RunScript'):
#workaround bug in Frodo that can cause lock-up
#when running a script favourite
if FRODO:
xbmc.executebuiltin('ActivateWindow(Home)')
if PLAY_PLAYLISTS:
import playlist
if playlist.isPlaylist(cmd):
return playlist.play(cmd)
if 'ActivateWindow' in cmd:
return activateWindowCommand(cmd)
if 'PlayMedia' in cmd:
return playMedia(originalCmd)
xbmc.executebuiltin(cmd)
except Exception as e:
utils.log('Error in playCommand')
utils.log('Command: %s' % cmd)
utils.log('Error: %s' % str(e))
def activateWindowCommand(cmd):
cmds = cmd.split(',', 1)
#special case for filemanager
if '10003' in cmds[0] or 'filemanager' in cmds[0].lower():
xbmc.executebuiltin(cmd)
return
plugin = None
activate = None
pluginArgs = None
if len(cmds) == 1:
activate = cmds[0]
else:
activate = cmds[0]+',return)'
plugin = cmds[1][:-1]
try:
pluginArgs = plugin.split('/?',1)[1]
except: pass
#check if it is a different window and if so activate it
id = str(xbmcgui.getCurrentWindowId())
if id not in activate:
xbmc.executebuiltin(activate)
if plugin and not pluginArgs is None:
try:
if "2Fcategories" in cmd:
xbmc.executebuiltin(cmd)
else:
xbmc.executebuiltin('RunPlugin(%s)' % plugin)
except Exception as e:
utils.log(str(e))
else: # call plugin without args
xbmc.executebuiltin(cmd+',"refresh"') # good for single plugin launch
def playMedia(original):
import re
cmd = favourite.tidy(original) #.replace(',', '') #remove spurious commas
processParentCommand(cmd)
try: mode = int(favourite.getOption(original, 'mode'))
except: mode = 0
if mode == PLAYMEDIA_MODE:
xbmc.executebuiltin(cmd)
return
plugin = re.compile('"(.+?)"').search(cmd).group(1)
if mode == SHOWPICTURE_MODE:
xbmc.executebuiltin('ShowPicture(%s)' % plugin)
return
if len(plugin) < 1:
xbmc.executebuiltin(cmd)
return
if mode == ACTIVATEWINDOW_MODE:
try: winID = int(favourite.getOption(original, 'winID'))
except: winID = 10025
#check if it is a different window and if so activate it
id = xbmcgui.getCurrentWindowId()
if id != winID :
xbmc.executebuiltin('ActivateWindow(%d)' % winID)
cmd = 'Container.Update(%s)' % plugin
xbmc.executebuiltin(cmd)
return
if mode == RUNPLUGIN_MODE:
cmd = 'RunPlugin(%s)' % plugin
xbmc.executebuiltin(cmd)
return
#if all else fails just execute it
xbmc.executebuiltin(cmd)