forked from primaeval/script.tvguide.fullscreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoplay.py
102 lines (84 loc) · 3.94 KB
/
autoplay.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
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012 Tommy Winther
# http://tommy.winther.nu
#
# Modified for FTV Guide (09/2014 onwards)
# by Thomas Geppert [bluezed] - [email protected]
#
# Modified for TV Guide Fullscreen (2016)
# by primaeval - [email protected]
#
# 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 LICENSE.txt. If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
# http://www.gnu.org/copyleft/gpl.html
#
import datetime
import os
import xbmc
import xbmcgui, xbmcaddon
import source as src
from strings import *
ADDON = xbmcaddon.Addon(id='script.tvguide.fullscreen')
class Autoplay(object):
def __init__(self, database, addonPath):
"""
@param database: source.Database
"""
self.database = database
self.addonPath = addonPath
self.icon = os.path.join(self.addonPath, 'icon.png')
def createAlarmClockName(self, programTitle, startTime):
return 'tvguide-%s-%s' % (programTitle, startTime)
def scheduleAutoplays(self):
for program in self.database.getFullAutoplays():
self._scheduleAutoplay(program.channel.id, program.title, program.startDate, program.endDate)
def _scheduleAutoplay(self, channelId, programTitle, startTime, endTime):
t = startTime - datetime.datetime.now()
timeToAutoplay = ((t.days * 86400) + t.seconds) / 60
if timeToAutoplay < 0:
return
#timeToAutoplay = 1
name = self.createAlarmClockName(programTitle, startTime)
xbmc.executebuiltin('AlarmClock(%s-start,RunScript(special://home/addons/script.tvguide.fullscreen/play.py,%s,%s),%d,True)' %
(name.encode('utf-8', 'replace'), channelId.encode('utf-8'), startTime, timeToAutoplay - int(ADDON.getSetting('autoplays.before'))))
t = endTime - datetime.datetime.now()
timeToAutoplay = ((t.days * 86400) + t.seconds) / 60
#timeToAutoplay = 0
if ADDON.getSetting('autoplays.stop') == 'true':
xbmc.executebuiltin('AlarmClock(%s-stop,RunScript(special://home/addons/script.tvguide.fullscreen/stop.py,%s,%s),%d,True)' %
(name.encode('utf-8', 'replace'), channelId.encode('utf-8'), startTime, timeToAutoplay + int(ADDON.getSetting('autoplays.after'))))
def _unscheduleAutoplay(self, programTitle, startTime):
name = self.createAlarmClockName(programTitle, startTime)
xbmc.executebuiltin('CancelAlarm(%s-start,True)' % name.encode('utf-8', 'replace'))
xbmc.executebuiltin('CancelAlarm(%s-stop,True)' % name.encode('utf-8', 'replace'))
def addAutoplay(self, program,type):
self.database.addAutoplay(program,type)
self._scheduleAutoplay(program.channel.id, program.title, program.startDate, program.endDate)
def removeAutoplay(self, program):
self.database.removeAutoplay(program)
self._unscheduleAutoplay(program.title, program.startDate)
if __name__ == '__main__':
database = src.Database()
def onAutoplaysCleared():
xbmcgui.Dialog().ok(strings(CLEAR_NOTIFICATIONS), strings(DONE)) #TODO
def onInitialized(success):
if success:
database.clearAllAutoplays()
database.close(onAutoplaysCleared)
ADDON.setSetting('playing.channel','')
ADDON.setSetting('playing.start','')
else:
database.close()
database.initialize(onInitialized)