Skip to content

Commit

Permalink
Changed id3 tag reader
Browse files Browse the repository at this point in the history
  • Loading branch information
philborman committed Feb 11, 2018
1 parent 46f2923 commit 97b1338
Show file tree
Hide file tree
Showing 6 changed files with 1,103 additions and 765 deletions.
5 changes: 4 additions & 1 deletion lazylibrarian/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,7 @@ def config_write(part=None):
else:
# keep the old value
value = CFG.get(section, key.lower())
CONFIG[key] = value
# if CONFIG['LOGLEVEL'] > 2:
# logger.debug("Leaving %s unchanged (%s)" % (key, value))

Expand All @@ -880,7 +881,7 @@ def config_write(part=None):
value = unaccented_str(value)

CFG.set(section, key.lower(), value)
CONFIG[key] = value


# sanity check for typos...
for key in list(CONFIG.keys()):
Expand Down Expand Up @@ -1012,6 +1013,8 @@ def config_write(part=None):
logger.warn(msg)

if not msg:
if part is None:
part = ''
msg = 'Config file [%s] %s has been updated' % (CONFIGFILE, part)
logger.info(msg)

Expand Down
30 changes: 21 additions & 9 deletions lazylibrarian/bookwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
is_valid_booktype, check_int, getList, replace_all, makeUnicode, makeBytestr
from lib.fuzzywuzzy import fuzz
from lib.six.moves.urllib_parse import quote_plus, urlencode
import lib.id3reader as id3reader
try:
from lib.tinytag import TinyTag
except ImportError:
TinyTag = None


# Need to remove characters we don't want in the filename BEFORE adding to EBOOK_DIR
Expand Down Expand Up @@ -53,23 +56,29 @@ def audioRename(bookid):
logger.debug("Invalid bookid in audioRename %s" % bookid)
return ''

if not TinyTag:
logger.warn("TinyTag library not available")
return ''

cnt = 0
parts = []
author = ''
book = ''
track = ''
total = ''
audio_file = ''
for f in os.listdir(makeBytestr(r)):
f = makeUnicode(f)
if is_valid_booktype(f, booktype='audiobook'):
cnt += 1
audio_file = f
try:
id3r = id3reader.Reader(os.path.join(r, f))
performer = id3r.get_value('performer')
composer = id3r.get_value('TCOM')
book = id3r.get_value('album')
track = id3r.get_value('track')
id3r = TinyTag.get(os.path.join(r, f))
performer = id3r.artist
composer = id3r.composer
book = id3r.album
track = id3r.track
total = id3r.track_total

if not track:
track = '0'
Expand All @@ -80,7 +89,7 @@ def audioRename(bookid):
if author and book:
parts.append([track, book, author, f])
except Exception as e:
logger.debug("id3reader %s %s" % (type(e).__name__, str(e)))
logger.debug("tinytag %s %s" % (type(e).__name__, str(e)))
pass

logger.debug("%s found %s audiofile%s" % (exists['BookName'], cnt, plural(cnt)))
Expand All @@ -92,8 +101,11 @@ def audioRename(bookid):
logger.warn("%s: Incorrect number of parts (found %i from %i)" % (exists['BookName'], len(parts), cnt))
return book_filename

# does the track include total (eg 1/12)
if '/' in track:
if check_int(total, 0) and check_int(total, 0) != cnt:
logger.warn("%s: Reported %s parts, got %i" % (exists['BookName'], b, cnt))
return book_filename

if '/' in track: # does the track include total (eg 1/12)
a, b = track.split('/')
if check_int(b, 0) and check_int(b, 0) != cnt:
logger.warn("%s: Expected %s parts, got %i" % (exists['BookName'], b, cnt))
Expand Down
59 changes: 32 additions & 27 deletions lazylibrarian/librarysync.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@

from lib.six.moves.urllib_parse import quote_plus, urlencode

