antizapret fix by inpos

pull/2/head
DiMartinoXBMC 2016-03-12 20:45:29 +03:00
parent 89d23556bb
commit 55cb8797e2
1 changed files with 32 additions and 24 deletions

View File

@ -14,43 +14,52 @@ if not os.path.exists(CACHE_DIR):
CACHE = 24 * 3600 # 24 hour caching CACHE = 24 * 3600 # 24 hour caching
@contextmanager #@contextmanager
def shelf(filename, ttl=0): def shelf(filename, ttl=0):
import shelve import shelve
filename = os.path.join(CACHE_DIR, filename) filename = os.path.join(CACHE_DIR, filename)
with LOCKS.get(filename, threading.RLock()): with LOCKS.get(filename, threading.RLock()):
with closing(shelve.open(filename, writeback=True)) as d: # with closing(shelve.open(filename, writeback=True)) as d:
d = shelve.open(filename, writeback=True)
try:
import time import time
if not d: if not dict(d):
d.update({ d.update({
"created_at": time.time(), "created_at": time.time(),
"data": {}, "data": {},
}) })
elif ttl > 0 and (time.time() - d["created_at"]) > ttl: elif ttl > 0 and (time.time() - d["created_at"]) > ttl:
d["created_at"] = time.time()
d["data"] = {} d["data"] = {}
yield d["data"] return d
except:
d.close()
raise
_config = {} _config = {}
def config(): def config():
global _config global _config
if not _config: if not _config:
with shelf("antizapret.pac_config", ttl=CACHE) as pac_config: # with shelf("antizapret.pac_config", ttl=CACHE) as pac_config:
if not pac_config: d = shelf("antizapret.pac_config2", ttl=CACHE)
log("[antizapret]: Fetching Antizapret PAC file on %s" %PAC_URL) pac_config = d['data']
try: if not pac_config:
pac_data = urllib2.urlopen(PAC_URL).read() log("[antizapret]: Fetching Antizapret PAC file on %s" %PAC_URL)
except: try:
pac_data = "" pac_data = urllib2.urlopen(PAC_URL).read()
except:
pac_data = ""
r = re.search(r"\"PROXY (.*); DIRECT", pac_data) r = re.search(r"\"PROXY (.*); DIRECT", pac_data)
if r: if r:
pac_config["server"] = r.group(1) pac_config["server"] = r.group(1)
pac_config["domains"] = map(lambda x: x.replace(r"\Z(?ms)", "").replace("\\", ""), map(fnmatch.translate, re.findall(r"\"(.*?)\",", pac_data))) pac_config["domains"] = map(lambda x: x.replace(r"\Z(?ms)", "").replace("\\", ""), map(fnmatch.translate, re.findall(r"\"(.*?)\",", pac_data)))
else: else:
pac_config["server"] = None pac_config["server"] = None
pac_config["domains"] = [] pac_config["domains"] = []
_config = pac_config d.close()
_config = pac_config
return _config return _config
class AntizapretProxyHandler(urllib2.ProxyHandler, object): class AntizapretProxyHandler(urllib2.ProxyHandler, object):
@ -96,4 +105,3 @@ def url_get(url, params={}, headers={}, post = None):
except urllib2.HTTPError as e: except urllib2.HTTPError as e:
log("[antizapret]: HTTP Error(%s): %s" % (e.errno, e.strerror)) log("[antizapret]: HTTP Error(%s): %s" % (e.errno, e.strerror))
return None return None