Skip to content

Commit 72d8d4a

Browse files
committed
feat: Add image and video generation APIs with example usage and documentation
1 parent abc8e07 commit 72d8d4a

File tree

7 files changed

+884
-2
lines changed

7 files changed

+884
-2
lines changed

.env.example

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@ META_AI_WD=your_wd_cookie_value
1111
#META_AI_JS_DATR=your_js_datr_value
1212
#META_AI_ABRA_CSRF=your_abra_csrf_value
1313

14+
# Additional cookies for image/video generation
15+
META_AI_C_USER=your_c_user_value
16+
META_AI_XS=your_xs_value
17+
META_AI_FR=your_fr_value
18+
1419
# Refresh interval in seconds (default: 3600 = 1 hour)
1520
META_AI_REFRESH_INTERVAL_SECONDS=3600
1621

1722
# How to get cookies:
1823
# 1. Open browser and go to https://meta.ai
1924
# 2. Open Developer Tools (F12)
2025
# 3. Go to Application/Storage > Cookies > https://meta.ai
21-
# 4. Copy the values for: datr, abra_sess, dpr, wd
26+
# 4. Copy the values for: datr, abra_sess, dpr, wd, c_user, xs, fr
2227
# 5. Paste them above (without quotes)

GENERATION_API.md

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
# Image and Video Generation API
2+
3+
This implementation is based on captured network requests from Meta AI's web interface for image and video generation.
4+
5+
## Features
6+
7+
- **Image Generation**: Generate images from text prompts with custom orientation
8+
- **Video Generation**: Generate videos from text prompts
9+
- **Cookie-based Authentication**: Uses browser cookies from Meta AI
10+
- **Multipart Response Parsing**: Handles Meta AI's multipart/mixed responses
11+
12+
## Installation
13+
14+
Make sure you have the package installed:
15+
16+
```bash
17+
pip install -e .
18+
```
19+
20+
## Configuration
21+
22+
1. Copy `.env.example` to `.env`:
23+
24+
```bash
25+
cp .env.example .env
26+
```
27+
28+
2. Get your Meta AI cookies:
29+
- Open browser and go to https://meta.ai
30+
- Open Developer Tools (F12)
31+
- Go to Application/Storage > Cookies > https://meta.ai
32+
- Copy the values for: `datr`, `abra_sess`, `dpr`, `wd`, `c_user`, `xs`, `fr`
33+
34+
3. Add cookies to `.env` file:
35+
```env
36+
META_AI_DATR=your_datr_value
37+
META_AI_ABRA_SESS=your_abra_sess_value
38+
META_AI_DPR=your_dpr_value
39+
META_AI_WD=your_wd_value
40+
META_AI_C_USER=your_c_user_value
41+
META_AI_XS=your_xs_value
42+
META_AI_FR=your_fr_value
43+
```
44+
45+
## Usage
46+
47+
### Image Generation
48+
49+
```python
50+
from metaai_api import MetaAI
51+
52+
# Initialize with cookies
53+
cookies = {
54+
'datr': 'your_datr_value',
55+
'abra_sess': 'your_abra_sess_value',
56+
'dpr': '1',
57+
'wd': '1920x1080'
58+
}
59+
60+
ai = MetaAI(cookies=cookies)
61+
62+
# Generate image
63+
result = ai.generate_image_new(
64+
prompt="Astronaut in space",
65+
orientation="VERTICAL", # or "HORIZONTAL", "SQUARE"
66+
num_images=1
67+
)
68+
69+
if result['success']:
70+
for url in result['image_urls']:
71+
print(f"Image URL: {url}")
72+
```
73+
74+
### Video Generation
75+
76+
```python
77+
from metaai_api import MetaAI
78+
79+
# Initialize with cookies
80+
cookies = {
81+
'datr': 'your_datr_value',
82+
'abra_sess': 'your_abra_sess_value',
83+
'dpr': '1',
84+
'wd': '1920x1080'
85+
}
86+
87+
ai = MetaAI(cookies=cookies)
88+
89+
# Generate video
90+
result = ai.generate_video_new(
91+
prompt="Astronaut floating in space"
92+
)
93+
94+
if result['success']:
95+
for url in result['video_urls']:
96+
print(f"Video URL: {url}")
97+
```
98+
99+
## Testing
100+
101+
Run the test script:
102+
103+
```bash
104+
python test_generation.py
105+
```
106+
107+
This will test both image and video generation using the cookies from your `.env` file.
108+
109+
## API Details
110+
111+
### Endpoint
112+
113+
- **URL**: `https://www.meta.ai/api/graphql`
114+
- **Method**: POST
115+
- **Doc ID**: `904075722675ba2c1a7b333d4c525a1b`
116+
117+
### Image Generation
118+
119+
**Operation**: `TEXT_TO_IMAGE`
120+
121+
**Parameters**:
122+
123+
- `prompt`: Text description of the image
124+
- `orientation`: Image orientation (`VERTICAL`, `HORIZONTAL`, `SQUARE`)
125+
- `numImages`: Number of images to generate
126+
127+
### Video Generation
128+
129+
**Operation**: `TEXT_TO_VIDEO`
130+
131+
**Parameters**:
132+
133+
- `prompt`: Text description of the video
134+
135+
### Request Structure
136+
137+
Both operations use similar request structure:
138+
139+
```json
140+
{
141+
"doc_id": "904075722675ba2c1a7b333d4c525a1b",
142+
"variables": {
143+
"conversationId": "uuid",
144+
"content": "Imagine/Animate <prompt>",
145+
"imagineOperationRequest": {
146+
"operation": "TEXT_TO_IMAGE" | "TEXT_TO_VIDEO",
147+
"textToImageParams": {
148+
"prompt": "<prompt>",
149+
"orientation": "VERTICAL" (for images)
150+
}
151+
},
152+
...
153+
}
154+
}
155+
```
156+
157+
## Response Format
158+
159+
The API returns responses in multipart/mixed format or JSON. The implementation automatically:
160+
161+
- Parses multipart responses
162+
- Extracts media URLs (images/videos)
163+
- Returns structured data
164+
165+
## Notes
166+
167+
- Cookies may expire and need to be refreshed periodically
168+
- The API requires valid Meta AI cookies for authentication
169+
- Generated content follows Meta AI's usage policies
170+
- Network captures show this is the same API used by meta.ai web interface
171+
172+
## Troubleshooting
173+
174+
1. **Authentication Errors**: Make sure your cookies are fresh and valid
175+
2. **Missing URLs**: Check that the response contains the expected data structure
176+
3. **Rate Limiting**: Meta AI may rate limit requests, add delays if needed
177+
178+
## Network Capture Details
179+
180+
This implementation is based on analyzing captured network requests:
181+
182+
- **network.json**: Contains video generation requests
183+
- **network-new.json**: Contains image and video generation requests
184+
185+
Key patterns identified:
186+
187+
- Same doc_id for both image and video generation
188+
- Different operation types (`TEXT_TO_IMAGE` vs `TEXT_TO_VIDEO`)
189+
- Consistent request structure with UUIDs and session tracking
190+
- Multipart/mixed response format for streaming results

