diff --git a/data/css/style.css b/data/css/style.css index 6ddd34c8..75925e84 100644 --- a/data/css/style.css +++ b/data/css/style.css @@ -148,7 +148,7 @@ a.blue { color: blue; } a.green { - color: #629632; + color: #3ee343; } a.externalred { color: red; diff --git a/headphones/__init__.py b/headphones/__init__.py index 48481490..6c769da7 100644 --- a/headphones/__init__.py +++ b/headphones/__init__.py @@ -385,7 +385,7 @@ def dbcheck(): conn=sqlite3.connect(DB_FILE) c=conn.cursor() - c.execute('CREATE TABLE IF NOT EXISTS artists (ArtistID TEXT UNIQUE, ArtistName TEXT, ArtistSortName TEXT, DateAdded TEXT, Status TEXT, IncludeExtras INTEGER)') + c.execute('CREATE TABLE IF NOT EXISTS artists (ArtistID TEXT UNIQUE, ArtistName TEXT, ArtistSortName TEXT, DateAdded TEXT, Status TEXT, IncludeExtras INTEGER, LatestAlbum TEXT, ReleaseDate TEXT, AlbumID TEXT, HaveTracks INTEGER, TotalTracks INTEGER)') c.execute('CREATE TABLE IF NOT EXISTS albums (ArtistID TEXT, ArtistName TEXT, AlbumTitle TEXT, AlbumASIN TEXT, ReleaseDate TEXT, DateAdded TEXT, AlbumID TEXT UNIQUE, Status TEXT, Type TEXT)') c.execute('CREATE TABLE IF NOT EXISTS tracks (ArtistID TEXT, ArtistName TEXT, AlbumTitle TEXT, AlbumASIN TEXT, AlbumID TEXT, TrackTitle TEXT, TrackDuration, TrackID TEXT)') c.execute('CREATE TABLE IF NOT EXISTS snatched (AlbumID TEXT, Title TEXT, Size INTEGER, URL TEXT, DateAdded TEXT, Status TEXT)') @@ -396,6 +396,31 @@ def dbcheck(): except sqlite3.OperationalError: c.execute('ALTER TABLE artists ADD COLUMN IncludeExtras INTEGER DEFAULT 0') + try: + c.execute('SELECT LatestAlbum from artists') + except sqlite3.OperationalError: + c.execute('ALTER TABLE artists ADD COLUMN LatestAlbum TEXT') + + try: + c.execute('SELECT ReleaseDate from artists') + except sqlite3.OperationalError: + c.execute('ALTER TABLE artists ADD COLUMN ReleaseDate TEXT') + + try: + c.execute('SELECT AlbumID from artists') + except sqlite3.OperationalError: + c.execute('ALTER TABLE artists ADD COLUMN AlbumID TEXT') + + try: + c.execute('SELECT HaveTracks from artists') + except sqlite3.OperationalError: + c.execute('ALTER TABLE artists ADD COLUMN HaveTracks INTEGER DEFAULT 0') + + try: + c.execute('SELECT TotalTracks from artists') + except sqlite3.OperationalError: + c.execute('ALTER TABLE artists ADD COLUMN TotalTracks INTEGER DEFAULT 0') + try: c.execute('SELECT Type from albums') except sqlite3.OperationalError: diff --git a/headphones/importer.py b/headphones/importer.py index 9ef8eed1..721af6ac 100644 --- a/headphones/importer.py +++ b/headphones/importer.py @@ -93,9 +93,9 @@ def is_exists(artistid): # See if the artist is already in the database artistlist = myDB.select('SELECT ArtistID, ArtistName from artists WHERE ArtistID=?', [artistid]) - + if any(artistid in x for x in artistlist): - logger.info(artistlist[0][1] + u" is already in the database, skipping") + logger.info(artistlist[0][1] + u" is already in the database. Updating 'have tracks', but not artist information") return True else: return False @@ -117,9 +117,19 @@ def artistlist_to_mbids(artistlist): logger.info('MusicBrainz query turned up no matches for: %s' % artist) continue + # Add to database if it doesn't exist if artistid != various_artists_mbid and not is_exists(artistid): addArtisttoDB(artistid) - + + # Update track count regardless of whether it already exists + if artistid != various_artists_mbid: + + myDB = db.DBConnection() + havetracks = len(myDB.select('SELECT TrackTitle from have WHERE ArtistName like ?', [artist['ArtistName']])) + + controlValueDict = {"ArtistID": artistid} + newValueDict = {"HaveTracks": havetracks} + myDB.upsert("artists", newValueDict, controlValueDict) def addArtisttoDB(artistid, extrasonly=False): @@ -216,8 +226,19 @@ def addArtisttoDB(artistid, extrasonly=False): myDB.upsert("tracks", newValueDict, controlValueDict) + latestalbum = myDB.action('SELECT AlbumTitle, ReleaseDate, AlbumID from albums WHERE ArtistID=? order by ReleaseDate DESC', [artistid]).fetchone() + totaltracks = len(myDB.select('SELECT TrackTitle from tracks WHERE ArtistID=?', [artistid])) + controlValueDict = {"ArtistID": artistid} - newValueDict = {"Status": "Active"} + + if latestalbum: + newValueDict = {"Status": "Active", + "LatestAlbum": latestalbum['AlbumTitle'], + "ReleaseDate": latestalbum['ReleaseDate'], + "AlbumID": latestalbum['AlbumID'], + "TotalTracks": totaltracks} + else: + newValueDict = {"Status": "Active"} myDB.upsert("artists", newValueDict, controlValueDict) logger.info(u"Updating complete for: " + artist['artist_name']) \ No newline at end of file diff --git a/headphones/webserve.py b/headphones/webserve.py index 686ff6b5..95a09dbd 100644 --- a/headphones/webserve.py +++ b/headphones/webserve.py @@ -33,9 +33,8 @@ class WebInterface(object): page.append(templates._logobar) page.append(templates._nav) myDB = db.DBConnection() - results = myDB.select('SELECT ArtistName, ArtistID, Status from artists order by ArtistSortName collate nocase') + results = myDB.select('SELECT ArtistName, ArtistID, Status, LatestAlbum, ReleaseDate, AlbumID, TotalTracks, HaveTracks from artists order by ArtistSortName collate nocase') if len(results): - i = 0 page.append('''
@@ -43,40 +42,45 @@ class WebInterface(object): ''') - while i < len(results): - latestalbum = myDB.select('SELECT AlbumTitle, ReleaseDate, DateAdded, AlbumID from albums WHERE ArtistID=? order by ReleaseDate DESC', [results[i][1]]) - totaltracks = len(myDB.select('SELECT TrackTitle from tracks WHERE ArtistID=?', [results[i][1]])) - havetracks = len(myDB.select('SELECT TrackTitle from have WHERE ArtistName like ?', [results[i][0]])) + for artist in results: + totaltracks = artist['TotalTracks'] + havetracks = artist['HaveTracks'] + if not havetracks: + havetracks = 0 try: percent = (havetracks*100.0)/totaltracks if percent > 100: percent = 100 - except ZeroDivisionError: - percent = 100 - today = datetime.date.today() - if len(latestalbum) > 0: - if latestalbum[0][1] > datetime.date.isoformat(today): - newalbumName = '%s' % (latestalbum[0][3], latestalbum[0][0]) - releaseDate = '(%s)' % latestalbum[0][1] + except (ZeroDivisionError, TypeError): + percent = 0 + totaltracks = '?' + + if artist['LatestAlbum']: + if artist['ReleaseDate'] > helpers.today(): + newalbumName = '%s' % (artist['AlbumID'], artist['LatestAlbum']) + releaseDate = '(%s)' % artist['ReleaseDate'] else: - newalbumName = '%s' % (latestalbum[0][3], latestalbum[0][0]) + newalbumName = '%s' % (artist['AlbumID'], artist['LatestAlbum']) releaseDate = "" - if len(latestalbum) == 0: + else: newalbumName = 'None' releaseDate = "" - if results[i][2] == 'Paused': - newStatus = '''%s(resume)''' % (results[i][2], results[i][1]) - elif results[i][2] == 'Loading': + + if artist['Status'] == 'Paused': + newStatus = '''%s(resume)''' % (artist['Status'], artist['ArtistID']) + elif artist['Status'] == 'Loading': newStatus = '''Loading...''' else: - newStatus = '''%s(pause)''' % (results[i][2], results[i][1]) + newStatus = '''%s(pause)''' % (artist['Status'], artist['ArtistID']) + page.append(''' - ''' % (results[i][1], results[i][0], results[i][1], results[i][1], newStatus, newalbumName, releaseDate, percent, havetracks, totaltracks)) - i = i+1 + ''' % (artist['ArtistID'], artist['ArtistName'], artist['ArtistID'], + artist['ArtistID'], newStatus, newalbumName, releaseDate, + percent, havetracks, totaltracks)) page.append('''
Artist NameUpcoming Albums Have
%s (link) [delete] %s %s %s
%s/%s
''') page.append(templates._footer % headphones.CURRENT_VERSION) @@ -484,13 +488,18 @@ class WebInterface(object): page.append(templates._logobar) page.append(templates._nav) page.append('''

''') - fileHandle = open(os.path.join(headphones.LOG_DIR, 'headphones.log')) - lineList = fileHandle.readlines() - fileHandle.close() - i = -1 - while i > -100: - page.append(lineList[i] + '

') - i -= 1 + if os.path.isfile(os.path.join(headphones.LOG_DIR, 'headphones.log')): + fileHandle = open(os.path.join(headphones.LOG_DIR, 'headphones.log')) + lineList = fileHandle.readlines() + fileHandle.close() + i = -1 + if len(lineList) < 100: + limit = -len(lineList) + else: + limit = -100 + while i > limit: + page.append(lineList[i] + '

') + i -= 1 page.append('''

''') page.append(templates._footer % headphones.CURRENT_VERSION) return page