@pbvision/partner-sdk 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +9 -0
- package/README.md +134 -0
- package/package.json +22 -0
- package/src/index.js +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright 2024 Pickleball Vision AI Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# PB Vision Partner Documentation <!-- omit in toc -->
|
|
2
|
+
|
|
3
|
+
The easiest way to use the PB Vision AI Engine is to simply share a link to
|
|
4
|
+
your video file. Our AI usually works on each video for about an hour for a
|
|
5
|
+
standard length game, so we'll notify your servers when the results are ready.
|
|
6
|
+
|
|
7
|
+
- [Using the API](#using-the-api)
|
|
8
|
+
- [Step 1: Get API Access](#step-1-get-api-access)
|
|
9
|
+
- [Step 2: Webhook Setup](#step-2-webhook-setup)
|
|
10
|
+
- [Step 3: Send Videos](#step-3-send-videos)
|
|
11
|
+
- [Step 4: Video Processing Done](#step-4-video-processing-done)
|
|
12
|
+
- [SDK Setup](#sdk-setup)
|
|
13
|
+
- [Video Guidelines](#video-guidelines)
|
|
14
|
+
|
|
15
|
+
## Using the API
|
|
16
|
+
|
|
17
|
+
### Step 1: Get API Access
|
|
18
|
+
|
|
19
|
+
Email **[dev@pb.vision](mailto:dev@pb.vision)** to request API access for your
|
|
20
|
+
account. We'll send you an API Key.
|
|
21
|
+
|
|
22
|
+
### Step 2: Webhook Setup
|
|
23
|
+
|
|
24
|
+
Each time our AI finishes processing a video, it will notify your servers via
|
|
25
|
+
an HTTP POST request to a URL ("webhook") of your choice. You can use our SDK
|
|
26
|
+
to tell our servers where you want us to notify your servers like this:
|
|
27
|
+
|
|
28
|
+
```javascript
|
|
29
|
+
import { PBVision } from '@pbvision/partner-sdk'
|
|
30
|
+
|
|
31
|
+
const pbv = new PBVision(YOUR_API_KEY)
|
|
32
|
+
await pbv.setWebhook(YOUR_WEBHOOK_URL)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Alternatively, you can just use `curl`:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
curl -X POST \
|
|
39
|
+
-H 'x-api-key: YOUR_API_KEY' \
|
|
40
|
+
-H 'Content-Type: application/json' \
|
|
41
|
+
-d '{"url": "https://YOUR_WEBHOOK_URL"}' \
|
|
42
|
+
https://api-ko3kowqi6a-uc.a.run.app/partner/webhook/set
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Step 3: Send Videos
|
|
46
|
+
|
|
47
|
+
First, upload a video to a _publicly_ accessible URL on your server. For best
|
|
48
|
+
results, videos should follow our [guidelines](#video-guidelines).
|
|
49
|
+
|
|
50
|
+
Next, tell us to download and work on the video. You can do this using our SDK:
|
|
51
|
+
|
|
52
|
+
```javascript
|
|
53
|
+
import { PBVision } from '@pbvision/partner-sdk'
|
|
54
|
+
|
|
55
|
+
const pbv = new PBVision(YOUR_API_KEY)
|
|
56
|
+
const playerEmails = [] // optional!
|
|
57
|
+
await pbv.sendVideoUrlToDownload(YOUR_VIDEO_URL, playerEmails)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Alternatively, you can just use `curl`:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
curl -X POST \
|
|
64
|
+
-H 'x-api-key: YOUR_API_KEY' \
|
|
65
|
+
-H 'Content-Type: application/json' \
|
|
66
|
+
-d '{"url": "https://YOUR_VIDEO_URL", "userEmails": ["test@example.com"]}' \
|
|
67
|
+
https://api-ko3kowqi6a-uc.a.run.app/partner/add_video_by_url
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Step 4: Video Processing Done
|
|
71
|
+
|
|
72
|
+
When our AI is done processing your video, we will email the players in the
|
|
73
|
+
game (_if_ their email addresses were provided).
|
|
74
|
+
|
|
75
|
+
If you provided a webhook in [step 2](#step-2-webhook-setup), then we'll send
|
|
76
|
+
an HTTP POST request to your server. Your server should acknowledge this
|
|
77
|
+
callback with a standard 200 (OK) response. If it does not, we will attempt to
|
|
78
|
+
retry sending this request later (up to some maximum number of attempts).
|
|
79
|
+
|
|
80
|
+
The HTTP POST body will contain JSON which looks like this:
|
|
81
|
+
|
|
82
|
+
```json
|
|
83
|
+
{
|
|
84
|
+
"from_url": "https://example.com/my-video.mp4",
|
|
85
|
+
"webpage": "https://app.pb.vision/video/xyz",
|
|
86
|
+
"events": EVENTS_DATA,
|
|
87
|
+
"insights": INSIGHTS_DATA,
|
|
88
|
+
"error": {
|
|
89
|
+
"reason": "some explanation here..."
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
- `from_url` is the video url you sent us in [step 3](#step-3-send-videos)
|
|
95
|
+
- `webpage` is a link to our web app where the stats can be explored
|
|
96
|
+
- `error` is only present if your video could not be processed
|
|
97
|
+
- `events` - describes the pickleball game in detailed, low-level events
|
|
98
|
+
- Explore the _events_ schema at <https://pbv-public.github.io/events/dev>
|
|
99
|
+
- Schema changes and diffs are in our [`pbv-public/events` repo](https://github.com/pbv-public/events/blob/dev/CHANGELOG.md)
|
|
100
|
+
- `insights` - a summary of high-level stats about the video
|
|
101
|
+
- Explore the _insights_ schema at <https://pbv-public.github.io/insights/dev>
|
|
102
|
+
- Schema changes and diffs are in our [`pbv-public/insights` repo](https://github.com/pbv-public/insights/blob/dev/CHANGELOG.md)
|
|
103
|
+
|
|
104
|
+
## SDK Setup
|
|
105
|
+
|
|
106
|
+
1. Install [node](https://nodejs.org/en/download)
|
|
107
|
+
2. Install the SDK: `npm i @pbvision/partner-sdk`
|
|
108
|
+
3. In your package.json, make sure you're configured to use ESM (modules): `"type": "module"`
|
|
109
|
+
4. Initialize the SDK:
|
|
110
|
+
|
|
111
|
+
```javascript
|
|
112
|
+
import { PBVision } from '@pbvision/partner-sdk'
|
|
113
|
+
const pbv = new PBVision(YOUR_API_KEY)
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
5. Use its methods as described to [send us video URLs](#step-3-send-videos) to
|
|
117
|
+
to process, or [tell us your webhook's URL](#step-2-webhook-setup).
|
|
118
|
+
|
|
119
|
+
## Video Guidelines
|
|
120
|
+
|
|
121
|
+
Requirements:
|
|
122
|
+
|
|
123
|
+
- Video Encoding: **H.264** codec
|
|
124
|
+
- Frame Rate: **30 or 60 FPS**
|
|
125
|
+
- File Extension: `.mp4`
|
|
126
|
+
|
|
127
|
+
For best results, we also recommend:
|
|
128
|
+
|
|
129
|
+
- Follow our [**video framing** guidelines](https://www.pb.vision/video-tips)
|
|
130
|
+
- Audio Encoding: MPEG-4 AAC
|
|
131
|
+
- Resolution: 1080p
|
|
132
|
+
- Max bitrate: 2 Mbps
|
|
133
|
+
- Max file size: 1GB
|
|
134
|
+
- Max duration: 30 minutes
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pbvision/partner-sdk",
|
|
3
|
+
"description": "SDK for interacting with the PB Vision pickleball AI engine.",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=20"
|
|
9
|
+
},
|
|
10
|
+
"exports": "./src/index.js",
|
|
11
|
+
"files": [
|
|
12
|
+
"src"
|
|
13
|
+
],
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/pbv-public/partner-sdk-nodejs"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"node-fetch": "^3.3.2"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {}
|
|
22
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import assert from 'node:assert'
|
|
2
|
+
|
|
3
|
+
import fetch from 'node-fetch'
|
|
4
|
+
|
|
5
|
+
export class PBVision {
|
|
6
|
+
constructor (apiKey, server = 'https://api-ko3kowqi6a-uc.a.run.app') {
|
|
7
|
+
assert(apiKey && apiKey.length === 31 && apiKey.indexOf('_') === 10,
|
|
8
|
+
`invalid API key: ${apiKey}`)
|
|
9
|
+
|
|
10
|
+
this.apiKey = apiKey
|
|
11
|
+
this.server = server
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Tells PB Vision to make an HTTP POST request your URL after each of your
|
|
16
|
+
* videos is done processing.
|
|
17
|
+
*
|
|
18
|
+
* @param {string} webhookUrl must start with https://
|
|
19
|
+
*/
|
|
20
|
+
async setWebhook (webhookUrl) {
|
|
21
|
+
assert(typeof webhookUrl === 'string' && webhookUrl.startsWith('https://'),
|
|
22
|
+
'URL must be a string beginning with https://')
|
|
23
|
+
return this.__callAPI('webhook/set', { url: webhookUrl })
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Tells PB Vision to download the specified video and process it. When
|
|
28
|
+
* processing is complete, your webhook URL will receive a callback.
|
|
29
|
+
*
|
|
30
|
+
* @param {string} videoUrl the publicly available URL of the video
|
|
31
|
+
* @param {Array<string>} [playerEmails] the email addresses of players in
|
|
32
|
+
* this video; they'll be notified when the video is done processing
|
|
33
|
+
* @returns
|
|
34
|
+
*/
|
|
35
|
+
async sendVideoUrlToDownload (videoUrl, playerEmails = []) {
|
|
36
|
+
assert(typeof videoUrl === 'string' && videoUrl.startsWith('http'),
|
|
37
|
+
'URL must be a string beginning with http')
|
|
38
|
+
assert(videoUrl.endsWith('.mp4'), 'video URL must have the .mp4 extension')
|
|
39
|
+
return this.__callAPI(
|
|
40
|
+
'add_video_by_url', { url: videoUrl, userEmails: playerEmails })
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async __callAPI (path, body) {
|
|
44
|
+
const resp = await fetch(`${this.server}/partner/${path}`, {
|
|
45
|
+
method: 'POST',
|
|
46
|
+
headers: {
|
|
47
|
+
'x-api-key': this.apiKey,
|
|
48
|
+
'content-type': 'application/json'
|
|
49
|
+
},
|
|
50
|
+
compress: true,
|
|
51
|
+
body: JSON.stringify(body)
|
|
52
|
+
})
|
|
53
|
+
if (resp.ok) {
|
|
54
|
+
return true
|
|
55
|
+
}
|
|
56
|
+
const errBody = await resp.text()
|
|
57
|
+
throw new Error(`PB Vision API ${path} failed (${resp.status}): ${errBody}`)
|
|
58
|
+
}
|
|
59
|
+
}
|