pull/91/head
eric 2018-06-08 15:17:39 -04:00
parent 8b385d05ed
commit 455032ebc3
4 changed files with 35 additions and 36 deletions

View File

@ -1,10 +1,11 @@
import requests
import random
from django.conf import settings
from urllib import quote
from functools import partial
from urllib import quote
from xml.etree import ElementTree
import requests
from django.conf import settings
from django.apps import apps
from . exceptions import BooXtreamError
@ -34,10 +35,10 @@ class BooXtream(object):
if not apiuser:
apiuser = settings.BOOXTREAM_API_USER
self.endpoint = 'https://service.booxtream.com/'
self.postrequest = partial(requests.post, timeout=timeout, auth=(apiuser,apikey))
self.postrequest = partial(requests.post, timeout=timeout, auth=(apiuser, apikey))
def platform(self, epubfile=None, epub=True, kf8mobi=False, **kwargs):
def platform(self, epubfile=None, epub=True, kf8mobi=False, **kwargs):
""" Make an API request to BooXtream
``self.apikey``, ``epubfile`` and the supplied ``kwargs``.
Attempts to deserialize the XML response and return the download link.
@ -48,24 +49,24 @@ class BooXtream(object):
Boox = apps.get_model('booxtream', 'Boox')
url = self.endpoint + 'booxtream.xml'
kwargs['epub'] = '1' if epub else '0'
kwargs['epub'] = '1' if epub else '0'
kwargs['kf8mobi'] = '1' if kf8mobi else '0'
if epubfile:
if hasattr(epubfile,'name') and str(epubfile.name).endswith('.epub'):
files= {'epubfile': (str(epubfile.name),epubfile)}
if hasattr(epubfile, 'name') and str(epubfile.name).endswith('.epub'):
files = {'epubfile': (str(epubfile.name), epubfile)}
else:
# give it a random file name so that kindlegen doesn't choke
# needed for in-memory (StringIO) epubs
files= {'epubfile': ('%012x.epub' % random.randrange(16**12),epubfile)}
files= {'epubfile': ('%012x.epub' % random.randrange(16**12), epubfile)}
else:
files={}
files = {}
if settings.LOCAL_TEST:
# fake it, so you can test other functions without hitting booxtream
boox = Boox.objects.create(
download_link_epub='https://github.com/eshellman/42_ebook/blob/master/download/42.epub?raw=true&extra=download.booxtream.com/',
download_link_mobi='https://github.com/eshellman/42_ebook/blob/master/download/42.mobi?raw=true',
referenceid= kwargs.get('referenceid', '42'),
downloads_remaining= kwargs.get('downloadlimit', 10),
downloads_remaining=kwargs.get('downloadlimit', 10),
expirydays=kwargs.get('expirydays', 30),
)
return boox
@ -86,9 +87,8 @@ class BooXtream(object):
boox = Boox.objects.create(
download_link_epub=download_link_epub,
download_link_mobi=download_link_mobi,
referenceid= kwargs.get('referenceid'),
downloads_remaining= kwargs.get('downloadlimit'),
referenceid=kwargs.get('referenceid'),
downloads_remaining=kwargs.get('downloadlimit'),
expirydays=kwargs.get('expirydays'),
)
return boox
return boox

View File

@ -7,7 +7,7 @@ class BooXtreamError(Exception):
self.errors = errors
def __str__(self):
errormsg='BooXtream errors:'
errormsg = 'BooXtream errors:'
for error in self.errors:
errormsg += 'Error %s: %s\n'% (error.find('Code').text,error.find('Msg').text)
return errormsg
errormsg += 'Error %s: %s\n'% (error.find('Code').text, error.find('Msg').text)
return errormsg

View File

@ -6,22 +6,21 @@ class Boox(models.Model):
"""
keeps a record of a file that's been watermarked
"""
download_link_epub = models.URLField(null=True)
download_link_mobi = models.URLField(null=True)
download_link_epub = models.URLField(null=True)
download_link_mobi = models.URLField(null=True)
referenceid = models.CharField(max_length=32)
downloads_remaining = models.PositiveSmallIntegerField(default = 0)
downloads_remaining = models.PositiveSmallIntegerField(default=0)
expirydays = models.PositiveSmallIntegerField()
created = models.DateTimeField(auto_now_add=True)
@property
def expired(self):
return self.created+timedelta(days=self.expirydays) < datetime.now()
def download_link(self, format):
if format == 'epub':
return self.download_link_epub
elif format == 'mobi':
return self.download_link_mobi
else:
return None
return None

View File

@ -1,6 +1,6 @@
import unittest
import time
import urllib2
import urllib2
from tempfile import NamedTemporaryFile
from StringIO import StringIO
from django.conf import settings
@ -32,7 +32,7 @@ class TestBooXtream(unittest.TestCase):
with self.assertRaises(BooXtreamError) as cm:
inst.platform()
self.assertIn('expirydays not set', str(cm.exception))
params={
params = {
'customername': 'Jane Test',
'languagecode':'1043',
'expirydays': 1,
@ -49,7 +49,7 @@ class TestBooXtream(unittest.TestCase):
def test_booxtream_good(self):
inst = self._makeOne()
params={
params = {
'customeremailaddress':'jane@example.com',
'customername': 'Jane Test',
'languagecode':'1043',
@ -59,16 +59,16 @@ class TestBooXtream(unittest.TestCase):
'chapterfooter':1,
'disclaimer':1,
}
params['referenceid']= 'order'+str(time.time())
boox=inst.platform(epubfile=self.epub2file, **params)
self.assertRegexpMatches(boox.download_link_epub,'download.booxtream.com/')
params['referenceid'] = 'order' + str(time.time())
boox = inst.platform(epubfile=self.epub2file, **params)
self.assertRegexpMatches(boox.download_link_epub, 'download.booxtream.com/')
self.assertFalse(boox.expired)
self.assertEqual(boox.downloads_remaining,3)
self.assertEqual(boox.downloads_remaining, 3)
# make sure it works with an in-memory file
self.epub2file.seek(0)
in_mem_epub = StringIO()
in_mem_epub.write(self.epub2file.read())
in_mem_epub.seek(0)
boox2=inst.platform(epubfile=in_mem_epub, **params)
self.assertRegexpMatches(boox2.download_link_epub,'download.booxtream.com/')
boox2 = inst.platform(epubfile=in_mem_epub, **params)
self.assertRegexpMatches(boox2.download_link_epub, 'download.booxtream.com/')