regluit/booxtream/__init__.py

94 lines
3.6 KiB
Python
Raw Normal View History

2013-08-23 21:14:48 +00:00
import requests
2013-09-16 01:41:53 +00:00
import random
2013-08-23 21:14:48 +00:00
from django.conf import settings
from urllib import quote
from functools import partial
from xml.etree import ElementTree
from . exceptions import BooXtreamError
from . models import Boox
2013-08-23 21:14:48 +00:00
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
``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.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
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.
"""
url = self.endpoint + 'booxtream.xml'
kwargs['epub'] = '1' if epub else '0'
kwargs['kf8mobi'] = '1' if kf8mobi else '0'
2013-09-16 01:41:53 +00:00
if 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)}
else:
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',
download_link_mobi='https://github.com/eshellman/42_ebook/blob/master/download/42.mobi?raw=true',
referenceid= kwargs.get('referenceid'),
downloads_remaining= kwargs.get('downloadlimit'),
expirydays=kwargs.get('expirydays'),
)
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
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:
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
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'),
expirydays=kwargs.get('expirydays'),
)
return boox
2013-08-23 21:14:48 +00:00