From 755567549ccb8daf81338e695bf91a15b7274d60 Mon Sep 17 00:00:00 2001 From: Krateng Date: Sat, 14 Nov 2020 19:42:23 +0100 Subject: [PATCH] Added makeshift pagination to api scrobbles list, fixes GH-52 --- maloja/apis/native_v1.py | 2 +- maloja/database.py | 10 +++++----- maloja/malojauri.py | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/maloja/apis/native_v1.py b/maloja/apis/native_v1.py index ac3cd98..667c3cd 100644 --- a/maloja/apis/native_v1.py +++ b/maloja/apis/native_v1.py @@ -52,7 +52,7 @@ def server_info(): @api.get("scrobbles") def get_scrobbles_external(**keys): - k_filter, k_time, _, k_amount, _ = uri_to_internal(keys) + k_filter, k_time, _, k_amount, _ = uri_to_internal(keys,api=True) ckeys = {**k_filter, **k_time, **k_amount} result = get_scrobbles(**ckeys) diff --git a/maloja/database.py b/maloja/database.py index 544a2a4..2741e26 100644 --- a/maloja/database.py +++ b/maloja/database.py @@ -34,6 +34,7 @@ from collections import namedtuple from threading import Lock import yaml import lru +import math # url handling from importlib.machinery import SourceFileLoader @@ -262,11 +263,10 @@ def api_key_correct(request): def get_scrobbles(**keys): - r = db_query(**{k:keys[k] for k in keys if k in ["artist","artists","title","since","to","within","timerange","associated","track","max_"]}) - #if keys.get("max_") is not None: - # return r[:int(keys.get("max_"))] - #else: - # return r + r = db_query(**{k:keys[k] for k in keys if k in ["artist","artists","title","since","to","within","timerange","associated","track"]}) + offset = (keys.get('page') * keys.get('perpage')) if keys.get('perpage') is not math.inf else 0 + r = r[offset:] + if keys.get('perpage') is not math.inf: r = r[:keys.get('perpage')] return r diff --git a/maloja/malojauri.py b/maloja/malojauri.py index 88d2331..8a55dfd 100644 --- a/maloja/malojauri.py +++ b/maloja/malojauri.py @@ -4,7 +4,7 @@ import urllib import math # this also sets defaults! -def uri_to_internal(keys,forceTrack=False,forceArtist=False): +def uri_to_internal(keys,forceTrack=False,forceArtist=False,api=False): # output: # 1 keys that define the filtered object like artist or track @@ -46,7 +46,7 @@ def uri_to_internal(keys,forceTrack=False,forceArtist=False): #4 - amountkeys = {"page":0,"perpage":100} + amountkeys = {"page":0,"perpage":math.inf if api else 100} # api doesnt paginate per default if "max" in keys: amountkeys["page"],amountkeys["perpage"] = 0, int(keys["max"]) #different max than the internal one! the user doesn't get to disable pagination if "page" in keys: amountkeys["page"] = int(keys["page"])