Skip to content

Commit

Permalink
add reset option
Browse files Browse the repository at this point in the history
  • Loading branch information
eshellman committed Jun 11, 2018
1 parent 20b8a29 commit ea278c3
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions core/management/commands/make_missing_mobis.py
@@ -1,22 +1,27 @@
from django.core.management.base import BaseCommand
from regluit.core.models import Work
from regluit.core.models import Work, EbookFile


class Command(BaseCommand):
help = "generate mobi ebooks where needed and possible."
args = "<max>"


def add_arguments(self, parser):
parser.add_argument('max', nargs='?', type=int, default=1, help="maximum mobis to make")
parser.add_argument('--reset', '-r', action='store_true', help="reset failed mobi conversions")


def handle(self, max=None, **options):
if max:
try:
max = int(max)
except ValueError:
max = 1
else:
max = 1
maxbad = 10
if options['reset']:
bads = EbookFile.objects.filter(mobied__lt=0)
for bad in bads:
bad.mobied = 0
bad.save()

epubs = Work.objects.filter(editions__ebooks__format='epub').distinct().order_by('-id')

i = 0
n_bad = 0
for work in epubs:
if not work.ebooks().filter(format="mobi"):
for ebook in work.ebooks().filter(format="epub"):
Expand All @@ -26,11 +31,14 @@ def handle(self, max=None, **options):
print u'making mobi for {}'.format(work.title)
if ebf.make_mobi():
print 'made mobi'
i = i + 1
i += 1
break
else:
print 'failed to make mobi'
self.stdout.write('failed to make mobi')
n_bad += 1

except:
print 'failed to make mobi'
if i >= max:
self.stdout.write('failed to make mobi')
n_bad += 1
if i >= max or n_bad >= maxbad:
break

0 comments on commit ea278c3

Please sign in to comment.