Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
f67e296
Initial version python 2.7
jdcaballerov Mar 31, 2020
537759a
Change Cython language version
jdcaballerov Mar 31, 2020
20c32c8
Python 3 corrections
jdcaballerov Mar 31, 2020
8353a40
Revert "Change Cython language version"
jdcaballerov Mar 31, 2020
78480be
Rename README.rst -> README.md
jdcaballerov Mar 31, 2020
1b054f0
Change Readme.rst -> Readme.md in setup.py
jdcaballerov Apr 1, 2020
9f74ba7
Refactor to use Cython properties
jdcaballerov Apr 2, 2020
7a7d442
Delete test.py
jdcaballerov Apr 2, 2020
fe7452c
Update all the examples cookbook
jdcaballerov Apr 2, 2020
57d1665
Add get_metadata dict to ZimReader
jdcaballerov Apr 2, 2020
60faf37
Delete tests files at start and ending
jdcaballerov Apr 2, 2020
d835898
fix README formatting
pirate Apr 6, 2020
73a7a64
Delete add_art from example
jdcaballerov Apr 6, 2020
a75bf02
Remove u identifier from example strings
jdcaballerov Apr 6, 2020
92a9c26
Remove u identifier in some examples.py strings
jdcaballerov Apr 6, 2020
ba9069c
Update README.md sections
jdcaballerov Apr 7, 2020
589e608
Add Dependencies to README.md
jdcaballerov Apr 8, 2020
4f15587
Add generated cython file
jdcaballerov Apr 8, 2020
f6d9938
Update and add docstrings
jdcaballerov Apr 8, 2020
0e9c65a
Update docstrings
jdcaballerov Apr 8, 2020
d3c74af
Add exception to add_article if it was finalised
jdcaballerov Apr 9, 2020
00f6a6d
Remove get_article_properties()
jdcaballerov Apr 9, 2020
0486802
Allow bytes data for article content
jdcaballerov Apr 9, 2020
8d53a5d
Change finalise for finalize everywhere
jdcaballerov Apr 9, 2020
dba4944
Change finalise for finalize pyzim.pyx
jdcaballerov Apr 9, 2020
ef034b1
Add repr to ZimArticle
jdcaballerov Apr 9, 2020
f1aa8a9
Add repr to ZimReader and ZimCreator
jdcaballerov Apr 9, 2020
8b3533b
Catch all C++ exceptions in cython
jdcaballerov Apr 9, 2020
10be212
Remove unused member function get_article_properties
jdcaballerov Apr 9, 2020
00e8eb8
Add property getters for ZimCreator parameters
jdcaballerov Apr 10, 2020
9e45d5c
Add feature mainPage setter
jdcaballerov Apr 10, 2020
b4efd90
Add independent metadata dict for each Creator
jdcaballerov Apr 10, 2020
51e348c
Add virtual default destructor to ZimArticle and missing virtual to m…
jdcaballerov Apr 10, 2020
5128f91
Mark get_metadata member function as internal for ZimCreator
jdcaballerov Apr 10, 2020
9cb954b
Change ZimReader get_metadata() name and to return only mandatory met…
jdcaballerov Apr 10, 2020
6903615
Add c++ 11
jdcaballerov Apr 11, 2020
be3df1d
Update setup.py
pirate Apr 12, 2020
49389e8
Update setup.py
pirate Apr 12, 2020
40c0035
WIP new API search and suggestions
jdcaballerov Apr 13, 2020
aaffabf
Merge branch 'dev' of github.com:openzim/python-libzim into dev
jdcaballerov Apr 13, 2020
054e763
Add search, suggestions iterators and counts
jdcaballerov Apr 13, 2020
e477e12
Change pyzim for libzim everywhere
jdcaballerov Apr 13, 2020
45f168f
Change set_metada for update_metadata for Creator, update docstrings
jdcaballerov Apr 14, 2020
279186b
Add except to try
jdcaballerov Apr 14, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
FROM ubuntu:bionic

# Update system
RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections

