A Python library for programmatic access to Library Genesis
Project description
ResearchMe
researchme is a Python library that provides a programmatic interface for accessing Library Genesis, allowing users to search and retrieve metadata about books, as well as download available resources. This library is useful for automating access to Library Genesis content, such as searching by title or author, retrieving detailed book information, and filtering results based on various criteria.
Features
- Search Library Genesis: Search for books and other materials using fields like title, author, ISBN, year, etc.
- Retrieve Metadata: Get detailed information about each item, including author, publisher, year, language, page count, and download links.
- Download Links: Programmatically resolve direct download links for available content.
- Flexible Filtering: Apply filters based on fields and categories to refine search results.
Installation
You can install researchme directly from PyPI:
pip install researchme
Usage
Below are examples of how to use researchme for common tasks such as searching for books and retrieving metadata.
Basic Example: Searching for Books
from researchme.libgen import Mirror1
# Initialize the Mirror1 class
mirror = Mirror1()
# Define search fields (e.g., search in title and author fields)
fields = mirror.search_fields(title=True, authors=True)
# Perform a search by title and author
mirror.search(query="Artificial Intelligence", search_by=fields)
# Retrieve metadata for the first 50 results
metadata = mirror.get_metadata(max_entries=50)
# Display metadata
for entry in metadata:
print(entry)
Using search_categories to Filter by Categories
The search_categories method allows you to specify categories, such as fiction, scientific articles, or magazines, to filter your search results.
from researchme.libgen import Mirror1
# Initialize the Mirror1 class
mirror = Mirror1()
# Define search categories (e.g., search in fiction and scientific articles)
categories = mirror.search_categories(fiction=True, scientific_articles=True)
# Perform a search with category filters
mirror.search(query="Machine Learning", categories=categories)
# Retrieve metadata for the first 20 results
metadata = mirror.get_metadata(max_entries=20)
# Display metadata
for entry in metadata:
print(entry)
Retrieving JSON Data
The get_json method retrieves additional metadata in JSON format, allowing for easy access to fields such as MD5 hash or other metadata identifiers.
# Retrieve JSON data from the initialized session
json_data = mirror.get_json()
# Access and print each item ID with its MD5 hash
for item_id, item_data in json_data.items():
print(item_id, item_data['md5'])
In this example, the search_categories method constructs category-specific URL filters to narrow the search results to specific types of content.
Filtering Search Results
The filtered method allows for filtering metadata based on specific criteria, such as author name or publication year.
# Example of filtering results by author and year
filtered_results = mirror.filtered(metadata, authors="John Shawe-Taylor", year="1999")
for entry in filtered_results:
print(entry)
Resolving a Download Link
To get a direct download link for a specific item:
# Get the first download URL from first entry of the metadata list
url = metadata[0]['content_url'][0]
# Resolve the full download link by passing the URL to the resolve_download method
download_link = mirror.resolve_download(url)
print(f"Resolved Download Link: {download_link}")
API Reference
Mirror1
The Mirror1 class provides core functionalities for interacting with Library Genesis, such as searching, retrieving metadata, and downloading content.
search(query: str, search_by=None, categories=None, max_results=100, page=1)
Initiates a search on Library Genesis based on a query and optional filters for specific fields or categories.
-
Parameters:
query(str, required): The main search term, such as a book title, author, or keyword.search_by(list, optional): A list of search field filters (e.g., title, author) generated by thesearch_fieldsmethod. If not provided, all fields will be searched by default.categories(list, optional): A list of category filters (e.g., fiction, scientific articles) generated by thesearch_categoriesmethod. If not specified, all categories are included in the search by default.max_results(int, optional): Limits the number of results to either 25, 50, or 100. Defaults to 100. If an invalid value is provided, the library defaults to 100 results.page(int, optional): Specifies the page number for pagination of results. Defaults to 1.
-
Returns: Initializes the session and stores the parsed HTML content, which can later be accessed using
get_metadata().
get_metadata(max_entries=100)
Retrieves metadata from the previously initialized search results, including fields such as title, author, publisher, year, language, and download links.
-
Parameters:
max_entries(int, optional): The maximum number of metadata entries to retrieve, with a default of 100.
-
Returns: A list of dictionaries, each containing metadata for an item found in the search results.
get_json()
Fetches and returns a formatted JSON representation of additional metadata if available. Each key in the JSON data represents an item ID, with values as dictionaries containing metadata fields, such as the MD5 hash.
-
Parameters: None
-
Returns: A dictionary in JSON format, where each key represents an item ID, and the value is a dictionary containing metadata details like
md5and other fields. -
Example Usage:
resolve_download(url: str)
Resolves and returns the direct download link for a specified item URL from Library Genesis.
-
Parameters:
url(str, required): The URL of the Library Genesis page containing the item to be downloaded.
-
Returns: A direct download link for the specified item, or
Noneif the download link cannot be resolved.
filtered(metadata, title='', authors='', language='', year='', publisher='')
Filters a metadata list based on specified criteria, allowing further refinement of search results.
-
Parameters:
metadata(list, required): The metadata list returned byget_metadata().title(str, optional): Filters results to include only items with titles that contain this string.authors(str, optional): Filters results to include only items with authors matching this string.language(str, optional): Filters results to include only items with languages matching this string.year(str, optional): Filters results to include only items published in this year.publisher(str, optional): Filters results to include only items from this publisher.
-
Returns: A filtered list of metadata dictionaries based on the specified criteria.
Static Methods for Search Parameters
The following static methods help create parameter filters to use with the search() method.
search_fields(title=False, authors=False, series=False, year=False, publisher=False, isbn=False)
Constructs a list of search field filters to be applied in a search. By default, no fields are filtered unless specified.
-
Parameters:
title(bool, optional): Filter by title field ifTrue.authors(bool, optional): Filter by author field ifTrue.series(bool, optional): Filter by series field ifTrue.year(bool, optional): Filter by publication year ifTrue.publisher(bool, optional): Filter by publisher field ifTrue.isbn(bool, optional): Filter by ISBN field ifTrue.
-
Returns: A list of field filter strings to be used as the
search_byparameter in thesearch()method.
search_categories(libgen=False, comics=False, fiction=False, scientific_articles=False, magazines=False, fiction_russian=False, standards=False)
Constructs a list of category filters to use in a search, allowing for more specific results based on content type.
-
Parameters:
libgen(bool, optional): Include the general Library Genesis database ifTrue.comics(bool, optional): Include comics in the search ifTrue.fiction(bool, optional): Include fiction books in the search ifTrue.scientific_articles(bool, optional): Include scientific articles in the search ifTrue.magazines(bool, optional): Include magazines in the search ifTrue.fiction_russian(bool, optional): Include Russian fiction ifTrue.standards(bool, optional): Include standards and technical documents ifTrue.
-
Returns: A list of category filter strings to be used as the
categoriesparameter in thesearch()method.
Error Handling
The library logs and handles common errors to ensure stable scraping and session management, including:
- Request Errors: HTTP errors are caught and logged to prevent interruptions.
- Parsing Errors: Any errors during HTML parsing are logged, allowing the library to continue functioning without breaking.
Contributing
Contributions are welcome! If you have suggestions for improving researchme, please submit a pull request or open an issue.
License
researchme is licensed under the MIT License. See the LICENSE file for more details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file researchme-0.1.1.tar.gz.
File metadata
- Download URL: researchme-0.1.1.tar.gz
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
25ae9172efc24246af3d32cf2e7f7f220b64dd89dfd46bab117e502b09d165c2
|
|
| MD5 |
2d295cbc1d8e80404f3ec95957ead38f
|
|
| BLAKE2b-256 |
c86ac1a6040c9d530e083a92523c8c6b05dd00822f54e5a13b04df680fb715bf
|
File details
Details for the file ResearchMe-0.1.1-py3-none-any.whl.
File metadata
- Download URL: ResearchMe-0.1.1-py3-none-any.whl
- Upload date:
- Size: 10.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4b391defaf0c6db85aa98c18331dfef31be7bc7a0e9d7faa6e356e4929f418cc
|
|
| MD5 |
c4974739ab6c79c2b8ef6c917d452df2
|
|
| BLAKE2b-256 |
ac04f916e02854defd7aa7605f7837fd1b3e1238ea35b918793773746ca00c7e
|