Search Library Genesis programmatically using a simple Python library.
Allows you to search Library Genesis by title or author, filter results, resolve download links, and get book cover images.
Acknowledgments
This project is a fork of the original libgen-api library created by Harrison Broadbent. We extend our sincere gratitude to the original author for creating this excellent library and making it open source. This fork builds upon the original work with additional features and improvements.
New Features
- Dynamic Domain Selection: Automatically finds and uses the fastest working LibGen mirror
- Book Cover Images: Added ability to fetch book cover images
- Improved Reliability: Better error handling and domain fallback
- Browser Simulation: Added realistic browser headers to prevent blocking
- Better Title Extraction: Improved accuracy in extracting book titles from search results
- Concurrent Domain Testing: Tests multiple domains simultaneously to find the fastest one
- Enhanced Error Handling: Better timeout and error management for domain testing
- Realistic Browser Headers: Comprehensive browser simulation to avoid detection
- Image Resolution: New method to extract book cover images from search results
Contents
- Getting Started
- Basic Searching
- Filtered Searching
- Resolving mirror links
- More Examples
- Further Information
- Testing
Please â� if you find this useful!
Getting Started
Install the package -
pip install libgen-api-refhub
Perform a basic search -
# search_title()
from libgen_api_refhub import LibgenSearch
s = LibgenSearch()
domains = [
"https://libgen.bz",
"https://libgen.li",
"https://libgen.la",
"https://libgen.vg",
"https://libgen.gs",
"https://libgen.gl",
]
fastest_domain = s.find_fastest_domain(domains)
results = s.search_title("Pride and Prejudice")
print(results)
Check out the results layout to see how the results data is formatted.
Basic Searching:
NOTE: All queries must be at least 3 characters long. This is to avoid any errors on the LibGen end (different mirrors have different requirements, but a minimum of 3 characters is the official limit).
Search by title or author:
Title:
# search_title()
from libgen_api_refhub import LibgenSearch
s = LibgenSearch()
domains = [
"https://libgen.bz",
"https://libgen.li",
"https://libgen.la",
"https://libgen.vg",
"https://libgen.gs",
"https://libgen.gl",
]
fastest_domain = s.find_fastest_domain(domains)
results = s.search_title("Pride and Prejudice")
print(results)
Author:
# search_author()
from libgen_api_refhub import LibgenSearch
s = LibgenSearch()
domains = [
"https://libgen.bz",
"https://libgen.li",
"https://libgen.la",
"https://libgen.vg",
"https://libgen.gs",
"https://libgen.gl",
]
fastest_domain = s.find_fastest_domain(domains)
results = s.search_author("Jane Austen")
print(results)
Filtered Searching
Skip to the Examples
- You can define a set of filters, and then use them to filter the search results that get returned.
- By default, filtering will remove results that do not match the filters exactly (case-sensitive) -
- This can be adjusted by setting
exact_match=Falsewhen calling one of the filter methods, which allows for case-insensitive and substring filtering.
- This can be adjusted by setting
Filtered Title Searching
# search_title_filtered()
from libgen_api_refhub import LibgenSearch
tf = LibgenSearch()
domains = [
"https://libgen.bz",
"https://libgen.li",
"https://libgen.la",
"https://libgen.vg",
"https://libgen.gs",
"https://libgen.gl",
]
fastest_domain = s.find_fastest_domain(domains)
title_filters = {"Year": "2007", "Extension": "epub"}
titles = tf.search_title_filtered("Pride and Prejudice", title_filters, exact_match=True)
print(titles)
Filtered Author Searching
# search_author_filtered()
from libgen_api_refhub import LibgenSearch
af = LibgenSearch()
domains = [
"https://libgen.bz",
"https://libgen.li",
"https://libgen.la",
"https://libgen.vg",
"https://libgen.gs",
"https://libgen.gl",
]
fastest_domain = s.find_fastest_domain(domains)
author_filters = {"Language": "German", "Year": "2009"}
titles = af.search_author_filtered("Agatha Christie", author_filters, exact_match=True)
print(titles)
Non-exact Filtered Searching
# search_author_filtered(exact_match = False)
from libgen_api_refhub import LibgenSearch
ne_af = LibgenSearch()
domains = [
"https://libgen.bz",
"https://libgen.li",
"https://libgen.la",
"https://libgen.vg",
"https://libgen.gs",
"https://libgen.gl",
]
fastest_domain = s.find_fastest_domain(domains)
partial_filters = {"Year": "200"}
titles = ne_af.search_author_filtered("Agatha Christie", partial_filters, exact_match=False)
print(titles)
Filter Fields
You can filter against any of the Library Genesis column names, which are given as -
col_names = [
"ID",
"Author",
"Title",
"Publisher",
"Year",
"Pages",
"Language",
"Size",
"Extension",
"Mirror_1",
"Mirror_2",
"Mirror_3",
"Mirror_4",
"Mirror_5",
"Edit",
]
Resolving mirror links
The mirror links returned in the results (ie. by running search_author() or search_title()) are not direct download links and do not resolve to a downloadable URL without further parsing.
An additional method, resolve_download_links(), can be to resolve the mirror links of a search item into direct download links.
The Mirror_1 field is used by resolve_download_links() as the results generally contain the most useful URLs.
This method accepts a single result (type: dictionary) from the array of searched results, and
returns a dictionary of all the download links for Mirror_1 (each mirror link has up to 4 download links):
# resolve_download_links()
from libgen_api_refhub import LibgenSearch
s = LibgenSearch()
domains = [
"https://libgen.bz",
"https://libgen.li",
"https://libgen.la",
"https://libgen.vg",
"https://libgen.gs",
"https://libgen.gl",
]
fastest_domain = s.find_fastest_domain(domains)
results = s.search_author("Jane Austen")
item_to_download = results[0]
download_links = s.resolve_download_links(item_to_download)
print(download_links)
Example output:
{
"GET": "http://example.com/file.epub",
"Cloudflare": "http://example.com/file.epub",
"IPFS.io": "http://example.com/file.epub",
"Infura": "http://example.com/file.epub"
}
Resolving book cover images
You can also extract book cover images from search results using the resolve_image() method.
This method accepts a single result (type: dictionary) from the array of searched results, and returns the URL of the book cover image if available:
# resolve_image()
from libgen_api_refhub import LibgenSearch
s = LibgenSearch()
results = s.search_author("Jane Austen")
item_to_get_image = results[0]
image_url = s.resolve_image(item_to_get_image)
print(image_url)
Example output:
http://example.com/covers/book_cover.jpg
Dynamic domain selection
The library now includes a method to automatically find the fastest working LibGen mirror from a list of domains:
# find_fastest_domain()
from libgen_api_refhub import LibgenSearch
s = LibgenSearch()
domains = [
"https://libgen.bz",
"https://libgen.li",
"https://libgen.la",
"https://libgen.vg",
"https://libgen.gs",
"https://libgen.gl",
]
fastest_domain = s.find_fastest_domain(domains)
print(f"Using fastest domain: {fastest_domain}")
This method tests all domains concurrently and automatically sets the fastest responding domain for future requests.
Enhanced browser simulation
The library now includes comprehensive browser simulation to avoid detection and blocking:
- Realistic User-Agent: Uses Firefox browser headers
- Complete HTTP Headers: Includes Accept, Accept-Language, Accept-Encoding, and other standard headers
- Security Headers: Implements DNT, Sec-Fetch-* headers for modern browser compatibility
- Connection Management: Proper keep-alive and cache control headers
This ensures better compatibility with LibGen mirrors and reduces the likelihood of being blocked.
More Examples
See the testing file for more examples.
Results Layout
Results are returned as a list of dictionaries:
[
{
"Author": "John Smith",
"Edit": "http://example.com",
"Extension": "epub",
"ID": "00000",
"Language": "German",
"Mirror_1": "http://example.com",
"Mirror_2": "http://example.com",
"Mirror_3": "http://example.com",
"Mirror_4": "http://example.com",
"Mirror_5": "http://example.com",
"Pages": "410",
"Publisher": "Publisher",
"Size": "1005 Kb",
"Title": "Title",
"Year": "2021"
}
]
Further information
- If there are no results, the library will return an empty array.
- All fields are strings.
- If a value is not present, the field will contain an empty string.
- Some listings will have page count listed in the form of "count[secondary-count]" as this is how they appear on Library Genesis.
- Only the first page of results (max. 25) will be returned.
Testing
libgen-api-refhub uses Pytest to run unit tests.
To run the tests -
-
Clone this repo -
git clone https://github.com/Merkousha/libgen-api-refhub.git && cd libgen-api-refhub -
Install dependencies with -
pip install . -
Run tests with -
pytest
Contributors
A massive thank you to those that have contributed to this project!
Please don't hesitate to raise an issue, or fork this project and improve on it.
Thanks to the following contributors -
New Distribution
(for my own reference)
- python3 -m pip install --user --upgrade setuptools wheel
- python3 -m pip install --user --upgrade twine
- python3 setup.py sdist bdist_wheel
- python3 -m twine upload dist/*
Metadata
Release files for libgen_api_refhub 1.0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| libgen_api_refhub-1.0.0.1.tar.gz | 12.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| libgen_api_refhub-1.0.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 21.2 kB
Release files / libgen_api_refhub-1.0.0.1.tar.gz
| Download URL | libgen_api_refhub-1.0.0.1.tar.gz |
|---|---|
| Size | 12.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a6093595a4b35cab9b000015e356b0e3cd55567180ee3628ea9b5ce9a7855f1e
|
|
BLAKE2b-256 checksum How to use checksums |
ee94e95454da4400876610c1c56633677c735f4025ae37b59aa7ab89b60e6935
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.6
|
Release files / libgen_api_refhub-1.0.0.1-py3-none-any.whl
| Download URL | libgen_api_refhub-1.0.0.1-py3-none-any.whl |
|---|---|
| Size | 9.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e4276a75dec0a941dc52afb2d8e7da6857ba2bd5010a0dfb931b543c1f30c20d
|
|
BLAKE2b-256 checksum How to use checksums |
fda30fef01b00e2eb97d54c71480c4308665145c4de1945f26823c5e2e5d833e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.6
|