Skip to content

Commit ea2b754

Browse files
committed
Automagic subcategorisation 🏷️
1 parent fb5ba59 commit ea2b754

1 file changed

Lines changed: 43 additions & 20 deletions

File tree

pythonbits/bb.py

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from collections import namedtuple, abc
1010
from concurrent.futures.thread import ThreadPoolExecutor
1111
from datetime import timedelta
12-
from mimetypes import guess_type
12+
import mimetypes
1313

1414
import pymediainfo
1515
import mutagen
@@ -59,6 +59,17 @@ def uniq(seq):
5959

6060
class BbSubmission(Submission):
6161
default_fields = ("form_title", "tags", "cover")
62+
ebook_types = {'application/epub+zip': 'EPUB',
63+
'application/x-mobipocket-ebook': 'MOBI',
64+
'application/pdf': 'PDF',
65+
'text/html': 'HTML',
66+
'text/plain': 'TXT',
67+
'image/vnd.djvu': 'DJVU',
68+
'application/vnd.ms-htmlhelp': 'CHM',
69+
'application/x-cbr': 'CBR',
70+
'application/x-cbz': 'CBZ',
71+
'application/x-cb7': 'CB7',
72+
'application/x-mobi8-ebook': 'AZW3'}
6273

6374
def show_fields(self, fields):
6475
return super(BbSubmission, self).show_fields(
@@ -70,6 +81,7 @@ def confirm_finalization(self, fields):
7081

7182
def subcategory(self):
7283
path = self['path']
84+
self.add_ebook_mime_types()
7385
if os.path.isfile(path):
7486
files = [(os.path.getsize(path), path)]
7587
else:
@@ -81,14 +93,15 @@ def subcategory(self):
8193
files.append((os.path.getsize(fpath), fpath))
8294

8395
for _, path in sorted(files, reverse=True):
84-
mime_guess, _ = guess_type(path)
96+
mime_guess, _ = mimetypes.guess_type(path)
8597
if mime_guess:
8698
mime_guess = mime_guess.split('/')
8799
if mime_guess[0] == 'video':
88100
return VideoSubmission
89101
elif mime_guess[0] == 'audio':
90102
return AudioSubmission
91-
103+
elif self.subcategorise_ebook('/'.join(mime_guess)):
104+
return BookSubmission
92105
log.info("Unable to guess submission category using known mimetypes")
93106
while True:
94107
cat = input("Please manually specify category. "
@@ -111,6 +124,30 @@ def subcategorise(self):
111124
sub.depends_on = self.depends_on
112125
return sub
113126

127+
def add_ebook_mime_types(self):
128+
contentTypes = mimetypes.types_map
129+
contentTypes.update(
130+
{
131+
'.epub': 'application/epub+zip',
132+
'.mobi': 'application/x-mobipocket-ebook',
133+
'.pdf': 'application/pdf',
134+
'.html': 'text/html',
135+
'.txt': 'text/plain',
136+
'.djvu': 'image/vnd.djvu',
137+
'.chm': 'application/vnd.ms-htmlhelp',
138+
'.cbr': 'application/x-cbr',
139+
'.cbz': 'application/x-cbz',
140+
'.cb7': 'application/x-cb7',
141+
'.azw3': 'application/x-mobi8-ebook'
142+
}
143+
)
144+
145+
def subcategorise_ebook(self, mime):
146+
try:
147+
return self.ebook_types.get(mime)
148+
except KeyError:
149+
return False
150+
114151
@staticmethod
115152
def submit(payload):
116153
t = Tracker()
@@ -965,23 +1002,9 @@ def _render_author(self):
9651002

9661003
@form_field('book_format')
9671004
def _render_format(self):
968-
book_format = {
969-
'EPUB': 'EPUB',
970-
'MOBI': 'MOBI',
971-
'PDF': 'PDF',
972-
'HTML': 'HTML',
973-
'TXT': 'TXT',
974-
'DJVU': 'DJVU',
975-
'CHM': 'CHM',
976-
'CBR': 'CBR',
977-
'CBZ': 'CBZ',
978-
'CB7': 'CB7',
979-
'TXT': 'TXT',
980-
'AZW3': 'AZW3',
981-
}
982-
983-
_, ext = os.path.splitext(self['path'])
984-
return book_format[ext.replace('.', '').upper()]
1005+
mime_type, _ = mimetypes.guess_type(self['path'])
1006+
fmt = self.subcategorise_ebook(mime_type)
1007+
return fmt
9851008

9861009
def _render_summary(self):
9871010
gr = goodreads.Goodreads()

0 commit comments

Comments
 (0)