|
1 |
| -# membit-python |
| 1 | +# Membit Python Client |
| 2 | + |
| 3 | +[](https://pypi.org/project/membit-python/) |
| 4 | +[](https://github.com/bandprotocol/membit-python/blob/main/LICENSE) |
| 5 | + |
| 6 | +The Membit Python client provides powerful social media analytics and monitoring capabilities for your Python applications. Easily integrate trending discussion discovery, cluster analysis, and social post search with a simple and intuitive API. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +```bash |
| 11 | +pip install membit-python |
| 12 | +``` |
| 13 | + |
| 14 | +Or with uv: |
| 15 | + |
| 16 | +```bash |
| 17 | +uv add membit-python |
| 18 | +``` |
| 19 | + |
| 20 | +## Quick Start |
| 21 | + |
| 22 | +### Basic Usage |
| 23 | + |
| 24 | +```python |
| 25 | +from membit import MembitClient |
| 26 | + |
| 27 | +# Initialize the client with API key |
| 28 | +client = MembitClient(api_key="your_api_key_here") |
| 29 | + |
| 30 | +# Or use environment variable MEMBIT_API_KEY |
| 31 | +# client = MembitClient() |
| 32 | + |
| 33 | +# Search for trending discussion clusters |
| 34 | +clusters = client.cluster_search("artificial intelligence", limit=5) |
| 35 | +print(clusters) |
| 36 | +``` |
| 37 | + |
| 38 | +## Features |
| 39 | + |
| 40 | +### 🔥 Trending Discussion Clusters |
| 41 | + |
| 42 | +Find and analyze trending discussions across social platforms with intelligent clustering. |
| 43 | + |
| 44 | +### 🔍 Deep Cluster Analysis |
| 45 | + |
| 46 | +Dive deeper into specific discussions to understand context and participants. |
| 47 | + |
| 48 | +### 📱 Social Post Search |
| 49 | + |
| 50 | +Search for individual social media posts on specific topics. |
| 51 | + |
| 52 | +### ⚡ Async Support |
| 53 | + |
| 54 | +Full async/await support for high-performance applications. |
| 55 | + |
| 56 | +### 📊 Multiple Response Formats |
| 57 | + |
| 58 | +Get data in JSON format for applications or LLM-optimized text for AI workflows. |
| 59 | + |
| 60 | +## Usage Examples |
| 61 | + |
| 62 | +### Finding Trending Discussion Clusters |
| 63 | + |
| 64 | +```python |
| 65 | +from membit import MembitClient |
| 66 | + |
| 67 | +# Initialize client with API key |
| 68 | +client = MembitClient(api_key="your_api_key_here") |
| 69 | +# Or use environment variable MEMBIT_API_KEY |
| 70 | +# client = MembitClient() |
| 71 | + |
| 72 | +# Search for trending clusters |
| 73 | +clusters = client.cluster_search("artificial intelligence", limit=5) |
| 74 | + |
| 75 | +# clusters is a dict containing trending discussion clusters |
| 76 | +for cluster in clusters.get("clusters", []): |
| 77 | + print(f"Cluster: {cluster['label']}") |
| 78 | +``` |
| 79 | + |
| 80 | +### Getting Detailed Cluster Information |
| 81 | + |
| 82 | +```python |
| 83 | +from membit import MembitClient |
| 84 | + |
| 85 | +# Initialize client with API key |
| 86 | +client = MembitClient(api_key="your_api_key_here") |
| 87 | + |
| 88 | +# First, find clusters |
| 89 | +clusters = client.cluster_search("climate change") |
| 90 | + |
| 91 | +# Get detailed info about the first cluster |
| 92 | +if clusters.get("clusters"): |
| 93 | + cluster_label = clusters["clusters"][0]["label"] |
| 94 | + cluster_details = client.cluster_info(label=cluster_label, limit=10) |
| 95 | + print(cluster_details) |
| 96 | +``` |
| 97 | + |
| 98 | +### Searching for Individual Posts |
| 99 | + |
| 100 | +```python |
| 101 | +from membit import MembitClient |
| 102 | + |
| 103 | +# Initialize client with API key |
| 104 | +client = MembitClient(api_key="your_api_key_here") |
| 105 | + |
| 106 | +# Search for specific social posts |
| 107 | +posts = client.post_search("machine learning breakthrough", limit=20) |
| 108 | + |
| 109 | +# Access individual social media posts |
| 110 | +for post in posts.get("posts", []): |
| 111 | + print(f"Post: {post}") |
| 112 | +``` |
| 113 | + |
| 114 | +### Using Different Response Formats |
| 115 | + |
| 116 | +```python |
| 117 | +from membit import MembitClient |
| 118 | + |
| 119 | +# Initialize client with API key |
| 120 | +client = MembitClient(api_key="your_api_key_here") |
| 121 | + |
| 122 | +# Get JSON response (default) |
| 123 | +json_response = client.cluster_search("space exploration", output_format="json") |
| 124 | +# Returns: dict with structured data |
| 125 | + |
| 126 | +# Get LLM-optimized text response |
| 127 | +llm_response = client.cluster_search("space exploration", output_format="llm") |
| 128 | +# Returns: str with formatted text optimized for AI processing |
| 129 | +``` |
| 130 | + |
| 131 | +## Async Support |
| 132 | + |
| 133 | +For applications that need high performance or handle multiple concurrent requests: |
| 134 | + |
| 135 | +```python |
| 136 | +import asyncio |
| 137 | +from membit import AsyncMembitClient |
| 138 | + |
| 139 | +async def analyze_topics(): |
| 140 | + client = AsyncMembitClient(api_key="your_api_key_here") |
| 141 | + |
| 142 | + # Search for trending clusters asynchronously |
| 143 | + clusters = await client.cluster_search("tech news", limit=5) |
| 144 | + |
| 145 | + # Get detailed info for multiple clusters concurrently |
| 146 | + if clusters.get("clusters"): |
| 147 | + tasks = [ |
| 148 | + client.cluster_info(label=cluster["label"]) |
| 149 | + for cluster in clusters["clusters"][:3] |
| 150 | + ] |
| 151 | + cluster_details = await asyncio.gather(*tasks) |
| 152 | + |
| 153 | + for details in cluster_details: |
| 154 | + print(details) |
| 155 | + |
| 156 | +# Run the async function |
| 157 | +asyncio.run(analyze_topics()) |
| 158 | +``` |
| 159 | + |
| 160 | +## API Reference |
| 161 | + |
| 162 | +### `MembitClient(api_key=None, api_url=None)` |
| 163 | + |
| 164 | +Initialize the Membit client. |
| 165 | + |
| 166 | +**Parameters:** |
| 167 | + |
| 168 | +- `api_key` (str, optional): Your Membit API key. If not provided, uses `MEMBIT_API_KEY` environment variable. |
| 169 | +- `api_url` (str, optional): Custom API URL. Uses default Membit API URL if not provided. |
| 170 | + |
| 171 | +--- |
| 172 | + |
| 173 | +### `cluster_search(q, limit=10, output_format="json", timeout=60)` |
| 174 | + |
| 175 | +Get trending discussions across social platforms. Useful for finding topics of interest and understanding live conversations. |
| 176 | + |
| 177 | +**Parameters:** |
| 178 | + |
| 179 | +- `q` (str): Search query string |
| 180 | +- `limit` (int, optional): Maximum number of results to return (default: 10) |
| 181 | +- `output_format` (str, optional): Response format - `"json"` or `"llm"` (default: `"json"`) |
| 182 | +- `timeout` (int, optional): Request timeout in seconds (default: 60, max: 120) |
| 183 | + |
| 184 | +**Returns:** |
| 185 | + |
| 186 | +- `dict`: Trending discussion clusters (when `output_format="json"`) |
| 187 | +- `str`: Formatted text response (when `output_format="llm"`) |
| 188 | + |
| 189 | +--- |
| 190 | + |
| 191 | +### `cluster_info(label, limit=10, output_format="json", timeout=60)` |
| 192 | + |
| 193 | +Dive deeper into a specific trending discussion cluster. Useful for understanding the context and participants of a particular conversation. |
| 194 | + |
| 195 | +**Parameters:** |
| 196 | + |
| 197 | +- `label` (str): Cluster label obtained from `cluster_search` |
| 198 | +- `limit` (int, optional): Maximum number of results to return (default: 10) |
| 199 | +- `output_format` (str, optional): Response format - `"json"` or `"llm"` (default: `"json"`) |
| 200 | +- `timeout` (int, optional): Request timeout in seconds (default: 60, max: 120) |
| 201 | + |
| 202 | +**Returns:** |
| 203 | + |
| 204 | +- `dict`: Detailed cluster information (when `output_format="json"`) |
| 205 | +- `str`: Formatted text response (when `output_format="llm"`) |
| 206 | + |
| 207 | +--- |
| 208 | + |
| 209 | +### `post_search(q, limit=10, output_format="json", timeout=60)` |
| 210 | + |
| 211 | +Search for raw social posts. Useful when you need to find specific posts (not recommended for finding trending discussions). |
| 212 | + |
| 213 | +**Parameters:** |
| 214 | + |
| 215 | +- `q` (str): Search query string |
| 216 | +- `limit` (int, optional): Maximum number of results to return (default: 10) |
| 217 | +- `output_format` (str, optional): Response format - `"json"` or `"llm"` (default: `"json"`) |
| 218 | +- `timeout` (int, optional): Request timeout in seconds (default: 60, max: 120) |
| 219 | + |
| 220 | +**Returns:** |
| 221 | + |
| 222 | +- `dict`: Raw social posts (when `output_format="json"`) |
| 223 | +- `str`: Formatted text response (when `output_format="llm"`) |
| 224 | + |
| 225 | +## Error Handling |
| 226 | + |
| 227 | +The client includes comprehensive error handling: |
| 228 | + |
| 229 | +```python |
| 230 | +from membit import MembitClient, MissingAPIKeyError |
| 231 | + |
| 232 | +try: |
| 233 | + # Initialize client with API key |
| 234 | + client = MembitClient(api_key="your_api_key_here") |
| 235 | + |
| 236 | + result = client.cluster_search("python programming") |
| 237 | + |
| 238 | +except MissingAPIKeyError: |
| 239 | + print("Please provide a valid API key") |
| 240 | + |
| 241 | +except TimeoutError: |
| 242 | + print("Request timed out") |
| 243 | + |
| 244 | +except Exception as e: |
| 245 | + print(f"An error occurred: {e}") |
| 246 | +``` |
| 247 | + |
| 248 | +## Requirements |
| 249 | + |
| 250 | +- **Python:** >=3.10 |
| 251 | +- **Dependencies:** |
| 252 | + - `requests>=2.25.0` (for synchronous client) |
| 253 | + - `httpx>=0.28.1` (for asynchronous client) |
| 254 | + |
| 255 | +## Examples |
| 256 | + |
| 257 | +Complete working examples are available in the `examples/` directory: |
| 258 | + |
| 259 | +- [`examples/client.py`](examples/client.py) - Synchronous client usage |
| 260 | +- [`examples/async_client.py`](examples/async_client.py) - Asynchronous client usage |
| 261 | + |
| 262 | +### Running Examples |
| 263 | + |
| 264 | +```bash |
| 265 | +# Set your API key |
| 266 | +export MEMBIT_API_KEY="your_api_key_here" |
| 267 | + |
| 268 | +# Install dependencies |
| 269 | +uv sync |
| 270 | + |
| 271 | +# Run synchronous example |
| 272 | +uv run examples/client.py |
| 273 | + |
| 274 | +# Run asynchronous example |
| 275 | +uv run examples/async_client.py |
| 276 | +``` |
| 277 | + |
| 278 | +## Development |
| 279 | + |
| 280 | +### Installing for Development |
| 281 | + |
| 282 | +```bash |
| 283 | +git clone <repository-url> |
| 284 | +cd membit-python |
| 285 | +uv sync |
| 286 | +``` |
| 287 | + |
| 288 | +### Running Tests |
| 289 | + |
| 290 | +```bash |
| 291 | +uv run pytest |
| 292 | +``` |
| 293 | + |
| 294 | +## Contributing |
| 295 | + |
| 296 | +We welcome contributions! Please feel free to submit a Pull Request. |
| 297 | + |
| 298 | +## License |
| 299 | + |
| 300 | +This project is licensed under the MIT License. |
| 301 | + |
| 302 | +## Support |
| 303 | + |
| 304 | +For support or questions about the Membit Python client, please reach out to our support team. |
0 commit comments