# Configure locales
RUN apt-get update -y && \
apt-get install -y --no-install-recommends locales && \
apt-get clean -y && \
rm -rf /var/lib/apt/lists/*
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
RUN locale-gen en_US.UTF-8

# Install necessary packages
RUN apt-get update -y && \
apt-get install -y --no-install-recommends git pkg-config libtool automake autoconf make g++ liblzma-dev coreutils meson ninja-build wget zlib1g-dev libicu-dev libgumbo-dev libmagic-dev ca-certificates && \
apt-get clean -y && \
rm -rf /var/lib/apt/lists/*

# Update CA certificates
RUN update-ca-certificates

# Install Xapian (wget zlib1g-dev)
RUN wget https://oligarchy.co.uk/xapian/1.4.14/xapian-core-1.4.14.tar.xz
RUN tar xvf xapian-core-1.4.14.tar.xz
RUN cd xapian-core-1.4.14 && ./configure
RUN cd xapian-core-1.4.14 && make all install
RUN rm -rf xapian

# Install zimlib (libicu-dev)
RUN git clone https://github.com/openzim/libzim.git
RUN cd libzim && git checkout 6.0.2
RUN cd libzim && meson . build
RUN cd libzim && ninja -C build install
RUN rm -rf libzim

RUN ldconfig
ENV LD_LIBRARY_PATH /usr/local/lib/x86_64-linux-gnu/

# Install python dependecies

RUN apt-get update -y && \
apt-get install -y --no-install-recommends python-dev python3-dev python3-pip && \
apt-get clean -y && \
rm -rf /var/lib/apt/lists/*

# Install Cython

RUN pip3 install Cython
106 changes: 106 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@

python-libzim
===========

This is the python binding to the [libzim](https://github.com/openzim/libzim). Read and write
[ZIM](https://openzim.org) files easily in Python.

## Dependencies

This package uses [Cython](https://github.com/cython/cython).

You will need to install [libzim](https://github.com/openzim/libzim).



## Setup

```bash
docker-compose build
docker-compose run libzim /bin/bash
```
```bash
python setup.py build_ext -i
python tests/test_libzim.py

# or

./rebuild.sh
./run_tests
```

## Usage

### Writing a Zim file

```python
import libzim

article = libzim.ZimArticle()

# article content

article_title = "Monadical SAS"
article_url = "Monadical_SAS"
article_longurl ="A/Monadical_SAS"
article_mimetype = "text/html"
article_content = '''<!DOCTYPE html> <html class="client-js"><head><meta charset="UTF-8">
<title>Monadical SAS</title> <h1> Hello, it works Monadical ñññ </h1></html>'''

article.title = article_title
article.url = article_url
article.mimetype = article_mimetype
article.content = article_content


import uuid

rnd_str = str(uuid.uuid1())
test_zim_file_path = "/opt/python-libzim/tests/kiwix-test"
zim_creator = libzim.ZimCreator(test_zim_file_path + '-' + rnd_str + '.zim',"welcome","spa",2048)




# Add and write article to second test zim file
zim_creator.add_article(article)
zim_creator.finalize()
```

### Reading a Zim file

```python
import libzim

# Read an article from a zim file

zim_file_path = "/opt/python-libzim/tests/wikipedia_es_physics_mini.zim"
zim_reader = libzim.ZimReader(zim_file_path)
zim_test_article_long_url = "A/Albert_Einstein"

read_article = zim_reader.get_article(zim_test_article_long_url)

# Read article properties

print(read_article.longurl)
print (read_article.title)
print(read_article.is_redirect)
print(read_article.can_write)
print(read_article.content[:100])

# Search or get suggestions from a zim file
search = zim_reader.search("Einstein")
print(search)
suggestions = zim_reader.suggest("Einstein")
print(suggestions)

```

## Cookbook

Visit [examples.py](libzim/examples.py)

## License

[GPLv3](https://www.gnu.org/licenses/gpl-3.0) or later, see
[LICENSE](LICENSE) for more details.
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: '3'

services:
libzim:
build:
context: .
dockerfile: ./Dockerfile
image: kiwix:python-libzim
working_dir: /opt/python-libzim
stdin_open: true
tty: true
volumes:
- .:/opt/python-libzim
127 changes: 127 additions & 0 deletions libzim/examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import libzim

test_content = '''<!DOCTYPE html>
<html class="client-js">
<head><meta charset="UTF-8">
<title>Monadical</title>
<h1> ñññ Hello, it works ñññ </h1></html>'''


# Read an article from a zim file

zim_file_path = "/opt/python-libzim/tests/wikipedia_es_physics_mini.zim"
zim_reader = libzim.ZimReader(zim_file_path)
zim_test_article_long_url = "A/Albert_Einstein"

read_article = zim_reader.get_article(zim_test_article_long_url)

# Read article properties

print(read_article.longurl)
print (read_article.title)
print(read_article.is_redirect)
print(read_article.can_write)
print(read_article.content[:100])

# Search or get suggestions from a zim file
search = zim_reader.search("Einstein")
print(search)
suggestions = zim_reader.suggest("Einstein")
print(suggestions)

# Get the total number of articles
print (f"Number of articles: {str(zim_reader.get_article_count())}")

# Get the file namespaces

namespaces = zim_reader.get_namespaces()
print(f'Namespaces: {namespaces}')
for ns in namespaces:
print(f"Namespace: {ns} | Count: {str(zim_reader.get_namespaces_count(ns))}")

# Get the file main page

print(f"Main Page URL: {zim_reader.get_main_page_url()}")


# Create a filled article

article = libzim.ZimArticle(namespace="A", url = "Monadical", title="Monadical", content=test_content, should_index = True)

print(article.longurl)
print(article.url)

# Create an empty article then fill it
article2 = libzim.ZimArticle()

article2.content = test_content
article2.url = "Monadical_SAS"
article2.title = "Monadical SAS"

# Fill an article from a read article

article3 = libzim.ZimArticle()

article3.content = read_article.content
article3.url = "Our_Einstein"
article3.title = "Our Einstein's bio"

# Create a redirect article

article4 = libzim.ZimArticle()
article4.namespace = "A"
article4.url = "Our_Einstein_redirect"
article4.title = "Redirect to Einstein"
article4.redirect_url = read_article.url



# Write the articles
import uuid
rnd_str = str(uuid.uuid1())

test_zim_file_path = "/opt/python-libzim/tests/kiwix-test"

zim_creator = libzim.ZimCreator(test_zim_file_path + '-' + rnd_str + '.zim',main_page = "welcome",index_language= "eng", min_chunk_size= 2048)

# Add article to zim file
zim_creator.add_article(article)
zim_creator.add_article(article2)
zim_creator.add_article(article3)
zim_creator.add_article(article4)
zim_creator.add_article(read_article)

# Write articles to zim file
zim_creator.finalize()

# Read written articles

zim_reader = libzim.ZimReader(test_zim_file_path + '-' + rnd_str + '.zim')
zim_test_article_long_url = "A/Monadical"

written_article = zim_reader.get_article(zim_test_article_long_url)
redirect_article = zim_reader.get_article("A/Our_Einstein_redirect")
print(f"Is {redirect_article.longurl} a redirect: {redirect_article.is_redirect}")

# Get redirected Article
redirected = zim_reader.get_redirect_article(redirect_article)
print(redirected.longurl)

# Read written metadata by url

date_metadata = zim_reader.get_article("M/Date")
print(f"Date Metadata: {date_metadata.content}")

counter_metadata = zim_reader.get_article("M/Counter")
# Redirect articles are not counted
print(f"Counter Metadata: {counter_metadata.content}")

# Get all the mandatory file metadata as dict
metadata = zim_reader.get_mandatory_metadata()

print(f"Metadata: {metadata}")

# Get the file checksum

print(f"File: {zim_reader.filename} \nChecksum: {zim_reader.get_checksum()}")

Loading