forked from rmrector/service.stinger.notification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.py
218 lines (193 loc) · 8.59 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
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
import json
import xbmc
import xbmcaddon
from lib import quickjson
from lib.chapters import ChaptersFile
from lib.notificationwindow import NotificationWindow
DURING_CREDITS_STINGER_MESSAGE = 32000
AFTER_CREDITS_STINGER_MESSAGE = 32001
BOTH_STINGERS_MESSAGE = 32002
DURING_CREDITS_STINGER_TYPE = 32003
AFTER_CREDITS_STINGER_TYPE = 32004
DURING_CREDITS_STINGER_TAG = 'duringcreditsstinger'
AFTER_CREDITS_STINGER_TAG = 'aftercreditsstinger'
BOTH_STINGERS_PROPERTY = DURING_CREDITS_STINGER_TAG + ' ' + AFTER_CREDITS_STINGER_TAG
addon = xbmcaddon.Addon()
def log(message, level=xbmc.LOGDEBUG):
xbmc.log('[service.stinger.notification] {0}'.format(message), level)
class StingerService(xbmc.Monitor):
def __init__(self):
super(StingerService, self).__init__()
self.currentid = None
self.totalchapters = None
self._stingertype = None
self.notified = False
self.externalchapterstart = None
self.get_settings()
def reset(self):
self.currentid = None
self.totalchapters = None
self._stingertype = None
xbmc.executebuiltin('ClearProperty(stinger, fullscreenvideo)')
self.notified = False
self.externalchapterstart = None
def get_settings(self):
self.use_simplenotification = addon.getSetting('use_simplenotification') == 'true'
self.query_chapterdb = addon.getSetting('query_chapterdb') == 'true'
self.preferredfps = addon.getSetting('preferredfps')
self.aftercredits_tag = addon.getSetting('aftercreditsstinger_tag')
self.duringcredits_tag = addon.getSetting('duringcreditsstinger_tag')
try:
self.whereis_theend = int(addon.getSetting('timeremaining_notification'))
except ValueError:
self.whereis_theend = 10
try:
self.notification_visibletime = int(addon.getSetting('notification_visibletime'))
except ValueError:
self.notification_visibletime = 8
@property
def stingertype(self):
return self._stingertype
@stingertype.setter
def stingertype(self, value):
self._stingertype = value
xbmc.executebuiltin('SetProperty(stinger, %s, fullscreenvideo)' % value)
def run(self):
while not self.waitForAbort(5):
if self.currentid and not self.notified:
if self.check_for_display():
self.notify()
def onNotification(self, sender, method, data):
if sender == 'service.stinger.notification' and method == 'Other.TagCheck':
from lib import commander
commander.graball_stingertags()
return
if method not in (('Player.OnStop', 'Player.OnAVStart') if quickjson.get_kodi_version() >= 18
else ('Player.OnPlay', 'Player.OnStop')):
return
data = json.loads(data)
if is_data_onplay_bugged(data, method):
data['item']['id'], data['item']['type'] = hack_onplay_databits()
if not data or 'item' not in data or 'id' not in data['item'] or \
data['item'].get('type') != 'movie' or data['item']['id'] == -1:
return
if method == 'Player.OnStop':
self.reset()
return
if not self.currentid:
self.currentid = data['item']['id']
self.checkstingerinfo()
def checkstingerinfo(self):
movie = quickjson.get_movie_details(self.currentid)
if not movie or 'tag' not in movie or not movie['tag']:
self.stingertype = None
else:
duringcredits = DURING_CREDITS_STINGER_TAG in movie['tag'] or self.duringcredits_tag and self.duringcredits_tag in movie['tag']
aftercredits = AFTER_CREDITS_STINGER_TAG in movie['tag'] or self.aftercredits_tag and self.aftercredits_tag in movie['tag']
if duringcredits and aftercredits:
self.stingertype = BOTH_STINGERS_PROPERTY
elif duringcredits:
self.stingertype = DURING_CREDITS_STINGER_TAG
elif aftercredits:
self.stingertype = AFTER_CREDITS_STINGER_TAG
else:
self.stingertype = None
if not self.stingertype:
self.currentid = None
return
player = xbmc.Player()
title = xbmc.getInfoLabel('Player.Title')
while not title:
if self.waitForAbort(2) or not player.isPlayingVideo():
self.currentid = None
return
title = xbmc.getInfoLabel('Player.Title')
try:
self.totalchapters = int(xbmc.getInfoLabel('Player.ChapterCount'))
except ValueError:
self.totalchapters = None
if not player.isPlayingVideo():
self.currentid = None
return
if not self.totalchapters:
duration = player.getTotalTime()
chapters = ChaptersFile(title, int(duration), self.preferredfps, self.query_chapterdb)
self.externalchapterstart = chapters.lastchapterstart
def check_for_display(self):
if self.totalchapters:
if self.on_lastchapter():
return True
elif self.externalchapterstart:
if self.on_lastexternalchapter():
return True
else:
if self.near_endofmovie():
return True
return False
def on_lastchapter(self):
try:
return int(xbmc.getInfoLabel('Player.Chapter')) == self.totalchapters
except ValueError:
return False
def on_lastexternalchapter(self):
return xbmc.getInfoLabel('Player.Time(hh:mm:ss)') > self.externalchapterstart
def near_endofmovie(self):
player = xbmc.Player()
if not player.isPlayingVideo():
return False
try:
timeremaining = (player.getTotalTime() - player.getTime()) // 60
return timeremaining < self.whereis_theend
except ValueError:
return False
def notify(self):
if self.notified:
return
self.notified = True
message = None
if self.stingertype == DURING_CREDITS_STINGER_TAG:
message = addon.getLocalizedString(DURING_CREDITS_STINGER_MESSAGE)
stingertype = addon.getLocalizedString(DURING_CREDITS_STINGER_TYPE)
elif self.stingertype == AFTER_CREDITS_STINGER_TAG:
message = addon.getLocalizedString(AFTER_CREDITS_STINGER_MESSAGE)
stingertype = addon.getLocalizedString(AFTER_CREDITS_STINGER_TYPE)
elif self.stingertype == BOTH_STINGERS_PROPERTY:
message = addon.getLocalizedString(BOTH_STINGERS_MESSAGE)
stingertype = '{0}, [LOWERCASE]{1}[/LOWERCASE]'.format(addon.getLocalizedString(DURING_CREDITS_STINGER_TYPE), addon.getLocalizedString(AFTER_CREDITS_STINGER_TYPE))
if not message:
return
if self.use_simplenotification:
xbmc.executebuiltin('Notification("{0}", "{1}", {2}, special://home/addons/service.stinger.notification/resources/media/logo.png)'.format(stingertype, message, self.notification_visibletime * 1000))
else:
window = NotificationWindow('script-stinger-notification-Notification.xml', addon.getAddonInfo('path'), 'Default', '1080i')
window.message = message
window.stingertype = stingertype
window.show()
self.waitForAbort(self.notification_visibletime)
window.close()
def onSettingsChanged(self):
self.get_settings()
def is_data_onplay_bugged(data, method):
return 'item' in data and 'id' not in data['item'] and data['item'].get('type') == 'movie' and \
data['item'].get('title') == '' and quickjson.get_kodi_version() >= 17 and method == 'Player.OnPlay'
def hack_onplay_databits():
# HACK: Workaround for Kodi 17 bug, not including the correct info in the notification when played
# from home window or other non-media windows. http://trac.kodi.tv/ticket/17270
# VideoInfoTag can be incorrect immediately after the notification as well, keep trying
if not xbmc.Player().isPlayingVideo(): # But not isPlayingVideo
return -1, ""
mediatype = xbmc.Player().getVideoInfoTag().getMediaType()
count = 0
while not mediatype and count < 10:
xbmc.sleep(200)
if not xbmc.Player().isPlayingVideo():
return -1, ""
mediatype = xbmc.Player().getVideoInfoTag().getMediaType()
count += 1
if not mediatype:
return -1, ""
return xbmc.Player().getVideoInfoTag().getDbId(), mediatype
if __name__ == '__main__':
log('Started', xbmc.LOGINFO)
StingerService().run()
log('Stopped', xbmc.LOGINFO)