-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.py
81 lines (76 loc) · 3.75 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2011-2014 Martijn Kaijser
#
# 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 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
#
# modules
import os
import time
import xbmc
import xbmcaddon
import xbmcvfs
import lib.common
### get addon info
__addon__ = lib.common.__addon__
__addonpath__ = lib.common.__addonpath__
__localize__ = lib.common.__localize__
__addonname__ = lib.common.__addonname__
__version__ = lib.common.__version__
__addonprofile__ = lib.common.__addonprofile__
#import libraries
from lib.settings import get
from lib.utils import log
setting = get()
# starts update/sync
def autostart():
xbmcaddon.Addon().setSetting(id="files_overwrite", value='false')
tempdir = os.path.join(__addonprofile__, 'temp')
service_runtime = str(setting.get('service_runtime') + ':00')
log('## Service - Run at startup: %s'% setting.get('service_startup'), xbmc.LOGDEBUG)
log('## Service - Delayed startup: %s minutes'% setting.get('service_startupdelay'), xbmc.LOGDEBUG)
log('## Service - Run as service: %s'% setting.get('service_enable'), xbmc.LOGDEBUG)
log('## Service - Time: %s'% service_runtime, xbmc.LOGDEBUG)
log("##########........................")
# Check if tempdir exists and remove it
if xbmcvfs.exists(tempdir):
xbmcvfs.rmdir(tempdir)
log('Removing temp folder from previous aborted run.')
xbmc.sleep(5000)
# Run script when enabled and check on existence of tempdir.
# This because it is possible that script was running even when we previously deleted it.
# Could happen when switching profiles and service gets triggered again
if setting.get('service_startup') and not xbmcvfs.exists(tempdir):
xbmc.executebuiltin('XBMC.AlarmClock(ArtworkDownloader,RunScript(script.artwork.downloader,silent=true),00:%s:15,silent)' % setting.get('setting.service_startupdelay'))
if setting.get('service_enable'):
while (not monitor.abortRequested):
xbmc.sleep(5000)
if not(time.strftime('%H:%M') == service_runtime):
pass
else:
if not xbmcvfs.exists(tempdir):
log('Time is %s:%s, Scheduled run starting' % (time.strftime('%H'), time.strftime('%M')))
xbmc.executebuiltin('RunScript(script.artwork.downloader,silent=true)')
# Because we now use the commoncache module the script is run so fast it is possible it is started twice
# within the one minute window. So keep looping until it goes out of that window
while (not monitor.abortRequested and time.strftime('%H:%M') == service_runtime):
xbmc.sleep(5000)
else:
log('Addon already running, scheduled run aborted', xbmc.LOGDEBUG)
if (__name__ == "__main__"):
log("######## Artwork Downloader Service: Initializing........................")
log('## Add-on Name = %s' % str(__addonname__))
log('## Version = %s' % str(__version__))
autostart()