From dead612147dc86de019a680a473351a10e419338 Mon Sep 17 00:00:00 2001 From: Jordan Date: Mon, 4 May 2020 21:12:12 -0700 Subject: use natural sort of files in RSS resp if 'ignore_tracknum' in book path --- README | 4 ++++ run.py | 34 +++++++++++++++++++++------------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/README b/README index 843cdbd..cef6721 100644 --- a/README +++ b/README @@ -33,5 +33,9 @@ design decisions 3. XML pubDate and list order is derived from MP3 track attributes; if not present or duplicates exist, tracks are sorted alphanumerically + if a book's track numbers are unique but incorrect, a preference for filename + sort can be established by creating an 'ignore_tracknum' file in the + audiobook's path + 4. no rebuild endpoint exists; cache-affecting routines are run externally via rebuild.py diff --git a/run.py b/run.py index af9abba..d73545b 100755 --- a/run.py +++ b/run.py @@ -127,20 +127,28 @@ def list_books(): book_title = ET.SubElement(channel, 'title') book_title.text = escape(books[a]['title']) - # sort by track number, alphanumerically if track is absent - track_list = [] # account for duplicates - for a_file in books[a]['files']: - track = books[a]['files'][a_file]['track'] - if not track or track in track_list: - # remove leading zeros from digits (natural sort) - conv = lambda s: [int(x) if x.isdigit() else x.lower() for x in - re.split('(\d+)', s)] - key = lambda x: conv(books[a]['files'][x]['filename']) - break - track_list.append(track) + # use filename sort if ignore_tracknum file present in book dir + ignore_tracknum = os.path.join(books[a]['path'], 'ignore_tracknum') + if os.path.exists(ignore_tracknum): + # remove leading zeros from digits (natural sort) + conv = lambda s: [int(x) if x.isdigit() else x.lower() for x in + re.split('(\d+)', s)] + key = lambda x: conv(books[a]['files'][x]['filename']) else: - # we have populated and unique track values, use those - key = lambda x: books[a]['files'][x]['track'] + # sort by track number, alphanumerically if track is absent + track_list = [] # account for duplicates + for a_file in books[a]['files']: + track = books[a]['files'][a_file]['track'] + if not track or track in track_list: + # remove leading zeros from digits (natural sort) + conv = lambda s: [int(x) if x.isdigit() else x.lower() + for x in re.split('(\d+)', s)] + key = lambda x: conv(books[a]['files'][x]['filename']) + break + track_list.append(track) + else: + # we have populated and unique track values, use those + key = lambda x: books[a]['files'][x]['track'] # populate XML attribute values required by Apple podcasts for idx, f in enumerate(sorted(books[a]['files'], key=key)): -- cgit v1.2.3-54-g00ecf