mirror of
https://github.com/alvierahman90/mobile-chemguide.git
synced 2024-10-22 12:31:52 +00:00
closes #1 cache pages when retrieved
This commit is contained in:
parent
d99e8589fd
commit
1cea4a7b1c
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
__pycache__
|
||||
env
|
11
docker-compose.py
Normal file
11
docker-compose.py
Normal file
@ -0,0 +1,11 @@
|
||||
version: '3'
|
||||
|
||||
services:
|
||||
web:
|
||||
build: .
|
||||
ports:
|
||||
- '80:80'
|
||||
db:
|
||||
image: redis:latest
|
||||
volumes:
|
||||
- ./:/data
|
@ -7,6 +7,7 @@ idna==2.10
|
||||
itsdangerous==1.1.0
|
||||
Jinja2==2.11.3
|
||||
MarkupSafe==1.1.1
|
||||
redis==3.5.3
|
||||
requests==2.25.1
|
||||
urllib3==1.26.4
|
||||
Werkzeug==1.0.1
|
||||
|
39
src/app.py
39
src/app.py
@ -1,16 +1,28 @@
|
||||
from flask import Flask
|
||||
import requests
|
||||
import re
|
||||
import redis
|
||||
import time
|
||||
|
||||
app = Flask(__name__)
|
||||
CHEMGUIDE_BASE="https://chemguide.co.uk/"
|
||||
PAGE_CACHE_DURATION = 3600 # cache pages for an hour before retrieving again
|
||||
db = redis.Redis(host='db', port=6379, db=0)
|
||||
|
||||
@app.route('/')
|
||||
@app.route('/<path:path>')
|
||||
def path(path='/'):
|
||||
class PathKeyType():
|
||||
Data = 1
|
||||
Date = 2
|
||||
|
||||
def pathkey(path, type):
|
||||
if type == PathKeyType.Data:
|
||||
return f"{path}:data"
|
||||
elif type == PathKeyType.Date:
|
||||
return f"{path}:date"
|
||||
|
||||
def set_cache(path):
|
||||
r = requests.get(CHEMGUIDE_BASE + '/' + path)
|
||||
try:
|
||||
return re.sub(
|
||||
data = re.sub(
|
||||
r'<table .* width="480"',
|
||||
'<table style="max-width: 60em; margin: 0 auto; font-size: 16px;"',
|
||||
re.sub(
|
||||
@ -41,7 +53,24 @@ def path(path='/'):
|
||||
)
|
||||
)
|
||||
except UnicodeDecodeError:
|
||||
return r.content
|
||||
data = r.content
|
||||
|
||||
db.set(pathkey(path, PathKeyType.Data), data)
|
||||
db.set(pathkey(path, PathKeyType.Date), time.time())
|
||||
|
||||
def get_page(path):
|
||||
date = db.get(pathkey(path, PathKeyType.Date))
|
||||
if date is not None:
|
||||
if float(str(date, 'utf-8')) + PAGE_CACHE_DURATION < time.time():
|
||||
set_cache(path)
|
||||
else:
|
||||
set_cache(path)
|
||||
return db.get(pathkey(path, PathKeyType.Data))
|
||||
|
||||
@app.route('/')
|
||||
@app.route('/<path:path>')
|
||||
def path(path='/'):
|
||||
return get_page(path)
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', debug=True)
|
||||
|
Loading…
Reference in New Issue
Block a user