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.
pull/1/head
eric 2016-05-09 18:32:38 -04:00
parent ee0d19810c
commit 50930ce2b1
4 changed files with 48 additions and 15 deletions

View File

@ -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

View File

@ -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):

View File

@ -62,11 +62,11 @@
document.getElementById('id_kindle_email').focus()
</script>
</div>
https://www.amazon.com/gp/digital/fiona/manage#manageDevices
<div>
<p>Don't know the email address for your device or reading app? <a href="https://www.amazon.com/myk#manageDevices">Find it here</a>. (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.)</p>
<p>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.</p>
<p>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".</p>
</div>
{% endwith %}
{% endblock %}

View File

@ -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)