-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathservice.py
123 lines (96 loc) · 4.76 KB
/
service.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
# -*- coding: utf-8 -*-
import os
import xbmc
import xbmcaddon
import xbmcvfs
import json
ADDON = xbmcaddon.Addon()
ADDON_ID = ADDON.getAddonInfo('id')
ADDON_PATH = xbmc.translatePath(ADDON.getAddonInfo('path'))
ADDON_PATH_DATA = xbmc.translatePath(os.path.join('special://profile/addon_data/', ADDON_ID)).replace('\\', '/') + '/'
ADDON_PATH_LIB = os.path.join(ADDON_PATH, 'resources', 'lib' )
ADDON_LANG = ADDON.getLocalizedString
sys.path.append(ADDON_PATH_LIB)
import debug
profiles = ['1', '2', '3', '4']
map_type = { 'movie': 'auto_movies', 'video': 'auto_videos', 'episode': 'auto_tvshows', 'channel': 'auto_pvr', 'musicvideo': 'auto_musicvideo', 'song': 'auto_music', 'unknown': 'auto_unknown' }
susppend_auto_change = False
set_for_susspend = None
class Monitor(xbmc.Monitor):
def __init__(self):
xbmc.Monitor.__init__(self)
# default for kodi start
self.changeProfile(ADDON.getSetting('auto_default'))
def onNotification(self, sender, method, data):
global susppend_auto_change
global set_for_susspend
data = json.loads(data)
if 'System.OnWake' in method:
debug.debug("[MONITOR] METHOD: " + str(method) + " DATA: " + str(data))
# default for kodi wakeup
self.changeProfile(ADDON.getSetting('auto_default'))
if 'Player.OnStop' in method:
debug.debug("[MONITOR] METHOD: " + str(method) + " DATA: " + str(data))
# gui
susppend_auto_change = False
self.changeProfile(ADDON.getSetting('auto_gui'))
if 'Player.OnPlay' in method:
debug.debug("[MONITOR] METHOD: " + str(method) + " DATA: " + str(data))
# auto show dialog
if 'true' in ADDON.getSetting('player_show'):
xbmc.executebuiltin('XBMC.RunScript(' + ADDON_ID + ', popup)')
# auto switch
if 'item' in data and 'type' in data['item']:
type = data['item']['type']
set = map_type.get(type)
# if video is not from library assign to auto_videos
if 'movie' in type and 'id' not in data['item']:
set = 'auto_videos'
# distinguish pvr TV and pvr RADIO
if 'channel' in type and 'channeltype' in data['item']:
if 'tv' in data['item']['channeltype']:
set = 'auto_pvr_tv'
elif 'radio' in data['item']['channeltype']:
set = 'auto_pvr_radio'
else:
set = None
# detect cdda that kodi return as unknown
if 'unknown' in type and 'player' in data and 'playerid' in data['player']:
jsonS = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "id": "1", "method": "Player.GetItem", "params": {"playerid": ' + str(data['player']['playerid']) + ', "properties": ["file"]}}')
jsonR = json.loads(unicode(jsonS, 'utf-8', errors='ignore'))
file = ''
try: file = jsonR['result']['item']['file']
except: pass
if file.startswith('cdda://'):
set = 'auto_music'
debug.debug("[MONITOR] Setting parsed: " + str(set))
# cancel susspend auto change when media type change
if set != set_for_susspend:
susppend_auto_change = False
set_for_susspend = set
if set is not None:
self.changeProfile(ADDON.getSetting(set))
susppend_auto_change = True
def changeProfile(self, profile):
if profile in profiles:
# get last loaded profile
lastProfile = self.getLastProfile()
debug.debug("[MONITOR] Last loaded profile: " + lastProfile + " To switch profile: " + profile)
if lastProfile != profile and susppend_auto_change is not True:
xbmc.executebuiltin('XBMC.RunScript(' + ADDON_ID + ', ' + profile + ')')
else:
debug.debug("[MONITOR] Switching omitted (same profile) or switching is susspend")
def getLastProfile(self):
try:
f = xbmcvfs.File(ADDON_PATH_DATA + 'profile')
p = f.read()
f.close()
if p in profiles:
return p
else:
return ''
except:
return ''
monitor = Monitor()
while(not xbmc.abortRequested):
xbmc.sleep(100)