Skip to main content

A Python client for accessing Grokipedia content

Project description

Grokipedia API

PyPI version Python 3.8+ License: MIT

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
  • MCP Server: Model Context Protocol server for AI integrations (Python 3.10+)
  • CLI Support: Command-line interface for quick access

Installation

Basic Installation

pip install grokipedia-api

With MCP Support (Python 3.10+)

For users who want MCP server functionality:

pip install grokipedia-api[mcp]

Or install from source:

git clone https://github.com/AkeBoss-tech/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'])

MCP Server (Python 3.10+)

For AI agent integrations:

# Start the MCP server
grokipedia-mcp

The server exposes tools for searching and retrieving Grokipedia content via the Model Context Protocol. See MCP_SERVER.md for detailed documentation.

Command Line Interface

# Search for articles
grokipedia search "Python programming"

# Get a specific page
grokipedia get "United_Petroleum" --citations

# Get full content
grokipedia get "United_Petroleum" --full

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/AkeBoss-tech/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.1.tar.gz (14.5 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.1-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: grokipedia_api-0.1.1.tar.gz
  • Upload date:
  • Size: 14.5 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.1.tar.gz
Algorithm Hash digest
SHA256 fadc865992614d5a94f1013e9dae9adeab871e66a6cc79044d8c90fd1a1ed5b0
MD5 50d96a38076c0ca66577cbc2e955900b
BLAKE2b-256 0c64ebc90a3c220cff9d6443cfe76e8cf89dd1c61cad522a80bfc749ff7b8604

See more details on using hashes here.

File details

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

File metadata

  • Download URL: grokipedia_api-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.7 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b4148b738d3cf90e5311270e8337ccb27369bb68c5612f8381b0648313cddc89
MD5 2378f64aae63f80f3da3b33831c8f092
BLAKE2b-256 f643d29bf525bd7a9681dbca8cad1776fa8d289e2634fd1f32fba5f2e03af9e6

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