-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
117 lines (88 loc) · 2.89 KB
/
utils.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
# -*- coding: utf-8 -*-
import xbmc, xbmcaddon, xbmcvfs
import os, time, datetime, sys, json
UTF8 = 'utf-8'
addon = xbmcaddon.Addon()
__addonid__ = addon.getAddonInfo('id')
__addonname__ = addon.getAddonInfo('name')
__addonicon__ = addon.getAddonInfo('icon')
cacheSec = int(addon.getSetting('cachesec'))
def log(txt):
message = '[%s]: %s' % (__addonid__, txt.encode('ascii', 'ignore'))
xbmc.log(msg=message, level=xbmc.LOGDEBUG)
def logerr(txt):
message = '[%s]: %s' % (__addonid__, txt.encode('ascii', 'ignore'))
xbmc.log(msg=message, level=xbmc.LOGERROR)
def loginfo(txt):
message = '[%s]: %s' % (__addonid__, txt.encode('ascii', 'ignore'))
xbmc.log(msg=message, level=xbmc.LOGINFO)
def dequote(u):
try:
u = urllib.unquote_plus(u)
except:
pass
return u
def boolean(s):
if s == 'true':
return True
else:
return False
def popup(txt):
xbmc.executebuiltin('Notification(%s, %s, %d, %s)' % (__addonname__, txt.encode(UTF8), 5000, __addonicon__))
def cacheSave(fn, data, cacheTime=cacheSec, forceClear=False):
log('Save cache: %s' % fn)
path = os.path.dirname(os.path.abspath(fn))
if not os.path.exists(path):
log('Create path: %s' % path)
xbmcvfs.mkdirs(path)
if xbmcvfs.exists(fn):
eTime = int(xbmcvfs.Stat(fn).st_mtime()) + cacheTime
cTime = int(time.time())
log('cTime=%s, eTime=%s' % (cTime, eTime))
if cTime > eTime:
log('Cache reset: expired')
xbmcvfs.delete(fn)
elif forceClear:
log('Cache reset: force clear')
xbmcvfs.delete(fn)
try:
fh = xbmcvfs.File(fn, 'w')
fh.write(data)
fh.close()
log('Cache saved')
except Exception as e:
logerr('Error saving cache %s, err=%s' % (fn, e))
def cacheLoad(fn, cacheTime=cacheSec):
log('Load cache: %s' % fn)
data = None
if xbmcvfs.exists(fn):
eTime = int(xbmcvfs.Stat(fn).st_mtime()) + cacheTime
cTime = int(time.time())
log('cTime=%s, eTime=%s' % (cTime, eTime))
if cTime > eTime:
log('Cache reset: expired')
xbmcvfs.delete(fn)
else:
fh = xbmcvfs.File(fn)
data = fh.read()
fh.close()
return data
def xbmcJsonRequest(params):
data = json.dumps(params)
req = xbmc.executeJSONRPC(data)
resp = json.loads(req)
try:
if 'result' in resp:
return resp['result']
return None
except KeyError:
logerr('[%s] %s' % (params['method'], resp['error']['message']))
return None
def secondSinceEpoch(dtime):
epoch = datetime.datetime(1970,1,1,8,0,0) # assume HK GMT+8
td = dtime - epoch
if sys.version_info >= (2, 7):
t = td.total_seconds()
else:
t = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
return int(t)