Skip to main content

A python-facing API for creating and interacting with ZIM files

Project description

python-libzim

libzim module allows you to read and write ZIM files in Python. It provides a shallow python interface on top of the C++ libzim library.

It is primarily used in openZIM scrapers like sotoki or youtube2zim.

Build Status CodeFactor License: GPL v3 PyPI version shields.io codecov

Installation

pip install libzim

The PyPI package is available for x86_64 macOS and GNU/Linux only. It bundles a recent release of the C++ libzim.

On other platforms, you'd have to compile C++ libzim from source first then build this one, adjusting LD_LIBRARY_PATH.

Contributions

git clone git@github.com:openzim/python-libzim.git && cd python-libzim
# python -m venv env && source env/bin/activate
pip install -U setuptools invoke
invoke download-libzim install-dev build-ext test
# invoke --list for available development helpers

See CONTRIBUTING.md for additional details then Open a ticket or submit a Pull Request on Github 🤗!

Usage

Read a ZIM file

from libzim.reader import Archive
from libzim.search import Query, Searcher
from libzim.suggestion import SuggestionSearcher

zim = Archive("test.zim")
print(f"Main entry is at {zim.main_entry.get_item().path}")
entry = zim.get_entry_by_path("home/fr")
print(f"Entry {entry.title} at {entry.path} is {entry.get_item().size}b.")
print(bytes(entry.get_item().content).decode("UTF-8"))

# searching using full-text index
search_string = "Welcome"
query = Query().set_query(search_string)
searcher = Searcher(zim)
search = searcher.search(query)
search_count = search.getEstimatedMatches()
print(f"there are {search_count} matches for {search_string}")
print(list(search.getResults(0, search_count)))

# accessing suggestions
search_string = "kiwix"
suggestion_searcher = SuggestionSearcher(zim)
suggestion = suggestion_searcher.suggest(search_string)
suggestion_count = suggestion.getEstimatedMatches()
print(f"there are {suggestion_count} matches for {search_string}")
print(list(suggestion.getResults(0, suggestion_count)))

Write a ZIM file

from libzim.writer import Creator, Item, StringProvider, FileProvider, Hint


class MyItem(Item):
    def __init__(self, title, path, content = "", fpath = None):
        super().__init__()
        self.path = path
        self.title = title
        self.content = content
        self.fpath = fpath

    def get_path(self):
        return self.path

    def get_title(self):
        return self.title

    def get_mimetype(self):
        return "text/html"

    def get_contentprovider(self):
        if self.fpath is not None:
            return FileProvider(self.fpath)
        return StringProvider(self.content)
       
    def get_hints(self):
        return {Hint.FRONT_ARTICLE: True}


content = """<html><head><meta charset="UTF-8"><title>Web Page Title</title></head>
<body><h1>Welcome to this ZIM</h1><p>Kiwix</p></body></html>"""

item = MyItem("Hello Kiwix", "home", content)
item2 = MyItem("Bonjour Kiwix", "home/fr", None, "home-fr.html")

with Creator("test.zim").config_indexing(True, "eng") as creator:
    creator.set_mainpath("home")
    creator.add_item(item)
    creator.add_item(item2)
    for name, value in {
        "creator": "python-libzim",
        "description": "Created in python",
        "name": "my-zim",
        "publisher": "You",
        "title": "Test ZIM",
    }.items():

        creator.add_metadata(name.title(), value)

License

GPLv3 or later, see LICENSE for more details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

libzim-2.1.0.tar.gz (8.3 MB view hashes)

Uploaded Source

Built Distributions

libzim-2.1.0-cp311-cp311-manylinux1_x86_64.whl (9.6 MB view hashes)

Uploaded CPython 3.11

libzim-2.1.0-cp311-cp311-macosx_10_9_x86_64.whl (8.0 MB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

libzim-2.1.0-cp310-cp310-manylinux1_x86_64.whl (9.6 MB view hashes)

Uploaded CPython 3.10

libzim-2.1.0-cp310-cp310-macosx_10_9_x86_64.whl (7.9 MB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

libzim-2.1.0-cp39-cp39-manylinux1_x86_64.whl (9.6 MB view hashes)

Uploaded CPython 3.9

libzim-2.1.0-cp39-cp39-macosx_10_9_x86_64.whl (7.8 MB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

libzim-2.1.0-cp38-cp38-manylinux1_x86_64.whl (9.6 MB view hashes)

Uploaded CPython 3.8

libzim-2.1.0-cp38-cp38-macosx_10_9_x86_64.whl (7.8 MB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

libzim-2.1.0-cp37-cp37m-manylinux1_x86_64.whl (9.5 MB view hashes)

Uploaded CPython 3.7m

libzim-2.1.0-cp37-cp37m-macosx_10_9_x86_64.whl (7.8 MB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

libzim-2.1.0-cp36-cp36m-manylinux1_x86_64.whl (9.5 MB view hashes)

Uploaded CPython 3.6m

libzim-2.1.0-cp36-cp36m-macosx_10_9_x86_64.whl (7.8 MB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page