CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-mastodon-py

Python wrapper for the Mastodon API providing comprehensive access to social media functionality

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

search.mddocs/

Search & Discovery

Comprehensive search functionality across accounts, statuses, and hashtags with support for both v1 and v2 search APIs. Enables applications to find and discover content across the Mastodon network.

Capabilities

General Search

Search across multiple content types with intelligent routing between API versions based on instance capabilities.

def search(
    self,
    q: str,
    resolve: bool = True,
    result_type: str = None,
    account_id: Union[Account, int] = None,
    offset: int = None,
    min_id: int = None,
    max_id: int = None,
    exclude_unreviewed: bool = True
) -> Union[Search, SearchV2]:
    """
    Search for hashtags, accounts, and statuses across the network.
    
    Args:
        q: Search query string
        resolve: Perform webfinger lookups for remote accounts/statuses
        result_type: Limit results to specific type ("accounts", "hashtags", "statuses")
        account_id: Only return results from this specific account
        offset: Pagination offset for results
        min_id: Return results newer than this ID
        max_id: Return results older than this ID
        exclude_unreviewed: Exclude unreviewed hashtags from results (Mastodon 3.0.0+)
    
    Returns:
        Dictionary containing search results with "accounts", "statuses", and "hashtags" keys
    """

Advanced Search (v2)

More sophisticated search with enhanced filtering and pagination options.

def search_v2(
    self,
    q: str,
    resolve: bool = True,
    result_type: str = None,
    account_id: Union[Account, int] = None,
    offset: int = None,
    min_id: int = None,
    max_id: int = None,
    exclude_unreviewed: bool = True
) -> SearchV2:
    """
    Advanced search API with enhanced features and object-based hashtag results.
    
    Args:
        q: Search query string
        resolve: Perform webfinger lookups (default: True for v2)
        result_type: Filter by content type ("accounts", "hashtags", "statuses") 
        account_id: Restrict search to specific account's content
        offset: Pagination offset (Mastodon 2.8.0+)
        min_id: Pagination - results newer than this ID (Mastodon 2.8.0+)
        max_id: Pagination - results older than this ID (Mastodon 2.8.0+)
        exclude_unreviewed: Exclude unreviewed hashtags (Mastodon 3.0.0+)
    
    Returns:
        Search results with hashtags as full objects (including usage stats)
    """

Legacy Search (v1)

Basic search functionality for older Mastodon instances.

def search_v1(self, q: str, resolve: bool = False) -> Search:
    """
    Legacy search API for Mastodon instances before 2.4.1.
    
    Args:
        q: Search query string
        resolve: Perform webfinger lookups (default: False for v1)
    
    Returns:
        Basic search results with hashtags as strings rather than objects
        
    Note:
        This method should not be used directly - use search() instead
        which automatically selects the appropriate API version.
    """

Usage Examples

Basic Search

from mastodon import Mastodon

mastodon = Mastodon(access_token='your_token.secret')

# Search for everything related to "python"
results = mastodon.search("python")
print(f"Found {len(results['accounts'])} accounts")
print(f"Found {len(results['statuses'])} statuses") 
print(f"Found {len(results['hashtags'])} hashtags")

# Search only for accounts
accounts = mastodon.search("python", result_type="accounts")
for account in accounts['accounts']:
    print(f"@{account['acct']}: {account['display_name']}")

Advanced Search with Pagination

# Search for recent posts about Python with pagination
results = mastodon.search(
    "python programming", 
    result_type="statuses",
    limit=20,
    resolve=True
)

# Get older results using max_id
if results['statuses']:
    older_results = mastodon.search(
        "python programming",
        result_type="statuses", 
        max_id=results['statuses'][-1]['id']
    )

Search Specific Account

# Find posts from a specific account
account = mastodon.account_lookup("gargron@mastodon.social")
results = mastodon.search(
    "federation",
    account_id=account['id'],
    result_type="statuses"
)

Return Types

Search results are returned as dictionaries with the following structure:

{
    "accounts": [
        {
            "id": "123",
            "username": "user",
            "acct": "user@example.com", 
            "display_name": "User Name",
            # ... other account fields
        }
    ],
    "statuses": [
        {
            "id": "456", 
            "content": "<p>Status content</p>",
            "account": { /* account object */ },
            # ... other status fields
        }
    ],
    "hashtags": [
        # v1: ["hashtag1", "hashtag2"]
        # v2: [{"name": "hashtag1", "history": [...], ...}]
    ]
}

Version Compatibility

The search API automatically adapts to your Mastodon instance version:

  • Mastodon < 2.4.1: Uses search_v1() with basic functionality
  • Mastodon 2.4.1+: Uses search_v2() with object-based hashtags
  • Mastodon 2.8.0+: Advanced parameters (account_id, offset, pagination) available
  • Mastodon 3.0.0+: exclude_unreviewed parameter for hashtag filtering

Parameters not supported by your instance version will raise a MastodonVersionError.

Install with Tessl CLI

npx tessl i tessl/pypi-mastodon-py@2.1.1

docs

accounts.md

authentication.md

index.md

search.md

statuses.md

streaming.md

tile.json