Skip to content

Commit 636e0ea

Browse files
committed
wip
1 parent 0986c70 commit 636e0ea

File tree

2 files changed

+55
-27
lines changed

2 files changed

+55
-27
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,38 @@ pip install streampot
1111
```
1212
## Usage
1313

14+
Here's a simple example that converts a video file to an audio file:
15+
1416
```python
1517
from streampot import StreamPot
1618

1719
client = StreamPot(secret='yourToken')
20+
21+
job = client.input('https://download.samplelib.com/mp4/sample-5s.mp4') \
22+
.output('audio.mp3') \
23+
.run_and_wait()
24+
25+
print(job.outputs['audio.mp3'])
26+
```
27+
28+
If you want to run the job in the background, you can use the `run` method:
29+
30+
```python
31+
job = client.input('https://download.samplelib.com/mp4/sample-5s.mp4') \
32+
.output('audio.mp3') \
33+
.run()
34+
```
35+
36+
And fetch the job info using the `get_job` method:
37+
38+
```python
39+
job = client.get_job(job.id)
40+
41+
print(job.status)
42+
print(job.outputs['audio.mp3']) # output url by file name
43+
print(job.logs) # error logs if any
44+
print(job.created_at)
45+
print(job.completed_at)
1846
```
1947

2048
## Development

streampot/_client.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
import requests
22
from dataclasses import dataclass
3-
from typing import List, Optional
4-
from enum import Enum, auto
3+
from typing import Optional
4+
from enum import Enum
55
import time
66

77

8-
@dataclass
9-
class Asset:
10-
name: str
11-
url: str
12-
13-
148
class JobStatus(Enum):
15-
PENDING = auto()
16-
COMPLETED = auto()
17-
FAILED = auto()
18-
UPLOADING = auto()
9+
PENDING = "pending"
10+
COMPLETED = "completed"
11+
FAILED = "failed"
12+
UPLOADING = "uploading"
1913

2014

2115
@dataclass
2216
class JobEntity:
2317
id: int
2418
status: JobStatus
2519
created_at: str
26-
assets: Optional[List[Asset]] = None
20+
logs: Optional[str] = None
21+
outputs: Optional[dict] = None
22+
completed_at: Optional[str] = None
23+
24+
25+
def _response_to_job(data: dict) -> JobEntity:
26+
data['status'] = JobStatus[data['status'].upper()]
27+
return JobEntity(**data)
2728

2829

2930
class StreamPotClient:
@@ -32,19 +33,17 @@ def __init__(self, secret: str, base_url: str = 'https://api.streampot.io/v1'):
3233
self.base_url = base_url
3334
self.actions = []
3435

35-
def get_job(self, job_id: int) -> dict:
36+
def get_job(self, job_id: int) -> JobEntity:
3637
response = requests.get(f"{self.base_url}/jobs/{job_id}", headers=self._auth_header())
3738
response.raise_for_status()
38-
return response.json()
39+
40+
return _response_to_job(response.json())
3941

4042
def run(self) -> JobEntity:
41-
response = requests.post(f"{self.base_url}/", headers=self._auth_header(json=True), json=self.actions)
43+
response = requests.post(f"{self.base_url}/", headers=self._auth_header(), json=self.actions)
4244
response.raise_for_status()
4345

44-
job_data = response.json()
45-
job_data['status'] = JobStatus[job_data['status'].upper()]
46-
47-
return JobEntity(**job_data)
46+
return _response_to_job(response.json())
4847

4948
def run_and_wait(self, interval_ms: int = 1000) -> JobEntity:
5049
job = self.run()
@@ -53,15 +52,16 @@ def run_and_wait(self, interval_ms: int = 1000) -> JobEntity:
5352
job = self.get_job(job.id)
5453
return job
5554

56-
def _auth_header(self, json: bool = False) -> dict:
57-
headers = {"Authorization": f"Bearer {self.secret}"}
58-
if json:
59-
headers['Accept'] = 'application/json'
60-
headers['Content-Type'] = 'application/json'
61-
return headers
55+
def _auth_header(self) -> dict:
56+
return {
57+
"Authorization": f"Bearer {self.secret}",
58+
"Accept": 'application/json',
59+
"Content-Type": 'application/json'
60+
}
6261

6362
def _add_action(self, name: str, *values):
64-
self.actions.append({"name": name, "value": values})
63+
self.actions.append({"name": name, "value": list(values)})
64+
return self
6565

6666
def merge_add(self, source: str):
6767
return self._add_action('mergeAdd', source)

0 commit comments

Comments
 (0)