import lib.id3reader as id3reader
try:
from lib.tinytag import TinyTag
except ImportError:
TinyTag = None

try:
import zipfile
Expand Down Expand Up @@ -512,30 +515,34 @@ def LibraryScan(startdir=None, library='eBook', authid=None, remove=True):
if PY2:
filename = filename.encode(lazylibrarian.SYS_ENCODING)
id3r = None
try:
id3r = id3reader.Reader(filename)
performer = id3r.get_value('performer')
composer = id3r.get_value('TCOM')
if composer: # if present, should be author
author = composer
elif performer: # author, or narrator if composer == author
author = performer
else:
author = None
if author and type(author) is list:
lst = ', '.join(author)
logger.debug("id3reader author list [%s]" % lst)
author = author[0] # if multiple authors, just use the first one
book = id3r.get_value('album')
if author and book:
match = True
author = makeUnicode(author)
book = makeUnicode(book)
except Exception as e:
logger.debug("id3reader error %s %s [%s]" % (type(e).__name__, str(e), filename))
if id3r and int(lazylibrarian.LOGLEVEL) > 2:
logger.debug(id3r.dump())
pass
if TinyTag:
try:
id3r = TinyTag.get(filename)
performer = id3r.artist
composer = id3r.composer
book = id3r.album
if lazylibrarian.LOGLEVEL > 2:
logger.debug("id3r.filename [%s]" % filename)
logger.debug("id3r.performer [%s]" % performer)
logger.debug("id3r.composer [%s]" % composer)
logger.debug("id3r.album [%s]" % book)
if composer: # if present, should be author
author = composer
elif performer: # author, or narrator if composer == author
author = performer
else:
author = None
if author and type(author) is list:
lst = ', '.join(author)
logger.debug("id3reader author list [%s]" % lst)
author = author[0] # if multiple authors, just use the first one
if author and book:
match = True
author = makeUnicode(author)
book = makeUnicode(book)
except Exception as e:
logger.debug("tinytag error %s %s [%s]" % (type(e).__name__, str(e), filename))
pass

# Failing anything better, just pattern match on filename
if not match:
Expand All @@ -557,8 +564,6 @@ def LibraryScan(startdir=None, library='eBook', authid=None, remove=True):
author = makeUnicode(author)
if len(book) > 2 and len(author) > 2:
match = True
author = makeUnicode(author)
book = makeUnicode(book)
else:
match = False
if not match:
Expand Down
20 changes: 10 additions & 10 deletions lazylibrarian/versioncheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

from lazylibrarian import logger, version
from lazylibrarian.common import USER_AGENT, proxyList
from lazylibrarian.formatter import check_int
from lazylibrarian.formatter import check_int, makeUnicode


def logmsg(level, msg):
Expand Down Expand Up @@ -67,19 +67,19 @@ def runGit(args):

cmd = cur_git + ' ' + args

cmd = makeUnicode(cmd)
try:
logmsg('debug', '(RunGit)Trying to execute: "' + cmd + '" with shell in ' + lazylibrarian.PROG_DIR)
logmsg('debug', '(RunGit)Trying to execute: "%s" with shell in %s' % (cmd, lazylibrarian.PROG_DIR))
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
shell=True, cwd=lazylibrarian.PROG_DIR)
output, err = p.communicate()
if not PY2:
if output:
# noinspection PyArgumentList
output = str(output, "utf-8")
if err:
# noinspection PyArgumentList
err = str(err, "utf-8")
logmsg('debug', '(RunGit)Git output: [%s]' % output.strip('\n'))

if output:
output = makeUnicode(output).strip('\n')
if err:
err = makeUnicode(err)

logmsg('debug', '(RunGit)Git output: [%s]' % output)

except OSError:
logmsg('debug', '(RunGit)Command ' + cmd + ' didn\'t work, couldn\'t find git')
Expand Down
Loading

0 comments on commit 97b1338

Please sign in to comment.