Skip to content

Commit 77d1c78

Browse files
authored
Add Python usage examples for libbbf
Added examples for using libbbf in Python, including creating and verifying .bbf files.
1 parent 759b1d0 commit 77d1c78

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

readme.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,60 @@ bbf2cbx <input.bbf> [options] --output <output.cbz_or_dir>
116116
bbf2cbx series.bbf --info
117117
```
118118
119+
## Using `libbbf`
120+
121+
You can also interface libbbf directly from python.
122+
123+
Example 1: Creating a .bbf file
124+
```python
125+
from libbbf import BBFBuilder
126+
import os
127+
128+
# Create a new Bound Book
129+
builder = BBFBuilder("my_comic.bbf")
130+
131+
# Add Metadata
132+
builder.add_metadata("Title", "Solo Leveling")
133+
builder.add_metadata("Author", "Chugong")
134+
135+
# Add Pages (Assets are automatically deduplicated by hash!)
136+
page_files = sorted(os.listdir("./chapter_images"))
137+
for i, filename in enumerate(page_files):
138+
full_path = os.path.join("./chapter_images", filename)
139+
140+
# Flags: 0 = Standard
141+
builder.add_page(full_path, type=1, flags=0)
142+
143+
# Define a Chapter every 20 pages
144+
if i % 20 == 0:
145+
builder.add_section(f"Chapter {i // 20 + 1}", start_page=i)
146+
147+
# Finalize writes the footer and optimized tables
148+
builder.finalize()
149+
print("BBF created successfully.")
150+
```
151+
152+
Example 2: Verifying a .bbf file
153+
```python
154+
import libbbf
155+
import time
156+
157+
reader = libbbf.BBFReader("massive_archive.bbf")
158+
159+
print("Verifying file integrity...")
160+
start = time.time()
161+
162+
# This releases the GIL, so it's thread-safe and incredibly fast
163+
is_valid = reader.verify()
164+
165+
end = time.time()
166+
167+
if is_valid:
168+
print(f"SUCCESS: Integrity verified in {end - start:.4f}s")
169+
else:
170+
print("ERROR: Corruption detected!")
171+
```
172+
119173
## Contributing
120174

121175
Contributions, issues, and feature requests are welcome! For libbbf, free to check the [issues page](https://github.com/ef1500/libbbf/issues) (or create one if it doesn't exist yet).

0 commit comments

Comments
 (0)