2017-11-09 17:17:32 -05:00
|
|
|
import requests
|
|
|
|
from cachecontrol.wrapper import CacheControl
|
|
|
|
from cachecontrol.cache import BaseCache
|
|
|
|
from cachecontrol.heuristics import LastModified
|
|
|
|
from datetime import datetime
|
|
|
|
from django.core.cache import cache as django_cache
|
|
|
|
from hashlib import sha256
|
|
|
|
from mf2py import Parser
|
|
|
|
|
|
|
|
|
|
|
|
class DjangoCache(BaseCache):
|
|
|
|
@classmethod
|
|
|
|
def key(cls, url):
|
2023-08-10 02:52:37 -04:00
|
|
|
return "req:" + sha256(url.encode("utf-8")).hexdigest()
|
2017-11-09 17:17:32 -05:00
|
|
|
|
|
|
|
def get(self, url):
|
|
|
|
key = self.key(url)
|
|
|
|
return django_cache.get(key)
|
|
|
|
|
|
|
|
def set(self, url, value, expires=None):
|
|
|
|
key = self.key(url)
|
|
|
|
if expires:
|
|
|
|
lifetime = (expires - datetime.utcnow()).total_seconds()
|
|
|
|
django_cache.set(key, value, lifetime)
|
|
|
|
else:
|
|
|
|
django_cache.set(key, value)
|
|
|
|
|
2017-11-16 05:52:42 -05:00
|
|
|
def delete(self, url):
|
|
|
|
key = self.key(url)
|
|
|
|
django_cache.delete(key)
|
|
|
|
|
2017-11-09 17:17:32 -05:00
|
|
|
|
|
|
|
req = CacheControl(
|
|
|
|
requests.Session(),
|
|
|
|
cache=DjangoCache(),
|
|
|
|
heuristic=LastModified(),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def get(url):
|
|
|
|
r = req.get(url)
|
|
|
|
r.raise_for_status()
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
|
def mf2(url):
|
|
|
|
r = get(url)
|
2023-08-10 02:52:37 -04:00
|
|
|
return Parser(doc=r.text, url=url, html_parser="html5lib")
|