forked from chriskbchan/plugin.video.hktv-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker.py
138 lines (114 loc) · 4.87 KB
/
tracker.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
# -*- coding: utf-8 -*-
#
import xbmc
import time, threading, urllib2, json
from utils import *
TRACKER = 'hktvTracker'
def goURL(url):
log(xbmc.LOGDEBUG, '[%s] Go URL %s' % (TRACKER, url))
try:
resp = urllib2.urlopen(url)
log(xbmc.LOGDEBUG, '[%s] HTTP status code = %s' % (TRACKER, resp.getcode()))
return
except Exception as e:
log(xbmc.LOGERROR, '[%s] goURL err=%s' % (TRACKER, str(e)))
return
class adsTimer(threading.Thread):
def __init__(self, second, trackers):
super(adsTimer, self).__init__()
self._stopevent = threading.Event()
self._sleepperiod = 1.0
self.timer = second
self.trackers = trackers
log(xbmc.LOGDEBUG, '[%s] [%s] timer for %d sec' % (TRACKER, self.getName(), self.timer))
def run(self):
log(xbmc.LOGDEBUG, '[%s] [%s] timer started' % (TRACKER, self.getName()))
sCount = 0
intv = [ 0, self.timer / 4, self.timer / 2, self.timer / 4 * 3, self.timer ]
tkList = map(list, zip(*self.trackers))
while not self._stopevent.isSet() and sCount <= self.timer:
for i in range(intv.__len__()):
interval = int(intv[i])
#log(xbmc.LOGDEBUG, '[%s] [%s] i=%d, intv=%d, sCount=%s' % (TRACKER, self.getName(), i, intv[i], sCount))
if sCount == interval:
#log(xbmc.LOGDEBUG, '[%s] [%s] match interval' % (TRACKER, self.getName()))
for t in range(tkList[i].__len__()): # per trackers
log(xbmc.LOGDEBUG, '[%s] [%s] interval %d - tracking - %s' % (TRACKER, self.getName(), i, tkList[i][t]))
goURL(tkList[i][t])
sCount += 1
if sCount < self.timer:
self._stopevent.wait(self._sleepperiod)
log(xbmc.LOGDEBUG, '[%s] [%s] timer completed' % (TRACKER, self.getName()))
def join(self, timeout=None):
log(xbmc.LOGDEBUG, '[%s] [%s] timer stopping' % (TRACKER, self.getName()))
self._stopevent.set()
threading.Thread.join(self, timeout)
class hktvTracker(xbmc.Player):
def __init__(self, *args, **kwargs):
xbmc.Player.__init__(self)
self.playing = False
self.adTrack = None
self.url = ''
self.playTime = 0
def onPlayBackStarted(self):
xbmc.sleep(100)
if self.isPlayingVideo():
try:
self.url = self.getPlayingFile()
self.playTime = self.getTotalTime()
log(xbmc.LOGDEBUG, '[%s] playing - %s' % (TRACKER, self.url))
adInfoJson = xbmc.getInfoLabel('VideoPlayer.Plot')
videoType = xbmc.getInfoLabel('VideoPlayer.PlotOutline')
log(xbmc.LOGDEBUG, '[%s] adInfoJson=%s' % (TRACKER, adInfoJson))
log(xbmc.LOGDEBUG, '[%s] videoType=%s' % (TRACKER, videoType))
if adInfoJson:
adInfo = json.loads(adInfoJson)
# get Ad info
if 'imp' in adInfo:
imList = adInfo['imp']
for i in range(imList.__len__()):
log(xbmc.LOGDEBUG, '[%s] impression=%s' % (TRACKER, imList[i]))
goURL(imList[i])
tkList = None
#tkList = [ ['1A', '1B', '1C', '1D', '1E'],
# ['2A', '2B', '2C', '2D', '2E'] ]
if 'track' in adInfo:
tkList = adInfo['track']
# tracker
#if self.playTime > 1 and self.playTime < 60:
if videoType == 'ADS':
if tkList:
self.adTrack = adsTimer(self.playTime, tkList)
self.adTrack.start()
self.playing = True
return
except Exception as e:
log(xbmc.LOGERROR, '[%s] onPlayBackStarted err=%s' % (TRACKER, str(e)))
return
def onPlayBackEnded(self):
try:
log(xbmc.LOGDEBUG, '[%s] ended %s' % (TRACKER, self.url))
self.adTrack.join()
return
except Exception as e:
log(xbmc.LOGERROR, '[%s] onPlayBackEnded err=%s' % (TRACKER, str(e)))
return
def onPlayBackStopped(self):
try:
log(xbmc.LOGDEBUG, '[%s] stopping %s' % (TRACKER, self.url))
self.adTrack.join()
return
except Exception as e:
log(xbmc.LOGERROR, '[%s] onPlayBackStopped err=%s' % (TRACKER, str(e)))
return
# Starting tracker
log(xbmc.LOGINFO, '[%s] Loading HKTV Tracker' % TRACKER)
player = hktvTracker()
while not xbmc.abortRequested:
xbmc.sleep(1000)
log(xbmc.LOGINFO, '[%s] Shutting down ...' % TRACKER)
#
# They tried to bury us.
# They didn't know we were seeds.
# - Mexican Proverb
#