From af9b45075d57c0dc8f4610a4275f77abc73751f1 Mon Sep 17 00:00:00 2001 From: David Rieger Date: Tue, 19 May 2015 15:58:49 +0200 Subject: [PATCH] sort results and give more songs when searching for time --- storagemanager.py | 24 +++++++++++++++++++----- web.py | 4 ++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/storagemanager.py b/storagemanager.py index 50be911..8057834 100644 --- a/storagemanager.py +++ b/storagemanager.py @@ -8,6 +8,7 @@ import redis from time import gmtime, strftime, strptime +from collections import OrderedDict import hashlib @@ -20,16 +21,29 @@ def __init__(self, host="localhost", port="6379", db="0"): def get_song(self, time=None): """ - Returns the song that was played at a certain time + Returns the songs that were played arount the given time sorted by time or the one that is currently plyed if no time given """ if time is None: return self._get_stored() for t in self._get_all_times(): - # Return the song with the first time that is equal to or smaller than the given + SCOPE = 5 + # Return the songs that was played is closest but before or exactly to/at the given time + # and the numer of SCOPE songs before and after this one if strptime(t, "%d.%m.%Y %H:%M:%S") <= strptime(time, "%d.%m.%Y %H:%M:%S"): - return {t: self._get_stored(hashname=t)} + + # index of this time in the list + _idx = self._get_all_times().index(t) + + _songs = OrderedDict() + for _timekey in self._get_all_times()[_idx-SCOPE:_idx+SCOPE]: + try: + _songs[_timekey] = self._get_stored(hashname=_timekey) + except IndexError: + pass + + return _songs def add_song(self, song): @@ -72,9 +86,9 @@ def _get_stored(self, hashname=None): def get_all_stored(self): """ - Returns all stored songs + Returns all stored songs sorted by time """ - _all_songs = dict() + _all_songs = OrderedDict() for t in self._get_all_times(): _all_songs[t] = self._get_stored(t) return _all_songs diff --git a/web.py b/web.py index 81be25f..2df65ae 100755 --- a/web.py +++ b/web.py @@ -11,6 +11,7 @@ sm = StorageManager() + @route('/api/get/all') def get_all_songs(): response.headers['Access-Control-Allow-Origin'] = '*' @@ -23,3 +24,6 @@ def get_song(time): return sm.get_song(time) app = bottle.default_app() + +if __name__ == '__main__': + run(host="localhost", port=8080)