-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.py
206 lines (176 loc) · 9.46 KB
/
common.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
import xbmc
import xbmcaddon
import xbmcgui
import xbmcplugin
import time
msgdialogprogress = xbmcgui.DialogProgress()
addon = xbmcaddon.Addon()
addon_path = addon.getAddonInfo("path")
addon_icon = addon_path + '/resources/icon.png'
notifycount = 0
def translate(text):
return addon.getLocalizedString(text)
def settings(setting, value = None):
# Get or add addon setting
if value:
xbmcaddon.Addon().setSetting(setting, value)
else:
return xbmcaddon.Addon().getSetting(setting)
def playCount(plcount, padjust, plflag, paflag, asevlog): # Calculate play counter
match = 0
if plflag == 0 and paflag == 0: # No file playing or paused
plcount = 0
match = 1
elif padjust == 'None': # Count uninterrupted by pause
plcount += 1
match = 2
elif padjust == 'Pause' and plflag == 1: # Continue counter
plcount += 1
match = 3
elif padjust == 'Pause' and paflag == 1: # Pause counter
plcount = plcount
match = 4
elif padjust == 'Reset' and plflag == 1: # Continue counter
plcount += 1
match = 5
elif padjust == 'Reset' and paflag == 1: # Reset counter
plcount = 0
match = 6
if asevlog == 'true' and plcount % 10 == 0:
xbmc.log('Autostop playCount: ' + str(plcount) + ' ' + padjust + ' ' + \
str(plflag) + ' ' + str(paflag) + ' ' + str(match), xbmc.LOGINFO)
return plcount
def sleepNotify(plcount, plstoptime, plflag, plnotify, plextend, asevlog, paflag):
global notifycount
extime = float(settings('extime'))
notifyset = settings('notifyset')
varextnotify = settings('varextnotify')
totalstoptime = plstoptime + extime
if plstoptime > 0 and plcount + plnotify >= totalstoptime \
* 60 and (plflag > 0 or (plflag == 0 and paflag == 1)): # Set or update notification
xbmc.log('Autostop sleepNotify set notification.', xbmc.LOGDEBUG)
if notifyset == 'no' and varextnotify =='no': # Notifications not set
msgdialogprogress.create(translate(30307), translate(30308))
settings('notifyset', 'yes')
notifycount = 0
xbmc.log('Autostop notify counter started.', xbmc.LOGINFO)
elif notifyset == 'yes': # Update notification
notifycount += 1
if notifycount >= plnotify: # Upper bounds check
percent = 0
tremain = '0'
msgdialogprogress.close()
else:
percent = 100 - int(float(notifycount) / float(plnotify) * 100)
tremain = str(plnotify - notifycount)
message = translate(30308) + tremain + translate(30311)
msgdialogprogress.update(percent, message)
if asevlog == 'true' and plcount % 10 == 0:
xbmc.log('Autostop notify counter: ' + str(plcount) + ' ' + str(plnotify) + ' ' + \
str(totalstoptime * 60) + ' ' + str(percent) + ' ' + str(notifycount) + ' ' + \
str(plstoptime), xbmc.LOGINFO)
if (msgdialogprogress.iscanceled()):
if plextend > 0: # Fixed extension
extime = extime + plextend
settings('extime', str(extime)) # Extend sleep counter
extmins = str(plextend)
else: # Get variable extension time
extmins = varExtension(totalstoptime, plcount, asevlog)
extime = extime + float(extmins)
settings('extime', str(extime))
msgdialogprogress.close()
dialog = xbmcgui.Dialog()
mgenlog = translate(30309) + extmins + translate(30313)
xbmc.log(mgenlog, xbmc.LOGINFO)
dialog.notification(translate(30310), mgenlog, addon_icon, 3000)
settings('notifyset', 'no') # Clear notification flag
elif notifyset == 'yes' and plflag == 0 and paflag == 0:
msgdialogprogress.close()
settings('notifyset', 'no') # Clear notification flag
if plflag > 0 and asevlog == 'true' and plcount % 10 == 0: # Activity logging
xbmc.log('Autostop sleepNotify counter: ' + \
str(plcount + plnotify) + ' ' + str(plstoptime * 60) \
+ ' ' + str(totalstoptime * 60), xbmc.LOGINFO)
if plflag == 0 and paflag == 0 and extime > 0: # Check for playback ended
settings('extime', '0') # Temporary extension clear
def varExtension(plstoptime, plcount, asevlog): # Get variable extension time
pselect = ["10 minutes", "20 minutes", "30 minutes", "40 minutes", \
"50 minutes", "60 minutes", "90 minutes"]
remtime = checkTime(plstoptime, plcount, asevlog) # Get remaining playback time
if float(remtime) > 0:
pselect.extend(["End of current file (" + remtime + " mins)"])
if settings('stopplay') == 'true': # Add stop now playback option
pselect.extend(["[COLOR blue]Stop Playback Now[/COLOR]"])
ddialog = xbmcgui.Dialog()
selection = ddialog.select(translate(30315), pselect)
if selection < 0: # User cancel. Default is 5
extension = '5' # minutes to avoid GUI loop
elif 'minutes' in (pselect[selection]):
extension = (pselect[selection])[:2] # Get minutes from selection
elif 'End' in (pselect[selection]):
extension = checkTime(plstoptime, plcount, asevlog) # Get remaining playback time
elif 'Stop' in (pselect[selection]):
extension = '0'
notifymsg = translate(30310)
logmsg = translate(30318)
stopPlayback(notifymsg, logmsg) # User requested stop playback
return extension
def stopPlayback(notifymsg, logmsg): # Function to stop playback
# log and notify user
try:
if xbmc.Player().isPlayingVideo():
ptag = xbmc.Player().getVideoInfoTag()
ptitle = ptag.getTitle()
elif xbmc.Player().isPlayingAudio():
ptag = xbmc.Player().getMusicInfoTag()
ptitle = ptag.getTitle()
else:
ptitle = "playing file"
pos = xbmc.Player().getTime()
xbmc.Player().stop()
mgenlog = logmsg + ptitle + ' at: ' + time.strftime("%H:%M:%S", \
time.gmtime(pos))
xbmc.log(mgenlog, xbmc.LOGINFO)
dialog = xbmcgui.Dialog()
dialog.notification(notifymsg, mgenlog, addon_icon, 5000)
if settings('screensaver') == 'true': # Active screensaver if option selected
xbmc.executebuiltin('ActivateScreensaver')
if settings('asreset') == 'true': # Reset sleep timer if option selected
settings('plstop', '0')
mgenlog = "Autostop sleep timer reset enabled. Sleep timer reset to 0."
xbmc.log(mgenlog, xbmc.LOGINFO)
settings('notifyset', 'no') # Clear notification flag
settings('varextnotify', 'no') # Clear notification flag
except:
xbmc.log('Autostop error when stopping playback.', xbmc.LOGINFO)
def checkTime(plstoptime, plcount, asevlog): # Calculate remaining playback time
try:
currpos = xbmc.Player().getTime()
endpos = int(xbmc.Player().getTotalTime())
remaintime = endpos - currpos - (plstoptime * 60 - plcount) - \
(10 - plcount % 10) - 1 # 1 second less than end time
if remaintime > 10800: # Maximum 3 hrs
remaintime = 10800
extension = '{:.2f}'.format(remaintime / float(60)) # Calculate to .01 min
if asevlog == 'true':
xbmc.log('Autostop calculating extension time. ' + str(currpos) + ' ' + \
str(endpos) + ' ' + str(plcount) + ' ' + str(remaintime) + ' ' + extension \
+ ' ' + str(plstoptime * 60), xbmc.LOGINFO)
settings('varextnotify', 'yes') # Only display progress bar once
return extension
except:
xbmc.log('Autostop error getting remaining extension time.', xbmc.LOGINFO)
return '-1'
def checkNotify(): # Verify plstop and plnotify are both 10 minutes
try:
plnotify = int(settings('plnotify'))
plstoptime = int(settings('plstop'))
if plnotify == plstoptime * 60:
settings('plnotify', '300') # Stop timer and notification timer cannot be the same
notifymsg = translate(30310)
mgenlog = translate(30321)
xbmc.log(mgenlog, xbmc.LOGINFO)
dialog = xbmcgui.Dialog()
dialog.notification(notifymsg, mgenlog, addon_icon, 5000)
except:
xbmc.log('Autostop error processing checkNotify comparison.', xbmc.LOGINFO)