Handle different release date formats

Some release entries in the MusicBrainz database have different formats
for the date field. In particular, a few have only a year value, while
others have a year and month. This commit introduces a new `parse_date`
function that attempts to parse a string into a date using one of the
known formats. The `release_year` function uses this new function to
parse the date and extract the year, but now also returns `None` if the
date could not be parsed or was not present in the record.
master
Dustin 2018-11-04 11:51:52 -06:00
parent 34eec61179
commit ab14fd15f4
1 changed files with 21 additions and 1 deletions

View File

@ -28,6 +28,13 @@ log = logging.getLogger('ripcd2')
filesystemencoding = sys.getfilesystemencoding() filesystemencoding = sys.getfilesystemencoding()
DATE_FMTS = [
'%Y-%m-%d',
'%Y-%m',
'%Y',
]
DRIVE_OFFSETS = { DRIVE_OFFSETS = {
'ASUS_BW-12B1ST_a': 6, 'ASUS_BW-12B1ST_a': 6,
'ATAPI_iHES212_3': 702, 'ATAPI_iHES212_3': 702,
@ -254,6 +261,16 @@ def get_release_list_from_device(device):
return res['release-list'] return res['release-list']
def parse_date(datestr):
for fmt in DATE_FMTS:
try:
return datetime.datetime.strptime(datestr, fmt)
except ValueError:
continue
else:
raise ValueError('Could not parse date string "{}"'.format(datestr))
def prompt(text, validate=None): def prompt(text, validate=None):
while True: while True:
try: try:
@ -299,7 +316,10 @@ def prompt_select_release(release_list):
def release_year(release): def release_year(release):
return datetime.datetime.strptime(release['date'], '%Y-%m-%d').year try:
return parse_date(release['date']).year
except (KeyError, ValueError):
return None
def safe_name(name): def safe_name(name):