requestcache, getting a url will return the data if it has it, fetch and store and return it if not

This commit is contained in:
admin 2026-02-26 13:55:50 -08:00
parent 927bf24b97
commit 6049955715

View File

@ -0,0 +1,36 @@
import requests
import base64
import io
import sys
from pathlib import Path
class RequestCache:
def __init__(self, cache_dir):
self.cache_dir = cache_dir
self.cache = {}
folder = Path(self.cache_dir)
if not folder.is_dir():
folder.mkdir(parents=True)
files_list = [p for p in folder.iterdir() if p.is_file()]
for file in files_list:
fname = file.name
unbase = base64.standard_b64decode(fname)
self.cache[unbase] = fname
def get_cache(self, url):
b64 = base64.urlsafe_b64encode(url)
if b64 in self.cache:
return open(self.cache[b64],'r').read()
else:
try:
response = requests.get(url)
data = response.text
file_path = self.cache_dir / b64
with open(file_path, 'w') as f:
f.write(data)
except Exception as e:
print(f"Error {str(e)}")