forked from leo-vip/cockybook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopdscore.py
275 lines (236 loc) · 8.48 KB
/
opdscore.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# coding: UTF-8
import logging
import os
from xml.dom.minidom import Document, Text, Element
import datetime
from flask import g
import Config
import Const
from filesystem import LocalFileSystem, QiniuFileSystem, LocalMetadataFileSystem
import utils
__author__ = 'lei'
if Config.filesyste_type == 'LocalFileSystem':
fs = LocalFileSystem()
elif Config.filesyste_type =='LocalMetadataFileSystem':
fs = LocalMetadataFileSystem()
else:
fs = QiniuFileSystem()
def setfeedNS(feed):
feed.setAttribute("xmlns:app", "http://www.w3.org/2007/app")
feed.setAttribute("xmlns:opds", "http://opds-spec.org/2010/catalog")
feed.setAttribute("xmlns:opds", Config.SITE_URL)
feed.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
#feed.setAttribute("xmlns", "http://www.w3.org/2005/Atom")
feed.setAttribute("xmlns:dcterms", "http://purl.org/dc/terms/")
feed.setAttribute("xmlns:thr", "http://purl.org/syndication/thread/1.0")
feed.setAttribute("xmlns:opensearch", "http://a9.com/-/spec/opensearch/1.1/")
def getCreateDate(file_path):
#return datetime.datetime.now(os.path.getctime(file_path)).strftime("%Y-%m-%dT%I:%M:%SZ")
return datetime.datetime.now().strftime("%Y-%m-%dT%I:%M:%SZ")
def create_entry(isFile, path, name):
'''
create filesystem return object
:param isFile:
:param path:
:param name:
:return:
'''
entry = Entry()
if not isFile:
entry.id = utils.connect_path(utils.connect_path(Config.SITE_BOOK_LIST,path),name)
entry.links=[]
entry.links.append(Link(entry.id, _get_book_entry_rel(name), name, _get_book_entry_type(name)))
else:
entry.id = utils.connect_path(utils.connect_path(Config.SITE_BOOK_LIST, path), name)
#TODO add Another Links
links=fs.getdownloadurl(path, name)
#name=os.path.basename(path)
entry.links=[]
if links !=None:
for link in links:
entry.links.append(Link(link, _get_book_entry_rel(link), name, _get_book_entry_type(link)))
entry.content = name
entry.title = name
entry.updated = utils.getNow()
return entry
def create__single_entry(isFile, path, name):
'''
create filesystem return object for file request
:param isFile:
:param path:
:param name:
:return:
'''
entry = Entry()
if not isFile:
entry.id = utils.connect_path(utils.connect_path(Config.SITE_BOOK_LIST,path),name)
entry.links=[]
entry.links.append(Link(entry.id, _get_book_entry_rel(name), name, _get_book_entry_type(name)))
else:
entry.id = utils.connect_path(utils.connect_path(Config.SITE_BOOK_LIST, path), name)
#TODO add Another Links
links=fs.getdownloadurl(os.path.dirname(path), name)
entry.links=[]
if links !=None:
for link in links:
entry.links.append(Link(link, _get_book_entry_rel(link), name, _get_book_entry_type(link)))
entry.content = name
entry.title = name
entry.updated = utils.getNow()
return entry
def _get_book_entry_type(name):
"""
get link type
"""
if name.endswith(".pdf"):
return Const.book_type_pdf
elif name.endswith(".epub"):
return Const.book_type_epub
elif name.endswith(".jpg"):
return Const.book_type_picture
elif name.endswith(".mobi"):
return Const.book_type_mobi
elif name.endswith(".txt"):
return Const.book_type_text
elif name.find('.') != -1:
return Const.book_type_content
else:
# No subifx
return Const.book_type_entry_catalog
def _get_book_entry_rel(name):
"""
get link type
"""
if name.endswith(".pdf"):
return Const.book_link_rel__acquisition
elif name.endswith(".epub"):
return Const.book_link_rel__acquisition
elif name.endswith(".jpg"):
return Const.book_link_rel_image
elif name.endswith(".mobi"):
return Const.book_link_rel__acquisition
elif name.endswith(".txt"):
return Const.book_link_rel__acquisition
elif name.find('.') != -1:
return Const.book_link_rel_subsection
else:
# No subifx
return Const.book_link_rel_subsection
class FeedDoc:
def __init__(self, doc , path=None):
"""
Root Element
:param doc: Document()
:return:
"""
self.doc = doc
# xml-stylesheet
if fs.isfile(path):
self.doc.appendChild(self.doc.createProcessingInstruction("xml-stylesheet","type=\"text/xsl\" href=\"%s/static/bookdetail.xsl\""%Config.SITE_URL))
else:
self.doc.appendChild(self.doc.createProcessingInstruction("xml-stylesheet","type=\"text/xsl\" href=\"%s/static/booklist.xsl\""%Config.SITE_URL))
# feed
self.feed = self.doc.createElement("feed")
setfeedNS(self.feed)
self.addNode(self.feed, Const.id, Config.SITE_URL)
self.addNode(self.feed, Const.author, Config.SITE_EMAIL)
self.addNode(self.feed, Const.title, Config.SITE_TITLE)
self.addNode(self.feed, Const.updated, utils.getNow())
self.addNode(self.feed, Const.description, Config.description)
self.createLink(self.feed, Config.SITE_URL, "Home", "Home",
"application/atom+xml; profile=opds-catalog; kind=navigation")
self.doc.appendChild(self.feed)
pass
def addNode(self, element, key, value, link=None):
"""
add A node to element
:param element:
:param key:
:param value: can be str & Element
:param link: if is link ,this field is Not None.
:return:
"""
if isinstance(value, Element):
element.appendChild(value)
else:
node = self.doc.createElement(key)
node.appendChild(self.doc.createTextNode(value))
element.appendChild(node)
def toString(self):
# return self.doc.toxml("utf-8")
return self.doc.toprettyxml(encoding='utf-8')
def createEntry(self, entry):
entryNode = self.doc.createElement(Const.entry)
self.addNode(entryNode, Const.entry_title, entry.title)
self.addNode(entryNode, Const.entry_updated, entry.updated)
self.addNode(entryNode, Const.entry_id, entry.id)
self.addNode(entryNode, Const.entry_content, entry.content)
for link in entry.links:
self.createLink(entryNode, link.href, link.rel, link.title, link.type)
self.feed.appendChild(entryNode)
def createLink(self, entry, href, rel, title, type):
link = self.doc.createElement(Const.link)
link.setAttribute("href", href)
link.setAttribute("rel", rel)
link.setAttribute("title", title)
link.setAttribute("type", type)
text = self.doc.createTextNode(href)
link.appendChild(text)
entry.appendChild(link)
return link
class Entry:
def __init__(self, title=None, updated=None, id=None, content=None, links=[]):
self.links = links
self.content = content
self.id = id
self.updated = updated
self.title = title
class Link:
"""
Link Entity
"""
def __init__(self, href, rel, title, type):
self.href = href
self.rel = rel
self.title = title
self.type = type
class OpdsProtocol:
"""
All Opds File System Must Realized this Class
"""
def listBooks(self, path):
"""
:return: {entiry ...}
"""
rslist = []
#not exist!
if (path!='/' and not fs.exists(path)):
logging.info("dest Path [%s] is Not Exist." % path)
return rslist
if (fs.isfile(path)):
logging.info("dest Path [%s] is a File Not Right." % path)
g.book_process = "detail"
rslist.append(create__single_entry(True, path, os.path.basename(path)))
return rslist
bookmap = {}
for name in fs.listdir(path):
try:
name = name.decode("utf-8")
except Exception:
try:
name = name.decode("gbk")
except Exception as e:
pass
file_path = utils.connect_path(path, name)
rslist.append(create_entry(fs.isfile(file_path), path, name))
return rslist
def dowloadBook(self, path):
"""
file
:param path:
:return: file
"""
return utils.connect_path(Config.base, path)
def showhtml(self):
return ("No Realized")
pass