-
Notifications
You must be signed in to change notification settings - Fork 7
/
quicknet.py
146 lines (104 loc) · 3.22 KB
/
quicknet.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
139
140
141
142
143
144
145
146
# Copyright (C) 2013-2014
# Sean Poyser (seanpoyser@gmail.com)
#
# 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 XBMC; see the file COPYING. If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
# http://www.gnu.org/copyleft/gpl.html
#
import os
import time
import urllib2
import xbmc
import utils
import sfile
CacheDir = os.path.join(utils.ADDON.getAddonInfo('profile'), 'c')
CacheSize = 100
def clearCache():
sfile.rmtree(CacheDir)
while sfile.exists(CacheDir):
xbmc.sleep(50)
checkCacheDir()
def checkCacheDir():
sfile.makedirs(CacheDir)
def getURLNoCache(url, agent=None, tidy=True):
req = urllib2.Request(url)
if agent:
req.add_header('User-Agent', agent)
response = urllib2.urlopen(req)
html = response.read()
response.close()
if tidy:
html = html.replace('\r', '').replace('\n', '').replace('\t', '')
return html
def getURL(url, maxSec=0, agent=None, tidy=True):
purgeCache()
if url == None:
return None
if maxSec > 0:
timestamp = getTimestamp(url)
if timestamp > 0:
if (time.time() - timestamp) <= maxSec:
return getCachedData(url)
data = getURLNoCache(url, agent, tidy)
addToCache(url, data)
return data
def getTimestamp(url):
cacheKey = createKey(url)
cachePath = os.path.join(CacheDir, cacheKey)
try: return sfile.mtime(cachePath)
except: pass
return 0
def getCachedData(url):
cacheKey = createKey(url)
cachePath = os.path.join(CacheDir, cacheKey)
data = sfile.read(cachePath)
return data
def addToCache(url, data):
checkCacheDir()
cacheKey = createKey(url)
cachePath = os.path.join(CacheDir, cacheKey)
f = sfile.file(cachePath, 'w')
f.write(data)
f.close()
purgeCache()
def createKey(url):
try:
from hashlib import md5
return md5(url).hexdigest()
except:
import md5
return md5.new(url).hexdigest()
def purgeCache():
files = sfile.glob(CacheDir)
nFiles = len(files)
try:
while nFiles > gCacheSize:
oldestFile = getOldestFile(files)
while sfile.exists(oldestFile):
try: sfile.remove(oldestFile)
except: pass
files = sfile.glob(CacheDir)
nFiles = len(files)
except:
pass
def getOldestFile(files):
if not files:
return None
now = time.time()
oldest = (files[0], now - sfile.ctime(files[0]))
for f in files[1:]:
age = now - sfile.ctime(f)
if age > oldest[1]:
oldest = (f, age)
return oldest[0]