example_generation.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""
2+
Quick example demonstrating the new image and video generation features.
3+
"""
4+
5+
from metaai_api import MetaAI
6+
7+
# Example cookies (replace with your actual cookies from meta.ai)
8+
cookies = {
9+
'datr': 'your_datr_cookie',
10+
'abra_sess': 'your_abra_sess_cookie',
11+
'dpr': '1',
12+
'wd': '1920x1080'
13+
# Optional but recommended:
14+
# 'c_user': 'your_c_user',
15+
# 'xs': 'your_xs',
16+
# 'fr': 'your_fr'
17+
}
18+
19+
# Initialize Meta AI client
20+
ai = MetaAI(cookies=cookies)
21+
22+
# Example 1: Generate an image
23+
print("\n=== Image Generation ===")
24+
image_result = ai.generate_image_new(
25+
prompt="A futuristic cityscape at sunset",
26+
orientation="VERTICAL", # Options: VERTICAL, HORIZONTAL, SQUARE
27+
num_images=1
28+
)
29+
30+
if image_result['success']:
31+
print(f"✅ Successfully generated {len(image_result['image_urls'])} image(s)")
32+
for i, url in enumerate(image_result['image_urls'], 1):
33+
print(f" Image {i}: {url}")
34+
else:
35+
print(f"❌ Image generation failed: {image_result.get('error')}")
36+
37+
# Example 2: Generate a video
38+
print("\n=== Video Generation ===")
39+
video_result = ai.generate_video_new(
40+
prompt="A robot dancing in a neon-lit city"
41+
)
42+
43+
if video_result['success']:
44+
print(f"✅ Successfully generated {len(video_result['video_urls'])} video(s)")
45+
for i, url in enumerate(video_result['video_urls'], 1):
46+
print(f" Video {i}: {url}")
47+
else:
48+
print(f"❌ Video generation failed: {video_result.get('error')}")
49+
50+
# Example 3: Generate with different orientations
51+
print("\n=== Different Orientations ===")
52+
orientations = ["VERTICAL", "HORIZONTAL", "SQUARE"]
53+
for orientation in orientations:
54+
result = ai.generate_image_new(
55+
prompt="Abstract art",
56+
orientation=orientation
57+
)
58+
if result['success']:
59+
print(f"✅ {orientation}: {len(result['image_urls'])} image(s)")
60+
else:
61+
print(f"❌ {orientation}: Failed")

src/metaai_api/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from .client import send_animate_request
1818
from .video_generation import VideoGenerator # noqa
1919
from .image_upload import ImageUploader # noqa
20+
from .generation import GenerationAPI # noqa
2021

21-
__all__ = ["MetaAI", "send_animate_request", "VideoGenerator", "ImageUploader"]
22+
__all__ = ["MetaAI", "send_animate_request", "VideoGenerator", "ImageUploader", "GenerationAPI"]
2223

0 commit comments

Comments
 (0)