Skip to content

Commit

Permalink
strip 0 dates (#866)
Browse files Browse the repository at this point in the history
  • Loading branch information
aw-was-here committed Jul 2, 2023
1 parent c3f8858 commit 388472f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
36 changes: 24 additions & 12 deletions nowplaying/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
class MetadataProcessors: # pylint: disable=too-few-public-methods
''' Run through a bunch of different metadata processors '''

def __init__(self, config=None):
def __init__(self, config: 'nowplaying.config.ConfigFile' = None):
self.metadata = {}
self.imagecache = None
if config:
Expand Down Expand Up @@ -65,10 +65,7 @@ async def getmoremetadata(self, metadata=None, imagecache=None, skipplugins=Fals
self.metadata['label'] = self.metadata['publisher']
del self.metadata['publisher']

if 'year' in self.metadata:
if 'date' not in self.metadata:
self.metadata['date'] = self.metadata['year']
del self.metadata['year']
self._fix_dates()

if self.metadata.get('artistlongbio') and not self.metadata.get('artistshortbio'):
self._generate_short_bio()
Expand All @@ -79,6 +76,19 @@ async def getmoremetadata(self, metadata=None, imagecache=None, skipplugins=Fals
self._fix_duration()
return self.metadata

def _fix_dates(self):
''' take care of year / date cleanup '''
if not self.metadata:
return

if 'year' in self.metadata:
if 'date' not in self.metadata:
self.metadata['date'] = self.metadata['year']
del self.metadata['year']

if 'date' in self.metadata and (not self.metadata['date'] or self.metadata['date'] == '0'):
del self.metadata['date']

def _fix_duration(self):
if not self.metadata or not self.metadata.get('duration'):
return
Expand Down Expand Up @@ -286,7 +296,7 @@ def _generate_short_bio(self):
class AudioMetadataRunner: # pylint: disable=too-few-public-methods
''' run through audio_metadata '''

def __init__(self, config=None):
def __init__(self, config: 'nowplaying.config.ConfigFile' = None):
self.metadata = {}
self.config = config

Expand Down Expand Up @@ -479,7 +489,9 @@ def _process_audio_metadata(self): # pylint: disable=too-many-branches
self.metadata['coverimageraw'] = base.pictures[0].data


def recognition_replacement(config=None, metadata=None, addmeta=None):
def recognition_replacement(config: 'nowplaying.config.ConfigFile' = None,
metadata=None,
addmeta=None):
''' handle any replacements '''
# if there is nothing in addmeta, then just bail early
if not addmeta:
Expand Down Expand Up @@ -509,13 +521,13 @@ def main():
logging.captureWarnings(True)
bundledir = os.path.abspath(os.path.dirname(__file__))
config = nowplaying.config.ConfigFile(bundledir=bundledir)
metadata = {'filename': sys.argv[1]}
testmeta = {'filename': sys.argv[1]}
myclass = MetadataProcessors(config=config)
metadata = asyncio.run(myclass.getmoremetadata(metadata=metadata))
if 'coverimageraw' in metadata:
testdata = asyncio.run(myclass.getmoremetadata(metadata=testmeta))
if 'coverimageraw' in testdata:
print('got an image')
del metadata['coverimageraw']
print(metadata)
del testdata['coverimageraw']
print(testdata)


if __name__ == "__main__":
Expand Down
22 changes: 22 additions & 0 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,25 @@ async def test_str_duration(bootstrap):
).getmoremetadata(metadata=metadatain
)
assert metadataout['duration'] == 1


@pytest.mark.asyncio
async def test_year_zeronum(bootstrap):
''' automated integration test '''
config = bootstrap
metadatain = {'date': 0}
metadataout = await nowplaying.metadata.MetadataProcessors(config=config
).getmoremetadata(metadata=metadatain
)
assert not metadataout.get('date')


@pytest.mark.asyncio
async def test_year_zerostr(bootstrap):
''' automated integration test '''
config = bootstrap
metadatain = {'date': '0'}
metadataout = await nowplaying.metadata.MetadataProcessors(config=config
).getmoremetadata(metadata=metadatain
)
assert not metadataout.get('date')

0 comments on commit 388472f

Please sign in to comment.