From 50930ce2b1909e2ef13eea6272943a63d412fcee Mon Sep 17 00:00:00 2001 From: eric Date: Mon, 9 May 2016 18:32:38 -0400 Subject: [PATCH] send kindle from s3 add file archiving code, because we needed it for push distribution. Also use archive for kindle. tweak send-to-kindle docs. --- core/models.py | 35 +++++++++++++++++++++++++-- core/tests.py | 12 ++++++++- frontend/templates/kindle_config.html | 4 +-- frontend/views.py | 12 ++------- 4 files changed, 48 insertions(+), 15 deletions(-) diff --git a/core/models.py b/core/models.py index 41f53499..fcd702ca 100755 --- a/core/models.py +++ b/core/models.py @@ -1001,8 +1001,8 @@ class Campaign(models.Model): self.work.make_ebooks_from_ebfs(add_ask=True) def make_unglued_ebf(self, format, watermarked): - ebf=EbookFile.objects.create(edition=self.work.preferred_edition, format=format) r=urllib2.urlopen(watermarked.download_link(format)) + ebf=EbookFile.objects.create(edition=self.work.preferred_edition, format=format) ebf.file.save(path_for_file(ebf,None),ContentFile(r.read())) ebf.file.close() ebf.save() @@ -2005,7 +2005,38 @@ class Ebook(models.Model): return True else: return False - + + def get_archive(self): # returns an archived file + if self.edition.ebook_files.filter(format=self.format).count()==0: + if self.provider is not 'Unglue.it': + try: + r=urllib2.urlopen(self.url) + try: + self.filesize = int(r.info().getheaders("Content-Length")[0]) + if self.save: + self.filesize = self.filesize if self.filesize < 2147483647 else 2147483647 # largest safe positive integer + self.save() + ebf=EbookFile.objects.create(edition=self.edition, format=self.format) + ebf.file.save(path_for_file(ebf,None),ContentFile(r.read())) + ebf.file.close() + ebf.save() + return ebf.file.open() + except IndexError: + # response has no Content-Length header probably a bad link + logging.error( 'Bad link error: {}'.format(ebook.url) ) + except IOError: + logger.error(u'could not open {}'.format(self.url) ) + else: + # this shouldn't happen, except in testing perhaps + logger.error(u'couldn\'t find ebookfile for {}'.format(self.url) ) + # try the url instead + f = urllib.urlopen(self.url) + return f + else: + f= self.edition.ebook_files.filter(format=self.format).order_by('-created')[0].file + f.open() + return f + def set_provider(self): self.provider=Ebook.infer_provider(self.url) return self.provider diff --git a/core/tests.py b/core/tests.py index 25588957..65119ce9 100755 --- a/core/tests.py +++ b/core/tests.py @@ -87,11 +87,18 @@ class BookLoaderTests(TestCase): huck = models.Work.objects.get(id=huck_id) self.assertTrue( huck.ebooks().count()>1) + def test_add_by_yaml(self): space_id = bookloader.load_from_yaml('https://github.com/gitenberg-dev/metadata/raw/master/samples/pandata.yaml') huck_id = bookloader.load_from_yaml('https://github.com/GITenberg/Adventures-of-Huckleberry-Finn_76/raw/master/metadata.yaml') space = models.Work.objects.get(id=space_id) huck = models.Work.objects.get(id=huck_id) + + #test ebook archiving + num_ebf= EbookFile.objects.all().count() + for ebook in huck.ebooks().all(): + f = ebook.get_archive() + self.assertTrue(EbookFile.objects.all().count()>num_ebf) def test_valid_subject(self): self.assertTrue(bookloader.valid_subject('A, valid, suj\xc3t')) @@ -455,7 +462,10 @@ class BookLoaderTests(TestCase): ebook = bookloader.load_gutenberg_edition(title, gutenberg_etext_id, ol_work_id, seed_isbn, epub_url, format, license, lang, publication_date) self.assertEqual(ebook.url, epub_url) - + def tearDown(self): + for ebf in EbookFile.objects.all(): + ebf.file.delete() + class SearchTests(TestCase): def test_basic_search(self): diff --git a/frontend/templates/kindle_config.html b/frontend/templates/kindle_config.html index d7c1efa9..4da5cac5 100644 --- a/frontend/templates/kindle_config.html +++ b/frontend/templates/kindle_config.html @@ -62,11 +62,11 @@ document.getElementById('id_kindle_email').focus() - https://www.amazon.com/gp/digital/fiona/manage#manageDevices +

Don't know the email address for your device or reading app? Find it here. (If you're not logged in to Amazon, you need to click "Manage Your Devices" in the "Your Kindle Account" section on the lower left side of the page.)

-

Once we have your Kindle email, you'll be able to send unglued ebooks to your Kindle device or app with one click from any Unglue.it download page.

+

Once we have your Kindle email, you'll be able to send unglued ebooks to your Kindle device or app with one click from any Unglue.it download page. Note: ebooks that we send will appear in the "Docs" tab, not "Books".

{% endwith %} {% endblock %} diff --git a/frontend/views.py b/frontend/views.py index 8e67a777..88780eea 100755 --- a/frontend/views.py +++ b/frontend/views.py @@ -3323,21 +3323,13 @@ def send_to_kindle(request, work_id, javascript='0'): This won't perfectly measure size of email, but should be safe, and is much faster than doing the check after download. """ try: - filehandle = urllib.urlopen(ebook.url) + filehandle = ebook.get_archive() except IOError: # problems connection to the ebook source logger.error("couldn't connect error: %s", ebook.url) return local_response(request, javascript, context, 5) if not ebook.filesize: - try: - ebook.filesize = int(filehandle.info().getheaders("Content-Length")[0]) - if ebook.save: - ebook.filesize = ebook.filesize if ebook.filesize < 2147483647 else 2147483647 # largest safe positive integer - ebook.save() - except IndexError: - # response has no Content-Length header probably a bad link - logger.error('Bad link error: %s', ebook.url) - return local_response(request, javascript, context, 4) + return local_response(request, javascript, context, 4) if ebook.filesize > models.send_to_kindle_limit: logger.info('ebook %s is too large to be emailed' % work.id) return local_response(request, javascript, context, 0)