Skip to content

Commit

Permalink
Fix for adding albums with no release date
Browse files Browse the repository at this point in the history
  • Loading branch information
rembo10 committed Feb 8, 2022
1 parent de74cd2 commit d934c86
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
17 changes: 8 additions & 9 deletions headphones/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,17 @@ def now():
return now.strftime("%Y-%m-%d %H:%M:%S")


def get_age(date):
try:
split_date = date.split('-')
except:
def is_valid_date(date):
if not date:
return False
else:
return bool(re.match(r'\d{4}-\d{2}-\d{2}', date))

try:
days_old = int(split_date[0]) * 365 + int(split_date[1]) * 30 + int(split_date[2])
except (IndexError, ValueError):
days_old = False

return days_old
def age(d):
'''Requires a valid date'''
delta = date.today() - date.fromisoformat(d)
return delta.days


def bytes_to_mb(bytes):
Expand Down
12 changes: 11 additions & 1 deletion headphones/helpers_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
from .unittestcompat import TestCase
from headphones.helpers import clean_name
from headphones.helpers import clean_name, is_valid_date, age


class HelpersTest(TestCase):
Expand Down Expand Up @@ -46,3 +46,13 @@ def test_clean_name_nonunicode(self):
self.assertEqual(
test, expected, "check clean_name() with narrow non-ascii input"
)

def test_is_valid_date(date):
test_cases = [
('2021-11-12', True, "check is_valid_date returns True for valid date"),
(None, False, "check is_valid_date returns False for None"),
('2021-11', False, "check is_valid_date returns False for incomplete"),
('2021', False, "check is_valid_date returns False for incomplete")
]
for input, expected, desc in test_cases:
self.assertEqual(is_valid_date(input), expected, desc)
19 changes: 7 additions & 12 deletions headphones/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def addArtisttoDB(artistid, extrasonly=False, forcefull=False, type="artist"):
rgid = rg['id']
skip_log = 0
# Make a user configurable variable to skip update of albums with release dates older than this date (in days)
pause_delta = headphones.CONFIG.MB_IGNORE_AGE
ignore_age = headphones.CONFIG.MB_IGNORE_AGE

rg_exists = myDB.action("SELECT * from albums WHERE AlbumID=?", [rg['id']]).fetchone()

Expand Down Expand Up @@ -279,13 +279,13 @@ def addArtisttoDB(artistid, extrasonly=False, forcefull=False, type="artist"):
release_date = check_release_date + "-12-31"
else:
release_date = today
if helpers.get_age(today) - helpers.get_age(release_date) < pause_delta:
if helpers.age(release_date) < ignore_age:
logger.info("[%s] Now updating: %s (Release Date <%s Days)",
artist['artist_name'], rg['title'], pause_delta)
artist['artist_name'], rg['title'], ignore_age)
new_releases = mb.get_new_releases(rgid, includeExtras, True)
else:
logger.info("[%s] Skipping: %s (Release Date >%s Days)",
artist['artist_name'], rg['title'], pause_delta)
artist['artist_name'], rg['title'], ignore_age)
skip_log = 1
new_releases = 0

Expand Down Expand Up @@ -450,14 +450,9 @@ def addArtisttoDB(artistid, extrasonly=False, forcefull=False, type="artist"):

if headphones.CONFIG.AUTOWANT_ALL:
newValueDict['Status'] = "Wanted"
elif album['ReleaseDate'] > today and headphones.CONFIG.AUTOWANT_UPCOMING:
newValueDict['Status'] = "Wanted"
# Sometimes "new" albums are added to musicbrainz after their release date, so let's try to catch these
# The first test just makes sure we have year-month-day
elif helpers.get_age(album['ReleaseDate']) and helpers.get_age(
today) - helpers.get_age(
album['ReleaseDate']) < 21 and headphones.CONFIG.AUTOWANT_UPCOMING:
newValueDict['Status'] = "Wanted"
elif headphones.CONFIG.AUTOWANT_UPCOMING:
if helpers.is_valid_date(album['ReleaseDate']) and helpers.age(album['ReleaseDate']) < 21:
newValueDict['Status'] = "Wanted"
else:
newValueDict['Status'] = "Skipped"

Expand Down

0 comments on commit d934c86

Please sign in to comment.