initial commit
parent
cf1eae6a5f
commit
10b975b839
|
@ -0,0 +1,71 @@
|
|||
import requests
|
||||
from django.conf import settings
|
||||
from urllib import quote
|
||||
from functools import partial
|
||||
from xml.etree import ElementTree
|
||||
|
||||
|
||||
from . exceptions import BooXtreamError
|
||||
|
||||
|
||||
class BooXtream(object):
|
||||
""" ``apikey``
|
||||
|
||||
The API key for your BooXtream account, obtained from BooXtream. Defaults to using
|
||||
settings.BOOXTREAM_API_KEY
|
||||
|
||||
``apiuser``
|
||||
|
||||
The username key for your BooXtream account, obtained from BooXtream. Defaults to using
|
||||
settings.BOOXTREAM_API_USER
|
||||
|
||||
``params``
|
||||
|
||||
Any extra keyword arguments supplied on initialization will be made
|
||||
available in a dict via this attribute. Only include parameters that
|
||||
should be used on each and every API call.
|
||||
|
||||
``timeout``
|
||||
|
||||
passed to requests
|
||||
"""
|
||||
def __init__(self,
|
||||
apikey='', apiuser='',
|
||||
timeout=None,
|
||||
**params):
|
||||
if not apikey:
|
||||
apikey = settings.BOOXTREAM_API_KEY
|
||||
if not apiuser:
|
||||
apiuser = settings.BOOXTREAM_API_USER
|
||||
self.params = params
|
||||
self.endpoint = 'http://service.booxtream.com/'
|
||||
self.postrequest = partial(requests.post, timeout=timeout, auth=(apiuser,apikey))
|
||||
|
||||
|
||||
def platform(self, epubfile=None, epub=True, kf8mobi=False, **kwargs):
|
||||
""" Make an API request to BooXtream
|
||||
``self.apikey``, ``self.params``, ``epubfile`` and the supplied ``kwargs``.
|
||||
Attempts to deserialize the XML response and return the download link.
|
||||
|
||||
Will raise ``BooXtreamError`` if BooXtream returns an exception
|
||||
code.
|
||||
"""
|
||||
url = self.endpoint + 'booxtream.xml'
|
||||
kwargs['epub'] = '1' if epub else '0'
|
||||
kwargs['kf8mobi'] = '1' if kf8mobi else '0'
|
||||
|
||||
files= {'epubfile': epubfile} if epubfile else {}
|
||||
resp = self.postrequest(url, data=kwargs, files=files)
|
||||
doc = ElementTree.fromstring(resp.content)
|
||||
errors = doc.findall('.//Error')
|
||||
if len(errors) > 0:
|
||||
raise BooXtreamError(errors)
|
||||
download_link_epub = doc.find('.//DownloadLink[@type="epub"]')
|
||||
if download_link_epub is not None:
|
||||
download_link_epub = download_link_epub.text
|
||||
download_link_mobi = doc.find('.//DownloadLink[@type="mobi"]')
|
||||
if download_link_mobi is not None:
|
||||
download_link_mobi = download_link_mobi.text
|
||||
|
||||
return (download_link_epub, download_link_mobi)
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
|
||||
|
||||
class BooXtreamError(Exception):
|
||||
""" list of errors returned in xml
|
||||
"""
|
||||
def __init__(self, errors):
|
||||
self.errors = errors
|
||||
|
||||
def __str__(self):
|
||||
errormsg='BooXtream errors:'
|
||||
for error in self.errors:
|
||||
errormsg += 'Error %s: %s\n'% (error.find('Code').text,error.find('Msg').text)
|
||||
return errormsg
|
|
@ -0,0 +1,15 @@
|
|||
|
||||
This is a thin Python (2.6+) wrapper for BooXtream's API for watermarking epub files. It's configured to look for parameters in Django settings files:
|
||||
BOOXTREAM_API_KEY = ''
|
||||
BOOXTREAM_API_USER = ''
|
||||
BOOXTREAM_TEST_EPUB = ''
|
||||
|
||||
http://www.booxtream.com/
|
||||
version 2.52.
|
||||
|
||||
Currently, only the platform method is implemented.
|
||||
|
||||
See Tests for usage
|
||||
|
||||
Eric Hellman August 2013
|
||||
Apache license
|
|
@ -0,0 +1,37 @@
|
|||
import unittest
|
||||
import time
|
||||
|
||||
# uses settings.BOOXTREAM_TEST_EPUB
|
||||
from . import settings
|
||||
|
||||
class TestBooXtream(unittest.TestCase):
|
||||
def _makeOne(self):
|
||||
from . import BooXtream
|
||||
manager = BooXtream()
|
||||
return manager
|
||||
|
||||
def test_booxtream_errors(self):
|
||||
from .exceptions import BooXtreamError
|
||||
inst = self._makeOne()
|
||||
with self.assertRaises(BooXtreamError) as cm:
|
||||
inst.platform()
|
||||
self.assertIn( 'expirydays not set',str(cm.exception))
|
||||
|
||||
|
||||
def test_booxtream_good(self):
|
||||
inst = self._makeOne()
|
||||
params={
|
||||
'customeremailaddress':'jane@example.com',
|
||||
'customername': 'Jane Test',
|
||||
'languagecode':'1043',
|
||||
'expirydays': 1,
|
||||
'downloadlimit': 3,
|
||||
'exlibris':1,
|
||||
'chapterfooter':1,
|
||||
'disclaimer':1,
|
||||
}
|
||||
params['referenceid']= 'order'+str(time.time())
|
||||
epubfile= open(settings.BOOXTREAM_TEST_EPUB)
|
||||
(epub_url,mobi_url)=inst.platform(epubfile=epubfile, **params)
|
||||
print epub_url
|
||||
self.assertRegexpMatches(epub_url,'download.booxtream.com/')
|
|
@ -148,6 +148,7 @@ INSTALLED_APPS = (
|
|||
# this must appear *after* django.frontend or else it overrides the
|
||||
# registration templates in frontend/templates/registration
|
||||
'django.contrib.admin',
|
||||
'booxtream',
|
||||
|
||||
)
|
||||
|
||||
|
@ -407,3 +408,6 @@ MARC_CHOICES = (
|
|||
('UNGLUE', 'Unglue.it link'),
|
||||
)
|
||||
|
||||
BOOXTREAM_API_KEY = '7ynRCsx4q21zEY67it7yk8u5rc6EXY'
|
||||
BOOXTREAM_API_USER = 'ungluetest'
|
||||
BOOXTREAM_TEST_EPUB = STATIC_ROOT+'/test/134221.0.epub'
|
Binary file not shown.
Loading…
Reference in New Issue