2013-09-16 01:41:53 +00:00
|
|
|
import random
|
2013-08-23 21:14:48 +00:00
|
|
|
from functools import partial
|
2018-06-08 19:17:39 +00:00
|
|
|
from urllib import quote
|
2013-08-23 21:14:48 +00:00
|
|
|
from xml.etree import ElementTree
|
|
|
|
|
2018-06-08 19:17:39 +00:00
|
|
|
import requests
|
|
|
|
from django.conf import settings
|
|
|
|
|
2018-04-20 01:23:24 +00:00
|
|
|
from django.apps import apps
|
2013-08-23 21:14:48 +00:00
|
|
|
|
|
|
|
from . exceptions import BooXtreamError
|
|
|
|
|
|
|
|
class BooXtream(object):
|
|
|
|
""" ``apikey``
|
|
|
|
|
2018-04-20 01:23:40 +00:00
|
|
|
The API key for your BooXtream account, obtained from BooXtream. Defaults to using
|
2013-08-23 21:14:48 +00:00
|
|
|
settings.BOOXTREAM_API_KEY
|
|
|
|
|
|
|
|
``apiuser``
|
|
|
|
|
2018-04-20 01:23:40 +00:00
|
|
|
The username key for your BooXtream account, obtained from BooXtream. Defaults to using
|
2013-08-23 21:14:48 +00:00
|
|
|
settings.BOOXTREAM_API_USER
|
2018-04-20 01:23:40 +00:00
|
|
|
|
|
|
|
|
2013-08-23 21:14:48 +00:00
|
|
|
``timeout``
|
2018-04-20 01:23:40 +00:00
|
|
|
|
2013-08-23 21:14:48 +00:00
|
|
|
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
|
2015-08-25 15:04:02 +00:00
|
|
|
self.endpoint = 'https://service.booxtream.com/'
|
2018-06-08 19:17:39 +00:00
|
|
|
self.postrequest = partial(requests.post, timeout=timeout, auth=(apiuser, apikey))
|
2018-04-20 01:23:40 +00:00
|
|
|
|
2013-08-23 21:14:48 +00:00
|
|
|
|
2018-06-08 19:17:39 +00:00
|
|
|
def platform(self, epubfile=None, epub=True, kf8mobi=False, **kwargs):
|
2018-04-20 01:23:40 +00:00
|
|
|
""" Make an API request to BooXtream
|
2013-08-27 03:43:40 +00:00
|
|
|
``self.apikey``, ``epubfile`` and the supplied ``kwargs``.
|
2013-08-23 21:14:48 +00:00
|
|
|
Attempts to deserialize the XML response and return the download link.
|
|
|
|
|
|
|
|
Will raise ``BooXtreamError`` if BooXtream returns an exception
|
|
|
|
code.
|
|
|
|
"""
|
2018-04-20 01:23:24 +00:00
|
|
|
Boox = apps.get_model('booxtream', 'Boox')
|
|
|
|
|
|
|
|
url = self.endpoint + 'booxtream.xml'
|
2018-06-08 19:17:39 +00:00
|
|
|
kwargs['epub'] = '1' if epub else '0'
|
2013-08-23 21:14:48 +00:00
|
|
|
kwargs['kf8mobi'] = '1' if kf8mobi else '0'
|
2013-09-16 01:41:53 +00:00
|
|
|
if epubfile:
|
2018-06-08 19:17:39 +00:00
|
|
|
if hasattr(epubfile, 'name') and str(epubfile.name).endswith('.epub'):
|
|
|
|
files = {'epubfile': (str(epubfile.name), epubfile)}
|
2013-09-16 01:41:53 +00:00
|
|
|
else:
|
2018-04-20 01:23:40 +00:00
|
|
|
# give it a random file name so that kindlegen doesn't choke
|
|
|
|
# needed for in-memory (StringIO) epubs
|
2018-06-08 19:17:39 +00:00
|
|
|
files= {'epubfile': ('%012x.epub' % random.randrange(16**12), epubfile)}
|
2013-09-16 01:41:53 +00:00
|
|
|
else:
|
2018-06-08 19:17:39 +00:00
|
|
|
files = {}
|
2013-11-15 16:25:41 +00:00
|
|
|
if settings.LOCAL_TEST:
|
|
|
|
# fake it, so you can test other functions without hitting booxtream
|
|
|
|
boox = Boox.objects.create(
|
2018-04-20 01:23:40 +00:00
|
|
|
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',
|
2018-06-08 19:01:22 +00:00
|
|
|
referenceid= kwargs.get('referenceid', '42'),
|
2018-06-08 19:17:39 +00:00
|
|
|
downloads_remaining=kwargs.get('downloadlimit', 10),
|
2018-06-08 19:01:22 +00:00
|
|
|
expirydays=kwargs.get('expirydays', 30),
|
2018-04-20 01:23:40 +00:00
|
|
|
)
|
2013-11-15 16:25:41 +00:00
|
|
|
return boox
|
|
|
|
|
2013-08-23 21:14:48 +00:00
|
|
|
resp = self.postrequest(url, data=kwargs, files=files)
|
|
|
|
doc = ElementTree.fromstring(resp.content)
|
2013-08-27 03:43:40 +00:00
|
|
|
|
|
|
|
# it turns out an Error can have an Error in it
|
2018-04-20 01:23:40 +00:00
|
|
|
errors = doc.findall('.//Response/Error')
|
2013-08-23 21:14:48 +00:00
|
|
|
if len(errors) > 0:
|
|
|
|
raise BooXtreamError(errors)
|
|
|
|
download_link_epub = doc.find('.//DownloadLink[@type="epub"]')
|
|
|
|
if download_link_epub is not None:
|
2018-04-20 01:23:40 +00:00
|
|
|
download_link_epub = download_link_epub.text
|
2013-08-23 21:14:48 +00:00
|
|
|
download_link_mobi = doc.find('.//DownloadLink[@type="mobi"]')
|
|
|
|
if download_link_mobi is not None:
|
2018-04-20 01:23:40 +00:00
|
|
|
download_link_mobi = download_link_mobi.text
|
2013-08-26 03:35:27 +00:00
|
|
|
boox = Boox.objects.create(
|
2018-04-20 01:23:40 +00:00
|
|
|
download_link_epub=download_link_epub,
|
|
|
|
download_link_mobi=download_link_mobi,
|
2018-06-08 19:17:39 +00:00
|
|
|
referenceid=kwargs.get('referenceid'),
|
|
|
|
downloads_remaining=kwargs.get('downloadlimit'),
|
2018-04-20 01:23:40 +00:00
|
|
|
expirydays=kwargs.get('expirydays'),
|
|
|
|
)
|
2018-06-08 19:17:39 +00:00
|
|
|
return boox
|