-
Notifications
You must be signed in to change notification settings - Fork 246
/
media.go
89 lines (70 loc) · 2.48 KB
/
media.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package anaconda
import (
"net/url"
"strconv"
)
type Media struct {
MediaID int64 `json:"media_id"`
MediaIDString string `json:"media_id_string"`
Size int `json:"size"`
Image Image `json:"image"`
}
type Image struct {
W int `json:"w"`
H int `json:"h"`
ImageType string `json:"image_type"`
}
type ChunkedMedia struct {
MediaID int64 `json:"media_id"`
MediaIDString string `json:"media_id_string"`
ExpiresAfterSecs int `json:"expires_after_secs"`
}
type Video struct {
VideoType string `json:"video_type"`
}
type VideoMedia struct {
MediaID int64 `json:"media_id"`
MediaIDString string `json:"media_id_string"`
Size int `json:"size"`
ExpiresAfterSecs int `json:"expires_after_secs"`
Video Video `json:"video"`
}
func (a TwitterApi) UploadMedia(base64String string) (media Media, err error) {
v := url.Values{}
v.Set("media_data", base64String)
var mediaResponse Media
response_ch := make(chan response)
a.queryQueue <- query{UploadBaseUrl + "/media/upload.json", v, &mediaResponse, _POST, response_ch}
return mediaResponse, (<-response_ch).err
}
func (a TwitterApi) UploadVideoInit(totalBytes int, mimeType string) (chunkedMedia ChunkedMedia, err error) {
v := url.Values{}
v.Set("command", "INIT")
v.Set("media_type", mimeType)
v.Set("total_bytes", strconv.FormatInt(int64(totalBytes), 10))
var mediaResponse ChunkedMedia
response_ch := make(chan response)
a.queryQueue <- query{UploadBaseUrl + "/media/upload.json", v, &mediaResponse, _POST, response_ch}
return mediaResponse, (<-response_ch).err
}
func (a TwitterApi) UploadVideoAppend(mediaIdString string,
segmentIndex int, base64String string) error {
v := url.Values{}
v.Set("command", "APPEND")
v.Set("media_id", mediaIdString)
v.Set("media_data", base64String)
v.Set("segment_index", strconv.FormatInt(int64(segmentIndex), 10))
var emptyResponse interface{}
response_ch := make(chan response)
a.queryQueue <- query{UploadBaseUrl + "/media/upload.json", v, &emptyResponse, _POST, response_ch}
return (<-response_ch).err
}
func (a TwitterApi) UploadVideoFinalize(mediaIdString string) (videoMedia VideoMedia, err error) {
v := url.Values{}
v.Set("command", "FINALIZE")
v.Set("media_id", mediaIdString)
var mediaResponse VideoMedia
response_ch := make(chan response)
a.queryQueue <- query{UploadBaseUrl + "/media/upload.json", v, &mediaResponse, _POST, response_ch}
return mediaResponse, (<-response_ch).err
}