Add option to sort by last modification date (close #88)

Store the metadate “last-modified” for a track and an album and add an
option sort by it.
This commit is contained in:
coderkun 2023-01-08 19:09:19 +01:00
parent 5618c9016c
commit 8607bf15a9
10 changed files with 84 additions and 26 deletions

View file

@ -3,6 +3,7 @@
import concurrent.futures
import configparser
import dateutil.parser
import logging
import os
import queue
@ -937,6 +938,8 @@ class Client(Base):
track.set_date(song['date'])
if 'albumartist' in song:
track.set_albumartists(song['albumartist'])
if 'last-modified' in song:
track.set_last_modified(song['last-modified'])
return track
@ -998,6 +1001,7 @@ class MCGAlbum:
self._host = host
self._tracks = []
self._length = 0
self._last_modified = None
self._id = Utils.generate_id(title)
@ -1057,6 +1061,9 @@ class MCGAlbum:
path = os.path.dirname(track.get_file())
if path not in self._pathes:
self._pathes.append(path)
if track.get_last_modified():
if not self._last_modified or track.get_last_modified() > self._last_modified:
self._last_modified = track.get_last_modified()
def get_tracks(self):
@ -1067,6 +1074,10 @@ class MCGAlbum:
return self._length
def get_last_modified(self):
return self._last_modified
def filter(self, filter_string):
if len(filter_string) == 0:
return True
@ -1102,6 +1113,8 @@ class MCGAlbum:
value_function = "get_title"
elif criterion == SortOrder.YEAR:
value_function = "get_date"
elif criterion == SortOrder.MODIFIED:
value_function = "get_last_modified"
value1 = getattr(album1, value_function)()
value2 = getattr(album2, value_function)()
@ -1137,6 +1150,7 @@ class MCGTrack:
self._track = None
self._length = 0
self._date = None
self._last_modified = None
def __eq__(self, other):
@ -1208,6 +1222,18 @@ class MCGTrack:
return self._file
def set_last_modified(self, date_string):
if date_string:
try:
self._last_modified = dateutil.parser.isoparse(date_string)
except ValueError as e:
self._logger.debug("Invalid date format: %s", date_string)
def get_last_modified(self):
return self._last_modified
class MCGPlaylistTrack(MCGTrack):