Skip to content

zimscraperlib.zim.archive

ZIM Archive helper

Convenient subclass of libzim.reader.Archive with: - direct access to Item from path - direct access to suggestions and suggestions count - direct access to search results and number of results - public Entry access by Id

Classes:

Archive

Bases: Archive

Methods:

Attributes:

counters property

counters: CounterMap

metadata property

metadata: dict[str, str]

key: value for all non-illustration metadata listed in .metadata_keys

tags property

tags

get_content

get_content(path: str) -> bytes

Actual content from a path

Source code in src/zimscraperlib/zim/archive.py
68
69
70
def get_content(self, path: str) -> bytes:
    """Actual content from a path"""
    return bytes(self.get_item(path).content)

get_entry_by_id

get_entry_by_id(id_: int) -> Entry

Entry from its Id in ZIM

Source code in src/zimscraperlib/zim/archive.py
60
61
62
def get_entry_by_id(self, id_: int) -> libzim.reader.Entry:
    """Entry from its Id in ZIM"""
    return self._get_entry_by_id(id_)

get_item

get_item(path: str) -> Item

Item from a path

Source code in src/zimscraperlib/zim/archive.py
64
65
66
def get_item(self, path: str) -> libzim.reader.Item:
    """Item from a path"""
    return self.get_entry_by_path(path).get_item()

get_search_results

get_search_results(
    query: str, start: int = 0, end: int | None = None
) -> Iterable[str]

paths iterator over search results for query

Source code in src/zimscraperlib/zim/archive.py
86
87
88
89
90
91
92
93
94
95
def get_search_results(
    self, query: str, start: int = 0, end: int | None = None
) -> Iterable[str]:
    """paths iterator over search results for query"""
    search = libzim.search.Searcher(self).search(
        libzim.search.Query().set_query(query)
    )
    if end is None:
        end = search.getEstimatedMatches()
    return search.getResults(start, end)

get_search_results_count

get_search_results_count(query: str) -> int

Estimated number of search results for query

Source code in src/zimscraperlib/zim/archive.py
 97
 98
 99
100
101
102
def get_search_results_count(self, query: str) -> int:
    """Estimated number of search results for query"""
    search = libzim.search.Searcher(self).search(
        libzim.search.Query().set_query(query)
    )
    return search.getEstimatedMatches()

get_suggestions

get_suggestions(
    query: str, start: int = 0, end: int | None = None
) -> Iterable[str]

paths iterator over suggestion matches for query

Source code in src/zimscraperlib/zim/archive.py
72
73
74
75
76
77
78
79
def get_suggestions(
    self, query: str, start: int = 0, end: int | None = None
) -> Iterable[str]:
    """paths iterator over suggestion matches for query"""
    suggestion = libzim.suggestion.SuggestionSearcher(self).suggest(query)
    if end is None:
        end = suggestion.getEstimatedMatches()
    return suggestion.getResults(start, end)

get_suggestions_count

get_suggestions_count(query: str) -> int

Estimated number of suggestion matches for query

Source code in src/zimscraperlib/zim/archive.py
81
82
83
84
def get_suggestions_count(self, query: str) -> int:
    """Estimated number of suggestion matches for query"""
    suggestion = libzim.suggestion.SuggestionSearcher(self).suggest(query)
    return suggestion.getEstimatedMatches()

get_tags

get_tags(*, libkiwix: bool = False) -> list[str]

List of ZIM tags, optionnaly expanded with libkiwix's hints

Source code in src/zimscraperlib/zim/archive.py
44
45
46
47
48
49
50
51
52
53
54
def get_tags(self, *, libkiwix: bool = False) -> list[str]:
    """List of ZIM tags, optionnaly expanded with libkiwix's hints"""
    try:
        tags_meta = self.get_text_metadata("Tags")
    except RuntimeError:  # pragma: no cover
        tags_meta = ""

    if libkiwix:
        return convertTags(tags_meta)

    return tags_meta.split(";")

get_text_metadata

get_text_metadata(name: str) -> str

Decoded value of a text metadata

Source code in src/zimscraperlib/zim/archive.py
56
57
58
def get_text_metadata(self, name: str) -> str:
    """Decoded value of a text metadata"""
    return super().get_metadata(name).decode("UTF-8")