first google API experiments
This commit is contained in:
commit
1bba7ef03d
4 changed files with 204 additions and 0 deletions
121
main.go
Normal file
121
main.go
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
toml "github.com/pelletier/go-toml/v2"
|
||||
"google.golang.org/api/option"
|
||||
"google.golang.org/api/youtube/v3"
|
||||
)
|
||||
|
||||
type (
|
||||
Config struct {
|
||||
Google GoogleConfig `toml:"google"`
|
||||
}
|
||||
|
||||
GoogleConfig struct {
|
||||
ApiKey string `toml:"api_key"`
|
||||
ClientID string `toml:"client_id"`
|
||||
ClientSecret string `toml:"client_secret"`
|
||||
}
|
||||
)
|
||||
|
||||
var DEFAULT_TAGS = []string{
|
||||
"ari melody",
|
||||
"ari melody LIVE",
|
||||
"livestream",
|
||||
"vtuber",
|
||||
"twitch",
|
||||
"gaming",
|
||||
"let's play",
|
||||
"full VOD",
|
||||
"VOD",
|
||||
"stream",
|
||||
"archive",
|
||||
}
|
||||
|
||||
const (
|
||||
CATEGORY_GAMING = "20"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Printf("usage: %s <video ID>\n", os.Args[0])
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// videoID := os.Args[1]
|
||||
|
||||
cfgBytes, err := os.ReadFile("config.toml")
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "fatal: failed to read config file: %s\n", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
cfg := Config{}
|
||||
err = toml.Unmarshal(cfgBytes, &cfg)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "fatal: failed to parse config: %s\n", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
service, err := youtube.NewService(
|
||||
ctx,
|
||||
option.WithScopes(youtube.YoutubeUploadScope),
|
||||
option.WithAPIKey(cfg.Google.ApiKey),
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "fatal: failed to create youtube service: %s\n", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
videoService := youtube.NewVideosService(service)
|
||||
|
||||
// get video by ID
|
||||
{
|
||||
// call := service.Videos.List([]string{
|
||||
// "snippet", "contentDetails", "statistics", "status",
|
||||
// }).Id(videoID)
|
||||
// res, err := call.Do()
|
||||
// if err != nil {
|
||||
// fmt.Fprintf(os.Stderr, "fatal: failed to request videos list: %s\n", err.Error())
|
||||
// os.Exit(1)
|
||||
// }
|
||||
|
||||
// data, err := json.MarshalIndent(res, "", " ")
|
||||
// if err != nil {
|
||||
// fmt.Fprintf(os.Stderr, "fatal: failed to marshal json: %s\n", err.Error())
|
||||
// os.Exit(1)
|
||||
// }
|
||||
|
||||
// fmt.Println(string(data))
|
||||
}
|
||||
|
||||
call := videoService.Insert([]string{
|
||||
"snippet", "status",
|
||||
}, &youtube.Video{
|
||||
Snippet: &youtube.VideoSnippet{
|
||||
Title: "Untitled Video",
|
||||
Description: "No description",
|
||||
Tags: DEFAULT_TAGS,
|
||||
CategoryId: CATEGORY_GAMING, // gaming
|
||||
},
|
||||
}).NotifySubscribers(false)
|
||||
// TODO: call.Media()
|
||||
video, err := call.Do()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "fatal: failed to upload video: %s\n", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
data, err := json.MarshalIndent(video, "", " ")
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "fatal: failed to marshal video data json: %s\n", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println(string(data))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue