fixed iteration counter, int casting of timestamps, altered model reprs

This commit is contained in:
aj 2019-10-06 09:17:31 +01:00
parent 977384729a
commit 13120b0a47
4 changed files with 39 additions and 12 deletions

View File

@ -27,7 +27,7 @@ class Album(LastFM):
return f'{self.name} / {self.artist}' return f'{self.name} / {self.artist}'
def __repr__(self): def __repr__(self):
return Color.DARKCYAN + Color.BOLD + 'Album' + Color.END + f': {self.artist} ' + super().__repr__() return Color.DARKCYAN + Color.BOLD + 'Album' + Color.END + f': {self.name} {self.artist} ' + super().__repr__()
@staticmethod @staticmethod
def wrap(name: str = None, def wrap(name: str = None,

View File

@ -23,4 +23,4 @@ class Artist(LastFM):
return f'{self.name}' return f'{self.name}'
def __repr__(self): def __repr__(self):
return Color.PURPLE + Color.BOLD + 'Artist ' + Color.END + super().__repr__() return Color.PURPLE + Color.BOLD + 'Artist' + Color.END + f': {self.name}' + super().__repr__()

View File

@ -31,7 +31,7 @@ class Track(LastFM):
def __repr__(self): def __repr__(self):
return Color.YELLOW + Color.BOLD + 'Track' + Color.END + \ return Color.YELLOW + Color.BOLD + 'Track' + Color.END + \
f': album({repr(self.album)}), artist({repr(self.artist)}) ' + super().__repr__() f': {self.name} album({repr(self.album)}), artist({repr(self.artist)}) ' + super().__repr__()
@staticmethod @staticmethod
def wrap(name: str = None, def wrap(name: str = None,

View File

@ -2,7 +2,7 @@ import requests
from typing import Optional, List from typing import Optional, List
from copy import deepcopy from copy import deepcopy
import logging import logging
from datetime import datetime from datetime import datetime, date, time, timedelta
from fmframework.model.fm import Scrobble, Wiki from fmframework.model.fm import Scrobble, Wiki
from fmframework.model.track import Track from fmframework.model.track import Track
@ -75,9 +75,9 @@ class Network:
} }
if from_time is not None: if from_time is not None:
params['from'] = from_time.timestamp() params['from'] = int(from_time.timestamp())
if to_time is not None: if to_time is not None:
params['to'] = to_time.timestamp() params['to'] = int(to_time.timestamp())
iterator = PageCollection(net=self, method='user.getrecenttracks', params=params, response_limit=limit) iterator = PageCollection(net=self, method='user.getrecenttracks', params=params, response_limit=limit)
iterator.response_limit = limit + 1 if limit is not None else None iterator.response_limit = limit + 1 if limit is not None else None
@ -90,10 +90,37 @@ class Network:
return [self.parse_scrobble(i) for i in items[:limit]] return [self.parse_scrobble(i) for i in items[:limit]]
def get_scrobbles_from_date(self,
input_date: date,
username: str = None,
limit: int = None) -> Optional[List[Scrobble]]:
logger.info(f'getting {input_date} scrobbles for {self.username if username is None else username}')
midnight = time(hour=0, minute=0, second=0)
from_date = datetime.combine(date=input_date, time=midnight)
to_date = datetime.combine(date=input_date + timedelta(days=1), time=midnight)
scrobbles = self.get_recent_tracks(username=username, from_time=from_date, to_time=to_date, limit=limit)
return scrobbles
def get_scrobble_count_from_date(self,
input_date: date,
username: str = None,
limit: int = None) -> int:
logger.info(f'getting {input_date} scrobble count for {self.username if username is None else username}')
scrobbles = self.get_scrobbles_from_date(input_date=input_date, username=username, limit=limit)
if scrobbles:
return len(scrobbles)
else:
return 0
def get_track(self, def get_track(self,
name: str, name: str,
artist: str, artist: str,
username: str = None): username: str = None) -> Optional[Track]:
logger.info(f'getting {name} / {artist} for {self.username if username is None else username}') logger.info(f'getting {name} / {artist} for {self.username if username is None else username}')
params = { params = {
@ -112,7 +139,7 @@ class Network:
def get_album(self, def get_album(self,
name: str, name: str,
artist: str, artist: str,
username: str = None): username: str = None) -> Optional[Album]:
logger.info(f'getting {name} / {artist} for {self.username if username is None else username}') logger.info(f'getting {name} / {artist} for {self.username if username is None else username}')
params = { params = {
@ -130,7 +157,7 @@ class Network:
def get_artist(self, def get_artist(self,
name: str, name: str,
username: str = None): username: str = None) -> Optional[Artist]:
logger.info(f'getting {name} for {self.username if username is None else username}') logger.info(f'getting {name} for {self.username if username is None else username}')
params = { params = {
@ -173,7 +200,7 @@ class Network:
wiki=self.parse_wiki(album_dict['wiki']) if album_dict.get('wiki', None) else None, wiki=self.parse_wiki(album_dict['wiki']) if album_dict.get('wiki', None) else None,
artist=album_dict.get('artist')) artist=album_dict.get('artist'))
def parse_track(self, track_dict): def parse_track(self, track_dict) -> Track:
track = Track(name=track_dict.get('name', 'n/a'), track = Track(name=track_dict.get('name', 'n/a'),
url=track_dict.get('url', 'n/a'), url=track_dict.get('url', 'n/a'),
mbid=track_dict.get('mbid', 'n/a'), mbid=track_dict.get('mbid', 'n/a'),
@ -191,7 +218,7 @@ class Network:
return track return track
@staticmethod @staticmethod
def parse_scrobble(scrobble_dict): def parse_scrobble(scrobble_dict) -> Scrobble:
album = None album = None
if scrobble_dict.get('album', None): if scrobble_dict.get('album', None):
album = Album(name=scrobble_dict['album'].get('#text', 'n/a'), album = Album(name=scrobble_dict['album'].get('#text', 'n/a'),
@ -228,7 +255,7 @@ class PageCollection:
self.pages: List[Page] = [] self.pages: List[Page] = []
self.page_limit = page_limit self.page_limit = page_limit
self.response_limit = response_limit self.response_limit = response_limit
self.counter = 1 self.counter = 0
def __len__(self): def __len__(self):
length = 0 length = 0