Skip to content

Commit c1fa0c0

Browse files
satawatnacksatawatnack
andauthored
Init membit-python (#1)
* init membit python * update type * clean up * Create LICENSE * add tag * change version * remove q= * change output_format * update api --------- Co-authored-by: satawatnack <[email protected]>
1 parent 50f50a4 commit c1fa0c0

File tree

13 files changed

+1135
-1
lines changed

13 files changed

+1135
-1
lines changed

.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Python
2+
__pycache__/
3+
*.py[cod]
4+
*.so
5+
6+
# Build/Distribution
7+
build/
8+
dist/
9+
*.egg-info/
10+
11+
# Virtual environments
12+
.venv/
13+
venv/
14+
15+
# Environment files
16+
.env
17+
18+
# IDE
19+
.vscode/
20+
.idea/
21+
22+
# OS
23+
.DS_Store
24+
Thumbs.db

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.12

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Band Protocol
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 304 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,304 @@
1-
# membit-python
1+
# Membit Python Client
2+
3+
[![PyPI - Downloads](https://img.shields.io/pypi/dm/membit-python)](https://pypi.org/project/membit-python/)
4+
[![License](https://img.shields.io/github/license/bandprotocol/membit-python)](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

Comments
 (0)