Moved code to call the mobigen service from .ipynb to regluit.core.mobigen (may eventually want to move core/mobigen.py to external package)
parent
50021f2ab2
commit
5995ae610a
|
@ -0,0 +1,45 @@
|
|||
"""
|
||||
Utility for calling mobigen
|
||||
|
||||
"""
|
||||
|
||||
import requests
|
||||
|
||||
|
||||
def convert_to_mobi(input_url, input_format="application/epub+zip"):
|
||||
|
||||
"""
|
||||
return a string with the output of mobigen computation
|
||||
|
||||
"""
|
||||
|
||||
|
||||
# substitute file_path with a local epub or html file
|
||||
#file_path = "/Users/raymondyee/D/Document/Gluejar/Gluejar.github/regluit/test-data/pg2701.epub"
|
||||
#file_type = "application/epub+zip"
|
||||
|
||||
# where to write the output
|
||||
#output_path = "/Users/raymondyee/Downloads/pg2701.mobi"
|
||||
|
||||
# url of the mobigen service
|
||||
mobigen_url = "https://docker.gluejar.com:5001/mobigen"
|
||||
mobigen_user_id = "admin"
|
||||
mobigen_password = "CXq5FSEQFgXtP_s"
|
||||
|
||||
# read the file and do a http post
|
||||
# equivalent curl
|
||||
# curl -k --user "admin:CXq5FSEQFgXtP_s" -X POST -H "Content-Type: application/epub+zip" --data-binary "@/Users/raymondyee/D/Document/Gluejar/Gluejar.github/regluit/test-data/pg2701.epub" https://docker.gluejar.com/mobigen:5001 > pg2701.mobi
|
||||
|
||||
# using verify=False since at the moment, using a self-signed SSL cert.
|
||||
|
||||
payload = requests.get(input_url, verify=False).content
|
||||
|
||||
headers = {'Content-Type': input_format}
|
||||
r = requests.post(mobigen_url, auth=(mobigen_user_id, mobigen_password),
|
||||
data=payload, verify=False, headers=headers)
|
||||
|
||||
# if HTTP reponse code is ok, the output is the mobi file; else error message
|
||||
if r.status_code == 200:
|
||||
return r.content
|
||||
else:
|
||||
raise Exception("{0}: {1}".format(r.status_code, r.content))
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"metadata": {
|
||||
"name": "",
|
||||
"signature": "sha256:62e6f2aa6e802f88279e70982a307ccd5ff3669b72454d6b0ae9409b5f1178e6"
|
||||
"signature": "sha256:c3439815c10b9795d3d95691a1edd9789672f61dc0b415cd57b8e06d0552f8a5"
|
||||
},
|
||||
"nbformat": 3,
|
||||
"nbformat_minor": 0,
|
||||
|
@ -76,64 +76,26 @@
|
|||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"def convert_to_mobi(input_url, input_format=\"application/epub+zip\"):\n",
|
||||
" \n",
|
||||
" \"\"\"\n",
|
||||
" return a StringIO with the mobi\n",
|
||||
" \n",
|
||||
" \"\"\"\n",
|
||||
"from regluit.core.mobigen import convert_to_mobi\n",
|
||||
"\n",
|
||||
" import requests\n",
|
||||
" from StringIO import StringIO\n",
|
||||
"\n",
|
||||
" # substitute file_path with a local epub or html file\n",
|
||||
" #file_path = \"/Users/raymondyee/D/Document/Gluejar/Gluejar.github/regluit/test-data/pg2701.epub\"\n",
|
||||
" #file_type = \"application/epub+zip\" \n",
|
||||
"\n",
|
||||
" # where to write the output \n",
|
||||
" #output_path = \"/Users/raymondyee/Downloads/pg2701.mobi\"\n",
|
||||
"\n",
|
||||
" # url of the mobigen service\n",
|
||||
" mobigen_url = \"https://docker.gluejar.com:5001/mobigen\"\n",
|
||||
" mobigen_user_id = \"admin\"\n",
|
||||
" mobigen_password = \"CXq5FSEQFgXtP_s\"\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" # read the file and do a http post\n",
|
||||
" # equivalent curl\n",
|
||||
" # curl -k --user \"admin:CXq5FSEQFgXtP_s\" -X POST -H \"Content-Type: application/epub+zip\" --data-binary \"@/Users/raymondyee/D/Document/Gluejar/Gluejar.github/regluit/test-data/pg2701.epub\" https://docker.gluejar.com/mobigen:5001 > pg2701.mobi\n",
|
||||
"\n",
|
||||
" # using verify=False since at the moment, using a self-signed SSL cert.\n",
|
||||
"\n",
|
||||
" payload = requests.get(input_url, verify=False).content \n",
|
||||
"\n",
|
||||
" headers = {'Content-Type': input_format}\n",
|
||||
" r = requests.post(mobigen_url, auth=(mobigen_user_id, mobigen_password),\n",
|
||||
" data=payload, verify=False, headers=headers)\n",
|
||||
"\n",
|
||||
" # if HTTP reponse code is ok, the output is the mobi file; else error message\n",
|
||||
" if r.status_code == 200:\n",
|
||||
" return r.content\n",
|
||||
" else:\n",
|
||||
" raise Exception(\"{0}: {1}\".format(r.status_code, r.content))\n"
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"output = convert_to_mobi(\"https://archive.org/download/Feeding_the_City/9781909254039_Feeding_the_City.epub\")\n",
|
||||
"\n",
|
||||
"with open(\"/Users/raymondyee/Downloads/test.mobi\", \"wb\") as f:\n",
|
||||
" f.write(output)\n",
|
||||
"\n"
|
||||
" f.write(output)\n"
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
"outputs": []
|
||||
"outputs": [
|
||||
{
|
||||
"output_type": "stream",
|
||||
"stream": "stderr",
|
||||
"text": [
|
||||
"/Users/raymondyee/anaconda/envs/regluit/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py:730: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html (This warning will only appear once by default.)\n",
|
||||
" InsecureRequestWarning)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 1
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
|
|
Loading…
Reference in New Issue