Skip to content

Commit

Permalink
sort results and give more songs when searching for time
Browse files Browse the repository at this point in the history
  • Loading branch information
David Rieger committed May 19, 2015
1 parent ebd5ad7 commit af9b450
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
24 changes: 19 additions & 5 deletions storagemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import redis
from time import gmtime, strftime, strptime
from collections import OrderedDict
import hashlib


Expand All @@ -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):

Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions web.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

sm = StorageManager()


@route('/api/get/all')
def get_all_songs():
response.headers['Access-Control-Allow-Origin'] = '*'
Expand All @@ -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)

0 comments on commit af9b450

Please sign in to comment.