Skip to content

Commit

Permalink
Merge pull request #1216 from philborman/master
Browse files Browse the repository at this point in the history
New id3 tag library
  • Loading branch information
philborman authored Feb 11, 2018
2 parents b53cbb1 + 97b1338 commit ae14d00
Show file tree
Hide file tree
Showing 10 changed files with 1,132 additions and 826 deletions.
24 changes: 10 additions & 14 deletions 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 Expand Up @@ -1248,7 +1251,6 @@ def build_monthtable():

for lang in getList(CONFIG['IMP_MONTHLANG']):
try:
lang = str(lang)
if lang in table[0] or ((lang.startswith('en_') or lang == 'C') and 'en_' in str(table[0])):
logger.debug('Month names for %s already loaded' % lang)
else:
Expand All @@ -1269,8 +1271,8 @@ def build_monthtable():
try:
wanted_lang = lang.split('_')[0]
params = ['locale', '-a']
# noinspection PyArgumentList
all_locales = subprocess.check_output(params).split()
res = subprocess.check_output(params, stderr=subprocess.STDOUT)
all_locales = makeUnicode(res).split()
locale_list = []
for a_locale in all_locales:
if a_locale.startswith(wanted_lang):
Expand Down Expand Up @@ -1413,21 +1415,15 @@ def shutdown(restart=False, update=False):
if platform.system() == "Windows":
params = ["where", prg]
try:
if PY2:
executable = subprocess.check_output(params, stderr=subprocess.STDOUT).strip()
else:
# noinspection PyArgumentList
executable = subprocess.check_output(params, stderr=subprocess.STDOUT, encoding='utf-8').strip()
executable = subprocess.check_output(params, stderr=subprocess.STDOUT)
executable = makeUnicode(executable).strip()
except Exception as e:
logger.debug("where %s failed: %s %s" % (prg, type(e).__name__, str(e)))
else:
params = ["which", prg]
try:
if PY2:
executable = subprocess.check_output(params, stderr=subprocess.STDOUT).strip()
else:
# noinspection PyArgumentList
executable = subprocess.check_output(params, stderr=subprocess.STDOUT, encoding='utf-8').strip()
executable = subprocess.check_output(params, stderr=subprocess.STDOUT)
executable = makeUnicode(executable).strip()
except Exception as e:
logger.debug("which %s failed: %s %s" % (prg, type(e).__name__, str(e)))

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
2 changes: 2 additions & 0 deletions lazylibrarian/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,12 @@ def safe_unicode(obj, *args):
if not PY2:
return str(obj, *args)
try:
# noinspection PyCompatibility
return unicode(obj, *args)
except UnicodeDecodeError:
# obj is byte string
ascii_text = str(obj).encode('string_escape')
# noinspection PyCompatibility
return unicode(ascii_text)


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
58 changes: 18 additions & 40 deletions lazylibrarian/magazinescan.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,8 @@ def create_cover(issuefile=None, refresh=False):
postfix = '[0]'
try:
params = [converter, '%s%s' % (issuefile, postfix), '%s' % coverfile]
if PY2:
res = subprocess.check_output(params, stderr=subprocess.STDOUT)
else:
# noinspection PyArgumentList
res = subprocess.check_output(params, stderr=subprocess.STDOUT, encoding='utf-8')
res = subprocess.check_output(params, stderr=subprocess.STDOUT)
res = makeUnicode(res).strip()
if res:
logger.debug('%s reports: %s' % (lazylibrarian.CONFIG['IMP_CONVERT'], res))
except Exception as e:
Expand All @@ -159,22 +156,16 @@ def create_cover(issuefile=None, refresh=False):
if not os.path.isfile(GS):
params = ["where", "gswin64c"]
try:
if PY2:
GS = subprocess.check_output(params, stderr=subprocess.STDOUT).strip()
else:
# noinspection PyArgumentList
GS = subprocess.check_output(params, stderr=subprocess.STDOUT, encoding='utf-8').strip()
GS = subprocess.check_output(params, stderr=subprocess.STDOUT)
GS = makeUnicode(GS).strip()
generator = "gswin64c"
except Exception as e:
logger.debug("where gswin64c failed: %s %s" % (type(e).__name__, str(e)))
if not os.path.isfile(GS):
params = ["where", "gswin32c"]
try:
if PY2:
GS = subprocess.check_output(params, stderr=subprocess.STDOUT).strip()
else:
# noinspection PyArgumentList
GS = subprocess.check_output(params, stderr=subprocess.STDOUT, encoding='utf-8').strip()
GS = subprocess.check_output(params, stderr=subprocess.STDOUT)
GS = makeUnicode(GS).strip()
generator = "gswin32c"
except Exception as e:
logger.debug("where gswin32c failed: %s %s" % (type(e).__name__, str(e)))
Expand All @@ -185,21 +176,17 @@ def create_cover(issuefile=None, refresh=False):
# noinspection PyBroadException
try:
params = [GS, "--version"]
if PY2:
res = subprocess.check_output(params, stderr=subprocess.STDOUT)
else:
# noinspection PyArgumentList
res = subprocess.check_output(params, stderr=subprocess.STDOUT, encoding='utf-8')
res = subprocess.check_output(params, stderr=subprocess.STDOUT)
res = makeUnicode(res).strip()
logger.debug("Found %s [%s] version %s" % (generator, GS, res))
generator = "%s version %s" % (generator, res)
issuefile = issuefile.split('[')[0]
params = [GS, "-sDEVICE=jpeg", "-dNOPAUSE", "-dBATCH", "-dSAFER", "-dFirstPage=1", "-dLastPage=1",
"-dUseCropBox", "-sOutputFile=%s" % coverfile, issuefile]
if PY2:
res = subprocess.check_output(params, stderr=subprocess.STDOUT)
else:
# noinspection PyArgumentList
res = subprocess.check_output(params, stderr=subprocess.STDOUT, encoding='utf-8')

