Skip to content

Commit fa23dfc

Browse files
jdcaballerovmgautierfr
authored andcommitted
Add get_metadata dict to ZimReader
1 parent 7f98ff8 commit fa23dfc

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

pyzim/pyzim.pyx

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,6 @@ cdef class ZimArticle:
103103
# Set new internal C zim.ZimArticle article
104104
self.c_zim_article = art
105105

106-
# An article must have at least non empty url to be writable
107-
# Content can be empty if article is a redirect
108-
if self.longurl and (self.content or self.is_redirect):
109-
self._can_write = True
110-
else:
111-
self._can_write = False
112106

113107
def get_article_properties(self):
114108
return dict((name, getattr(self, name)) for name in dir(self) if not name.startswith('__') )
@@ -210,6 +204,8 @@ cdef class ZimArticle:
210204
@property
211205
def can_write(self):
212206
"""Get if the article is valid for writing"""
207+
# An article must have at least non empty url to be writable
208+
# Content can be empty if article is a redirect
213209
if self.longurl and (self.content or self.is_redirect):
214210
self._can_write = True
215211
else:
@@ -225,6 +221,23 @@ cdef class ZimReader:
225221
cdef zim.ZimSearch *c_search
226222
cdef object _filename
227223

224+
_metadata_keys =[
225+
"Name",
226+
"Title",
227+
"Creator",
228+
"Publisher",
229+
"Date",
230+
"Description",
231+
"Language",
232+
# Optional
233+
"LongDescription",
234+
"Licence",
235+
"Tags",
236+
"Flavour",
237+
"Source",
238+
"Counter",
239+
"Scraper"]
240+
228241
def __cinit__(self, str filename):
229242
self.c_file = new zim.File(filename.encode('UTF-8'))
230243
self.c_search = new zim.ZimSearch(self.c_file)
@@ -248,6 +261,17 @@ cdef class ZimReader:
248261
article = ZimArticle.from_read_article(art)
249262
return article
250263

264+
def get_metadata(self):
265+
metadata = dict()
266+
for key in self._metadata_keys:
267+
try:
268+
meta_art = self.get_article(f"M/{key}")
269+
except:
270+
pass
271+
else:
272+
metadata[key] = meta_art.content
273+
return metadata
274+
251275
def get_article_by_id(self, id):
252276
# Read to a zim::Article
253277
cdef zim.Article art = self.c_file.getArticle(<int> id)
@@ -330,7 +354,7 @@ cdef class ZimCreator:
330354

331355
_article_counter = defaultdict(int)
332356

333-
def __cinit__(self, str filename, str main_page = '', str index_language = 'eng', min_chunk_size = 2048):
357+
def __cinit__(self, str filename, str main_page = "", str index_language = "eng", min_chunk_size = 2048):
334358
self.c_creator = zim.ZimCreator.create(filename.encode("UTF-8"), main_page.encode("UTF-8"), index_language.encode("UTF-8"), min_chunk_size)
335359
self.set_metadata(date=datetime.date.today(), language= index_language)
336360
self._finalised = False

0 commit comments

Comments
 (0)