Skip to main content

A Python client for accessing Grokipedia content

Project description

Grokipedia API

A Python client for accessing content from Grokipedia, an open-source, comprehensive collection of all knowledge.

Features

  • Search: Search through Grokipedia's vast collection of articles
  • Page Retrieval: Get full page content, including headings, text, and citations
  • Structured Data: Access well-structured data with proper typing
  • Easy to Use: Simple and intuitive API

Installation

Install from PyPI (when available):

pip install grokipedia-api

Or install from source:

git clone https://github.com/yourusername/grokipedia-api.git
cd grokipedia-api
pip install -e .

Quick Start

Basic Usage

from grokipedia_api import GrokipediaClient

# Create a client
client = GrokipediaClient()

# Search for articles
results = client.search("Python programming")
print(f"Found {len(results['results'])} results")

# Get a specific page
page = client.get_page("United_Petroleum")
print(f"Title: {page['page']['title']}")
print(f"Content: {page['page']['content'][:200]}...")

Search Functionality

from grokipedia_api import GrokipediaClient

client = GrokipediaClient()

# Search with pagination
results = client.search("machine learning", limit=20, offset=0)
for result in results['results']:
    print(f"- {result['title']}")
    print(f"  Slug: {result['slug']}")
    print(f"  Views: {result['viewCount']}")
    print()

Get Full Page Content

from grokipedia_api import GrokipediaClient

client = GrokipediaClient()

# Get full page with all content
page = client.get_page("United_Petroleum", include_content=True)

# Access structured data
title = page['page']['title']
content = page['page']['content']
citations = page['page']['citations']

print(f"Article: {title}")
print(f"\nCitations: {len(citations)}")
for citation in citations:
    print(f"- [{citation['id']}] {citation['title']}")
    print(f"  {citation['url']}")

Using Context Manager

from grokipedia_api import GrokipediaClient

# Use context manager for automatic cleanup
with GrokipediaClient() as client:
    results = client.search("Python")
    for result in results['results']:
        print(result['title'])

API Reference

GrokipediaClient

The main client class for interacting with Grokipedia.

Methods

search(query, limit=12, offset=0)

Search for articles in Grokipedia.

Parameters:

  • query (str): Search query string
  • limit (int): Maximum number of results to return (default: 12)
  • offset (int): Number of results to skip for pagination (default: 0)

Returns:

  • Dictionary containing:
    • results: List of search result dictionaries
    • total_count: Total number of results (if available)

Example:

results = client.search("Python programming", limit=20)
get_page(slug, include_content=True, validate_links=True)

Get a specific page by its slug.

Parameters:

  • slug (str): Page slug (e.g., "United_Petroleum")
  • include_content (bool): Whether to include full content (default: True)
  • validate_links (bool): Whether to validate links (default: True)

Returns:

  • Dictionary containing:
    • page: Page information including title, content, citations, images, etc.
    • found: Boolean indicating if the page was found

Example:

page = client.get_page("Python_(programming_language)")
search_pages(query, limit=12)

Search for pages and return results as a list.

Parameters:

  • query (str): Search query string
  • limit (int): Maximum number of results to return (default: 12)

Returns:

  • List of search result dictionaries

Example:

pages = client.search_pages("machine learning")

Data Models

The library provides structured data models for working with Grokipedia content:

  • Page: Represents a full Grokipedia page
  • Citation: Represents a citation in an article
  • Image: Represents an image in an article
  • SearchResult: Represents a search result

Error Handling

The library provides custom exceptions:

  • GrokipediaError: Base exception for all Grokipedia errors
  • GrokipediaNotFoundError: Raised when a requested resource is not found
  • GrokipediaAPIError: Raised when there's an API-related error
from grokipedia_api import GrokipediaClient
from grokipedia_api.exceptions import GrokipediaNotFoundError

client = GrokipediaClient()

try:
    page = client.get_page("NonExistentPage")
except GrokipediaNotFoundError:
    print("Page not found!")

Development

Setup Development Environment

# Clone the repository
git clone https://github.com/yourusername/grokipedia-api.git
cd grokipedia-api

# Install in development mode with dev dependencies
pip install -e ".[dev]"

Running Tests

# Run all tests
pytest

# Run tests with coverage
pytest --cov=grokipedia_api --cov-report=html

Code Style

This project uses:

  • Black for code formatting
  • Ruff for linting
  • mypy for type checking
# Format code
black grokipedia_api tests

# Lint code
ruff check grokipedia_api tests

# Type check
mypy grokipedia_api

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Grokipedia for providing the amazing knowledge base
  • All the contributors and maintainers

Support

If you encounter any issues or have questions, please open an issue on GitHub.

Changelog

0.1.0 (2025-01-09)

  • Initial release
  • Search functionality
  • Page retrieval with full content
  • Structured data models
  • Basic error handling

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

grokipedia_api-0.1.0.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

grokipedia_api-0.1.0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file grokipedia_api-0.1.0.tar.gz.

File metadata

  • Download URL: grokipedia_api-0.1.0.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for grokipedia_api-0.1.0.tar.gz
Algorithm Hash digest
SHA256 19ea5f7460c3873d1aae11523a40a8317540acd9ff12d1b3ca039dc79dd18004
MD5 20cb74a6ac29bd8f29cc5b725a9acad5
BLAKE2b-256 8cbbf266398022a05ca4b754569cf53bfd311f0f790b0098db9787f56e61bcc5

See more details on using hashes here.

File details

Details for the file grokipedia_api-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: grokipedia_api-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for grokipedia_api-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0ebbf2b2c7d3f6f311011cfbef0dc1155406a7b6811ed06fe89e523ebc3be075
MD5 975dbebc082cbdaa85f59b454f24786f
BLAKE2b-256 103dc2d70ada6fd5ad10b82b4efc10fe273dc264c4172e0c39395f559298ab3d

See more details on using hashes here.

Supported by

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