/
service.py
134 lines (112 loc) · 4.74 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
# -*- coding: utf-8 -*-
import datetime, time
import xbmc
import xbmcaddon
import xbmcvfs
import sqlite3
from rpc import RPC
from xbmcswift2 import Plugin
plugin = Plugin()
def addon_id():
return xbmcaddon.Addon().getAddonInfo('id')
def log(what):
xbmc.log(repr(what),xbmc.LOGERROR)
class KodiPlayer(xbmc.Player):
def __init__(self, *args, **kwargs):
xbmc.Player.__init__(self)
@classmethod
def onPlayBackEnded(self):
pass
@classmethod
def onPlayBackStopped(self):
path = ""
retry = 0
while not path and retry < 50:
path = xbmc.getInfoLabel('ListItem.FileNameAndPath')
retry=retry+1
time.sleep(0.1)
label = xbmc.getInfoLabel('ListItem.Label')
conn = sqlite3.connect(xbmc.translatePath('special://profile/addon_data/%s/replay.db' % addon_id()), detect_types=sqlite3.PARSE_DECLTYPES)
c = conn.cursor()
now = datetime.datetime.now() + datetime.timedelta(microseconds=1) # add 1 microsecond, required for dbapi2
if path:
c.execute("INSERT OR REPLACE INTO links VALUES (?,?,?)", (label.decode("utf8"),path,now))
conn.commit()
conn.close()
def onPlayBackStarted(self):
try:
file = self.getPlayingFile()
log(file)
response = RPC.player.get_item(playerid=1, properties=["title", "year", "thumbnail", "fanart", "showtitle", "season", "episode"])
log(response)
except:
return
item = response["item"]
conn = sqlite3.connect(xbmc.translatePath('special://profile/addon_data/%s/replay.db' % addon_id()), detect_types=sqlite3.PARSE_DECLTYPES)
c = conn.cursor()
now = datetime.datetime.now() + datetime.timedelta(microseconds=1) # add 1 microsecond, required for dbapi2
if file:
c.execute("INSERT OR REPLACE INTO streams VALUES (?,?,?)", (item["label"],file,now))
conn.commit()
conn.close()
conn = sqlite3.connect(xbmc.translatePath('special://profile/addon_data/%s/replay.db' % addon_id()), detect_types=sqlite3.PARSE_DECLTYPES)
c = conn.cursor()
c.execute('CREATE TABLE IF NOT EXISTS streams (title TEXT, file TEXT, date TIMESTAMP, PRIMARY KEY(file))')
c.execute('CREATE TABLE IF NOT EXISTS links (title TEXT, file TEXT, date TIMESTAMP, PRIMARY KEY(file))')
conn.commit()
conn.close()
player_monitor = KodiPlayer()
servicing = False
def Service():
global servicing
if servicing:
return
servicing = True
xbmc.log("[plugin.video.addon.recorder] SERVICE",xbmc.LOGERROR)
xbmc.executebuiltin('XBMC.RunPlugin(plugin://plugin.video.addon.recorder/service)')
time.sleep(2)
servicing = False
if plugin.get_setting('service.startup',bool):
Service()
plugin.set_setting('last.update', str(time.time()))
if plugin.get_setting('service',bool) and plugin.get_setting('service.type',int):
monitor = xbmc.Monitor()
xbmc.log("[plugin.video.addon.recorder] service started...",xbmc.LOGERROR)
while not monitor.abortRequested():
if plugin.get_setting('service.type') == '1':
interval = int(plugin.get_setting('service.interval'))
waitTime = 3600 * interval
ts = plugin.get_setting('last.update') or "0.0"
lastTime = datetime.datetime.fromtimestamp(float(ts))
now = datetime.datetime.now()
nextTime = lastTime + datetime.timedelta(seconds=waitTime)
td = nextTime - now
timeLeft = td.seconds + (td.days * 24 * 3600)
xbmc.log("[plugin.video.addon.recorder] Service waiting for interval %s" % waitTime,xbmc.LOGERROR)
elif plugin.get_setting('service.type') == '2':
next_time = plugin.get_setting('service.time')
if next_time:
hms = next_time.split(':')
hour = hms[0]
minute = hms[1]
now = datetime.datetime.now()
next_time = now.replace(hour=int(hour),minute=int(minute),second=0,microsecond=0)
if next_time < now:
next_time = next_time + datetime.timedelta(hours=24)
td = next_time - now
timeLeft = td.seconds + (td.days * 24 * 3600)
if timeLeft <= 0:
timeLeft = 1
xbmc.log("[plugin.video.addon.recorder] Service waiting for %d seconds" % timeLeft,xbmc.LOGERROR)
if timeLeft and monitor.waitForAbort(timeLeft):
break
xbmc.log("[plugin.video.addon.recorder] Service now triggered...",xbmc.LOGERROR)
Service()
now = time.time()
plugin.set_setting('last.update', str(now))
else:
monitor = xbmc.Monitor()
time.sleep(10)
while not monitor.abortRequested():
monitor.waitForAbort(1)
del player_monitor