Don't send data if not modified
This commit is contained in:
@@ -4,9 +4,12 @@ from pathlib import Path
|
||||
from htmlmin import minify
|
||||
from time import time
|
||||
from datetime import datetime, timedelta
|
||||
from hashlib import md5
|
||||
from json import dumps
|
||||
from musicbrainzngs import get_image_front
|
||||
from flask import (
|
||||
Blueprint,
|
||||
request,
|
||||
render_template,
|
||||
send_file,
|
||||
send_from_directory,
|
||||
@@ -40,6 +43,12 @@ def utmsmp(unix: int) -> str:
|
||||
def rtmsmp(unix: int) -> str:
|
||||
return tmsmp(int(time() - unix))
|
||||
|
||||
def lb_etag():
|
||||
return md5((dumps(listens.get('data')) + dumps(listening.get('data'))).encode()).hexdigest()
|
||||
|
||||
def steam_etag():
|
||||
return md5((dumps(recent.get('data')) + dumps(owned.get('data'))).encode()).hexdigest()
|
||||
|
||||
bp = Blueprint(
|
||||
"risdeveau",
|
||||
__name__,
|
||||
@@ -73,6 +82,8 @@ def mb_cover(mbid):
|
||||
return r
|
||||
|
||||
args = {
|
||||
"lb_etag": lb_etag,
|
||||
"steam_etag": steam_etag,
|
||||
"lb": listens,
|
||||
"lb_now": listening,
|
||||
"recent": recent,
|
||||
@@ -88,4 +99,22 @@ def index():
|
||||
|
||||
@bp.route("/m/<module>")
|
||||
def module(module):
|
||||
if modified_since := request.headers.get('if-modified-since'):
|
||||
modified_since = int(modified_since)
|
||||
none_match = request.headers.get('if-none-match')
|
||||
|
||||
if any((modified_since, none_match)):
|
||||
match module:
|
||||
case "listenbrainz":
|
||||
if modified_since >= int(max((x.get('last_updated', 0) for x in (listens, listening)))):
|
||||
return '', 304
|
||||
if none_match == lb_etag():
|
||||
return '', 304
|
||||
|
||||
case "steam":
|
||||
if modified_since >= int(max((x.get('last_updated', 0) for x in (recent, owned)))):
|
||||
return "Not updated yet", 304
|
||||
if none_match == steam_etag():
|
||||
return '', 304
|
||||
|
||||
return render_tmpl(f'{module}.htm', **args)
|
||||
|
||||
Reference in New Issue
Block a user