diff --git a/headphones/db.py b/headphones/db.py
index 502195dc..76d7307a 100644
--- a/headphones/db.py
+++ b/headphones/db.py
@@ -54,7 +54,7 @@ class DBConnection:
logger.error('Database error: %s' % e)
raise
except sqlite3.DatabaseError, e:
- logger.error('Fatal Error executing %s :: %s' % (command, e))
+ logger.error('Fatal Error executing %s :: %s' % (query, e))
raise
return sqlResult
diff --git a/headphones/helpers.py b/headphones/helpers.py
index 4b6ea043..ff7bddb9 100644
--- a/headphones/helpers.py
+++ b/headphones/helpers.py
@@ -4,7 +4,7 @@ import datetime
import headphones
-from headphones import db, logger
+from headphones import logger
def multikeysort(items, columns):
@@ -92,42 +92,4 @@ def bytes_to_mb(bytes):
mb = int(bytes)/1048576
size = '%.1f MB' % mb
- return size
-
-def sortNZBList(resultlist, albumid):
- """
- Takes a list of NZBresults from searcher.py and sorts them by distance to
- a target size based on bitrate * album duration
- """
-
- bitrate = headphones.PREFERRED_BITRATE
-
- myDB = db.DBConnection()
- tracks = myDB.select('SELECT TrackDuration from tracks WHERE AlbumID=?', [albumid])
-
- # album length in milliseconds - if there is no track info, return False
- try:
- albumlength = sum([pair[0] for pair in tracks])
- except TypeError:
- return False
-
- # target size, in bytes
- targetsize = albumlength/1000 * bitrate * 128
-
- logger.info('Target size: %s' % bytes_to_mb(targetsize))
-
- newlist = []
- # resultlist = [(title, size, url), (title2, size2, url2)...]
-
- for result in resultlist:
-
- delta = abs(targetsize - result[1])
- newlist.append((result[0], result[1], result[2], delta))
-
- bestqual = sorted(newlist, key=lambda title: title[3])[0]
-
- return bestqual
-
-
-
-
\ No newline at end of file
+ return size
\ No newline at end of file
diff --git a/headphones/importer.py b/headphones/importer.py
index 80163359..0bb3c6ed 100644
--- a/headphones/importer.py
+++ b/headphones/importer.py
@@ -26,21 +26,19 @@ def scanMusic(dir=None):
logger.error('Can not decode file %s. Error: %s' % (str(files), str(e)))
continue
- logger.info(u'%i music files found' % len(results))
+ logger.info(u'%i music files found. Reading metadata....' % len(results))
if results:
- lst = []
- bitrates = []
-
myDB = db.DBConnection()
myDB.action('''DELETE from have''')
for song in results:
try:
f = MediaFile(song)
+ logger.debug('Reading: %s' % song.decode('UTF-8'))
except:
- logger.info("Could not read file: '" + song + "'")
+ logger.warn('Could not read file: %s' % song.decode('UTF-8'))
else:
if f.albumartist:
artist = f.albumartist
@@ -50,17 +48,17 @@ def scanMusic(dir=None):
continue
myDB.action('INSERT INTO have VALUES( ?, ?, ?, ?, ?, ?, ?, ?, ?)', [artist, f.album, f.track, f.title, f.length, f.bitrate, f.genre, f.date, f.mb_trackid])
- lst.append(artist)
- bitrates.append(f.bitrate)
# Get the average bitrate if the option is selected
if headphones.DETECT_BITRATE:
try:
- headphones.PREFERRED_BITRATE = sum(bitrates)/len(bitrates)/1000
- except ZeroDivisionError:
- logger.error('No bitrates found - cannot automatically detect preferred bitrate')
+ avgbitrate = myDB.action("SELECT AVG(BitRate) FROM have").fetchone()[0]
+ headphones.PREFERRED_BITRATE = int(avgbitrate)/1000
+
+ except Exception, e:
+ logger.error('Error detecting preferred bitrate:' + str(e))
- artistlist = {}.fromkeys(lst).keys()
+ artistlist = myDB.action('SELECT DISTINCT ArtistName FROM have').fetchall()
logger.info(u"Preparing to import %i artists" % len(artistlist))
artistlist_to_mbids(artistlist)
@@ -105,7 +103,10 @@ def artistlist_to_mbids(artistlist):
for artist in artistlist:
- results = mb.findArtist(artist, limit=1)
+ results = mb.findArtist(artist['ArtistName'], limit=1)
+
+ if not results:
+ continue
try:
artistid = results[0]['id']
@@ -129,6 +130,9 @@ def addArtisttoDB(artistid):
artist = mb.getArtist(artistid)
+ if not artist:
+ return
+
if artist['artist_name'].startswith('The '):
sortname = artist['artist_name'][4:]
else:
@@ -154,7 +158,14 @@ def addArtisttoDB(artistid):
logger.info('Unable to get release information for %s - it may not be a valid release group' % rg['title'])
continue
+ if not releaseid:
+ continue
+
release = mb.getRelease(releaseid)
+
+ if not release:
+ logger.warn('Unable to get release information for %s. Skipping for now.' % rg['title'])
+ continue
logger.info(u"Now adding/updating album: " + rg['title'])
controlValueDict = {"AlbumID": rg['id']}
diff --git a/headphones/mb.py b/headphones/mb.py
index b4947adb..eacc937e 100644
--- a/headphones/mb.py
+++ b/headphones/mb.py
@@ -34,6 +34,10 @@ def findArtist(name, limit=1):
time.sleep(1)
+ if not artistResults:
+ return False
+
+
for result in artistResults:
artistlist.append({
@@ -67,6 +71,9 @@ def getArtist(artistid):
attempt += 1
time.sleep(1)
+ if not artist:
+ return False
+
time.sleep(1)
artist_dict['artist_name'] = artist.name
@@ -113,6 +120,9 @@ def getReleaseGroup(rgid):
attempt += 1
time.sleep(1)
+ if not releaseGroup:
+ return False
+
time.sleep(1)
# I think for now we have to make separate queries for each release, in order
# to get more detailed release info (ASIN, track count, etc.)
@@ -173,6 +183,9 @@ def getRelease(releaseid):
attempt += 1
time.sleep(1)
+ if not results:
+ return False
+
time.sleep(1)
release['title'] = results.title
diff --git a/headphones/searcher.py b/headphones/searcher.py
index 2fd8bb29..7ae43037 100644
--- a/headphones/searcher.py
+++ b/headphones/searcher.py
@@ -169,12 +169,29 @@ def searchNZB(albumid=None):
if len(resultlist):
if headphones.PREFERRED_QUALITY == 2 and headphones.PREFERRED_BITRATE:
+
+ logger.debug('Target bitrate: %i kbps' % headphones.PREFERRED_BITRATE)
+
+ tracks = myDB.select('SELECT TrackDuration from tracks WHERE AlbumID=?', [albumid])
+
+ try:
+ albumlength = sum([pair[0] for pair in tracks])
+
+ targetsize = albumlength/1000 * headphones.PREFERRED_BITRATE * 128
+ logger.info('Target size: %s' % helpers.bytes_to_mb(targetsize))
+
+ newlist = []
+
+ for result in resultlist:
+ delta = abs(targetsize - result[1])
+ newlist.append((result[0], result[1], result[2], delta))
+
+ bestqual = sorted(newlist, key=lambda title: title[3])[0]
- logger.debug('Sorting results list')
- bestqual = helpers.sortNZBList(resultlist, albumid)
-
- if not bestqual:
+ except TypeError:
+
logger.info('No track information for %s - %s. Defaulting to highest quality' % (albums[0], albums[1]))
+
bestqual = sorted(resultlist, key=lambda title: title[1], reverse=True)[0]
else:
@@ -213,7 +230,7 @@ def searchNZB(albumid=None):
break
myDB.action('UPDATE albums SET status = "Snatched" WHERE AlbumID=?', [albums[2]])
- myDB.action('INSERT INTO snatched VALUES( ?, ?, ?, ?, CURRENT_DATE, ?)', [albums[2], bestqual[0], bestqual[1], bestqual[2], "Snatched"])
+ myDB.action('INSERT INTO snatched VALUES( ?, ?, ?, ?, DATETIME("NOW", "localtime"), ?)', [albums[2], bestqual[0], bestqual[1], bestqual[2], "Snatched"])
elif headphones.BLACKHOLE:
@@ -228,10 +245,4 @@ def searchNZB(albumid=None):
break
myDB.action('UPDATE albums SET status = "Snatched" WHERE AlbumID=?', [albums[2]])
- myDB.action('INSERT INTO snatched VALUES( ?, ?, ?, ?, CURRENT_DATE, ?)', [albums[2], bestqual[0], bestqual[1], bestqual[2], "Snatched"])
-
-
-
-
-
-
\ No newline at end of file
+ myDB.action('INSERT INTO snatched VALUES( ?, ?, ?, ?, DATEIME("NOW", "localtime"), ?)', [albums[2], bestqual[0], bestqual[1], bestqual[2], "Snatched"])
\ No newline at end of file
diff --git a/headphones/webserve.py b/headphones/webserve.py
index 6f708da7..17b81673 100644
--- a/headphones/webserve.py
+++ b/headphones/webserve.py
@@ -289,7 +289,6 @@ class WebInterface(object):
import searcher
threading.Thread(target=searcher.searchNZB, args=[AlbumID]).start()
- time.sleep(5)
raise cherrypy.HTTPRedirect("artistPage?ArtistID=%s" % ArtistID)
@@ -457,11 +456,12 @@ class WebInterface(object):
snatched = myDB.select('''SELECT AlbumID, Title TEXT, Size INTEGER, URL TEXT, DateAdded TEXT, Status TEXT from snatched order by DateAdded DESC''')
page.append('''
+ History clear all
+ |
|
- |
- |
- |
+ |
+ |
''')
if len(snatched) == 0:
page.append("""
""")
@@ -470,10 +470,10 @@ class WebInterface(object):
while i < len(snatched):
mb = snatched[i][2] / 1048576
size = '%.2fM' % mb
- page.append('''| %s |
- %s |
- %s |
+ page.append('''
| %s |
%s |
+ %s |
+ %s |
''' % (snatched[i][5], snatched[i][1], size, snatched[i][4]))
i += 1