Add feed=opds to download URLs for ebooks in OPDS feed

pull/1/head
Raymond Yee 2014-06-25 16:54:01 -07:00
parent 49133935a0
commit 8830919318
2 changed files with 102 additions and 3 deletions

View File

@ -22,11 +22,25 @@ def text_node(tag, text):
node.text = text
return node
def map_to_unglueit(url, domain="unglue.it", protocol="https"):
def map_to_domain(url, domain="unglue.it", protocol="https"):
"""
for the given url, substitute with the given domain and protocol
"""
m = list(urlparse.urlparse(url))
(m[0], m[1]) = (protocol,domain)
return urlparse.urlunparse(m)
def add_query_component(url, qc):
""" """
m = list(urlparse.urlparse(url))
if len(m[4]):
m[4] = "&".join([m[4],qc])
else:
m[4] = qc
return urlparse.urlunparse(m)
def work_node(work, domain="unglue.it", protocol="https"):
node = etree.Element("entry")
@ -45,7 +59,13 @@ def work_node(work, domain="unglue.it", protocol="https"):
for ebook in work.ebooks():
link_node = etree.Element("link")
link_node.attrib.update({"href":map_to_unglueit(ebook.download_url, domain, protocol),
# ebook.download_url is an absolute URL with the protocol, domain, and path baked in
# when computing the URL from a laptop but wanting to have the URL correspond to unglue.it,
# I made use of:
# "href":map_to_domain(ebook.download_url, domain, protocol),
link_node.attrib.update({"href":add_query_component(ebook.download_url, "feed=opds"),
"type":FORMAT_TO_MIMETYPE.get(ebook.format, ""),
"rel":"http://opds-spec.org/acquisition"})
node.append(link_node)

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:7a5c33348cb395843aa3eb5a4c56f5d131784a76bb2d5aa85245ac102f3385e7"
"signature": "sha256:6bd6c988240f3f5084034a9aca36b1e1f5de3799a7b1a46c9624e8de724010f2"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -618,6 +618,85 @@
"metadata": {},
"outputs": []
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Dealing URLs of downloaded books"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from regluit.core.models import Work\n",
"\n",
"work = Work.objects.get(id=137688)\n",
"[ebook.download_url for ebook in work.ebooks()]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 18,
"text": [
"['http://127.0.0.1:8000/download_ebook/1317/']"
]
}
],
"prompt_number": 18
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Tacking on a query component to a URL"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"urlli"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import urllib\n",
"\n",
"def add_query_component(url, qc):\n",
" m = list(urlparse.urlparse(url))\n",
" if len(m[4]):\n",
" m[4] = \"&\".join([m[4],qc])\n",
" else:\n",
" m[4] = qc\n",
" return urlparse.urlunparse(m)\n",
"\n",
"add_query_component(\"https://unglue.it/download_ebook/906/\", \"feed=opds\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 30,
"text": [
"'https://unglue.it/download_ebook/906/?feed=opds'"
]
}
],
"prompt_number": 30
},
{
"cell_type": "heading",
"level": 1,