-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcache_control_helper.py
42 lines (32 loc) · 1.54 KB
/
cache_control_helper.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
import calendar
from cachecontrol.heuristics import BaseHeuristic
from datetime import datetime, timedelta
from email.utils import parsedate, formatdate
import requests
from cachecontrol import CacheControl
from cachecontrol.caches.file_cache import FileCache
class CustomHeuristic(BaseHeuristic):
def __init__(self, days: float = ...):
self.days = days
def update_headers(self, response):
date = parsedate(response.headers['date'])
expires = datetime(*date[:6]) + timedelta(days=self.days)
# print(formatdate(calendar.timegm(expires.timetuple())))
return {
'expires': formatdate(calendar.timegm(expires.timetuple())),
'cache-control': 'public',
}
def warning(self, response):
msg = 'Automatically cached! Response is Stale.'
return '110 - "%s"' % msg
class CacheControlHelper(object):
def __init__(self):
self.sess = CacheControl(requests.session(), heuristic=CustomHeuristic(days=30), cache=FileCache('.web_cache'))
self.exceptions = requests.exceptions
def get(self, url, params=None, timeout=120, cookies=None, headers={'Accept': 'application/json'}):
if cookies:
return self.sess.get(url, params=params, timeout=timeout, cookies=cookies, headers=headers)
else:
return self.sess.get(url, params=params, timeout=timeout, headers=headers)
def post(self, url, data, timeout=120, headers={'Accept': 'application/json'}):
return self.sess.post(url, data=data, timeout=timeout, headers=headers)