res = subprocess.check_output(params, stderr=subprocess.STDOUT)
res = makeUnicode(res).strip()

if not os.path.isfile(coverfile):
logger.debug("Failed to create jpg: %s" % res)
except Exception: # as why:
Expand Down Expand Up @@ -242,11 +229,8 @@ def create_cover(issuefile=None, refresh=False):
GS = ""
params = ["which", "gs"]
try:
if PY2:
GS = subprocess.check_output(params, stderr=subprocess.STDOUT).strip()
else:
# noinspection PyArgumentList
GS = subprocess.check_output(params, stderr=subprocess.STDOUT, encoding='utf-8').strip()
GS = subprocess.check_output(params, stderr=subprocess.STDOUT)
GS = makeUnicode(GS).strip()
generator = GS
except Exception as e:
logger.debug("which gs failed: %s %s" % (type(e).__name__, str(e)))
Expand All @@ -255,21 +239,15 @@ def create_cover(issuefile=None, refresh=False):
generator = "(no gs found)"
else:
params = [GS, "--version"]
if PY2:
res = subprocess.check_output(params, stderr=subprocess.STDOUT)
else:
# noinspection PyArgumentList
res = subprocess.check_output(params, stderr=subprocess.STDOUT, encoding='utf-8')
res = subprocess.check_output(params, stderr=subprocess.STDOUT)
res = makeUnicode(res).strip()
logger.debug("Found gs [%s] version %s" % (GS, res))
generator = "%s version %s" % (generator, res)
issuefile = issuefile.split('[')[0]
params = [GS, "-sDEVICE=jpeg", "-dNOPAUSE", "-dBATCH", "-dSAFER", "-dFirstPage=1",
"-dLastPage=1", "-dUseCropBox", "-sOutputFile=%s" % coverfile, issuefile]
if PY2:
res = subprocess.check_output(params, stderr=subprocess.STDOUT)
else:
# noinspection PyArgumentList
res = subprocess.check_output(params, stderr=subprocess.STDOUT, encoding='utf-8')
res = subprocess.check_output(params, stderr=subprocess.STDOUT)
res = makeUnicode(res).strip()
if not os.path.isfile(coverfile):
logger.debug("Failed to create jpg: %s" % res)
except Exception as e:
Expand Down
10 changes: 3 additions & 7 deletions lazylibrarian/notifiers/custom_notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import lazylibrarian
from lazylibrarian import logger, database
from lazylibrarian.common import notifyStrings, NOTIFY_SNATCH, NOTIFY_DOWNLOAD
from lib.six import PY2
from lazylibrarian.formatter import makeUnicode


class CustomNotifier:
Expand Down Expand Up @@ -60,12 +60,8 @@ def _notify(message, event, force=False):
params.append(str(dictionary[item]))

try:
if PY2:
res = subprocess.check_output(params, stderr=subprocess.STDOUT).strip()
else:
# noinspection PyArgumentList
res = subprocess.check_output(params, stderr=subprocess.STDOUT, encoding='utf-8').strip()
return res
res = subprocess.check_output(params, stderr=subprocess.STDOUT)
return makeUnicode(res).strip()
except Exception as e:
logger.warn('Error sending command: %s' % e)
return False
Expand Down
1 change: 0 additions & 1 deletion lazylibrarian/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import time
import datetime
import threading
from xml.etree import ElementTree

import lazylibrarian
Expand Down
Loading

0 comments on commit ae14d00

Please sign in to comment.