tidying up a *lot*; add QoL options
This commit is contained in:
parent
84de96df31
commit
2954689784
6 changed files with 261 additions and 154 deletions
247
main.go
247
main.go
|
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
|
|
@ -9,41 +10,26 @@ import (
|
|||
"path"
|
||||
"strings"
|
||||
|
||||
toml "github.com/pelletier/go-toml/v2"
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/oauth2/google"
|
||||
"google.golang.org/api/youtube/v3"
|
||||
|
||||
"arimelody.space/live-vod-uploader/config"
|
||||
"arimelody.space/live-vod-uploader/scanner"
|
||||
vid "arimelody.space/live-vod-uploader/video"
|
||||
yt "arimelody.space/live-vod-uploader/youtube"
|
||||
)
|
||||
|
||||
type (
|
||||
Config struct {
|
||||
Google GoogleConfig `toml:"google"`
|
||||
}
|
||||
const segmentExtension = "mkv"
|
||||
|
||||
GoogleConfig struct {
|
||||
ApiKey string `toml:"api_key"`
|
||||
ClientID string `toml:"client_id"`
|
||||
ClientSecret string `toml:"client_secret"`
|
||||
}
|
||||
)
|
||||
|
||||
const CONFIG_FILENAME = "config.toml"
|
||||
//go:embed res/help.txt
|
||||
var helpText string
|
||||
|
||||
func showHelp() {
|
||||
execSplits := strings.Split(os.Args[0], "/")
|
||||
execName := execSplits[len(execSplits) - 1]
|
||||
fmt.Printf(
|
||||
"usage: %s [options] [directory]\n\n" +
|
||||
"options:\n" +
|
||||
"\t-h, --help: Show this help message.\n" +
|
||||
"\t-v, --verbose: Show verbose logging output.\n" +
|
||||
"\t--init: Initialise `directory` as a VOD directory.\n",
|
||||
execName)
|
||||
}
|
||||
fmt.Printf(helpText, execName)
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 || os.Args[1] == "--help" || os.Args[1] == "-h" {
|
||||
|
|
@ -51,9 +37,11 @@ func main() {
|
|||
os.Exit(0)
|
||||
}
|
||||
|
||||
var directory string
|
||||
var initDirectory bool = false
|
||||
var verbose bool = false
|
||||
var initDirectory bool = false
|
||||
var deleteFullVod bool = false
|
||||
var forceUpload bool = false
|
||||
var directory string
|
||||
|
||||
for i, arg := range os.Args {
|
||||
if i == 0 { continue }
|
||||
|
|
@ -66,14 +54,24 @@ func main() {
|
|||
showHelp()
|
||||
os.Exit(0)
|
||||
|
||||
case "--init":
|
||||
initDirectory = true
|
||||
|
||||
case "-v":
|
||||
fallthrough
|
||||
case "--verbose":
|
||||
verbose = true
|
||||
|
||||
case "--init":
|
||||
initDirectory = true
|
||||
|
||||
case "-d":
|
||||
fallthrough
|
||||
case "-deleteAfter":
|
||||
deleteFullVod = true
|
||||
|
||||
case "-f":
|
||||
fallthrough
|
||||
case "--force":
|
||||
forceUpload = true
|
||||
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "Unknown option `%s`\n", arg)
|
||||
os.Exit(1)
|
||||
|
|
@ -84,89 +82,68 @@ func main() {
|
|||
}
|
||||
}
|
||||
|
||||
cfg := Config{}
|
||||
cfgBytes, err := os.ReadFile(CONFIG_FILENAME)
|
||||
// config
|
||||
cfg, err := config.ReadConfig(config.CONFIG_FILENAME)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to read config file: %v", err)
|
||||
|
||||
tomlBytes, err := toml.Marshal(&cfg)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to marshal json: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
err = os.WriteFile(CONFIG_FILENAME, tomlBytes, 0o644)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to write config file: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
log.Printf("New config file created. Please edit this before running again!")
|
||||
os.Exit(0)
|
||||
}
|
||||
err = toml.Unmarshal(cfgBytes, &cfg)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to parse config: %v", err)
|
||||
log.Fatalf("Failed to read config: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if initDirectory {
|
||||
dirInfo, err := os.Stat(directory)
|
||||
if err != nil {
|
||||
if err == os.ErrNotExist {
|
||||
log.Fatalf("No such directory: %s", directory)
|
||||
os.Exit(1)
|
||||
}
|
||||
log.Fatalf("Failed to open directory: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if !dirInfo.IsDir() {
|
||||
log.Fatalf("Not a directory: %s", directory)
|
||||
os.Exit(1)
|
||||
}
|
||||
dirEntry, err := os.ReadDir(directory)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open directory: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
for _, entry := range dirEntry {
|
||||
if !entry.IsDir() && entry.Name() == "metadata.toml" {
|
||||
log.Printf("Directory `%s` already initialised", directory)
|
||||
os.Exit(0)
|
||||
return
|
||||
}
|
||||
|
||||
defaultMetadata := scanner.DefaultMetadata()
|
||||
metadataStr, _ := toml.Marshal(defaultMetadata)
|
||||
err = os.WriteFile(path.Join(directory, "metadata.toml"), metadataStr, 0o644)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to write to file: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
log.Printf("Directory successfully initialised")
|
||||
os.Exit(0)
|
||||
}
|
||||
if cfg == nil {
|
||||
log.Printf(
|
||||
"New config file created (%s). " +
|
||||
"Please edit this file before running again!",
|
||||
config.CONFIG_FILENAME,
|
||||
)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
metadata, err := scanner.FetchMetadata(directory)
|
||||
// initialising directory (--init)
|
||||
if initDirectory {
|
||||
err = initialiseDirectory(directory)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to initialise directory: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
log.Printf("Directory successfully initialised")
|
||||
}
|
||||
|
||||
// read directory metadata
|
||||
metadata, err := scanner.ReadMetadata(directory)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to fetch VOD metadata: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if metadata == nil {
|
||||
log.Fatal("Directory contained no metadata. Use `--init` to initialise this directory.")
|
||||
log.Fatal(
|
||||
"Directory contained no metadata. " +
|
||||
"Use `--init` to initialise this directory.",
|
||||
)
|
||||
os.Exit(1)
|
||||
}
|
||||
vodFiles, err := scanner.FetchVideos(metadata.FootageDir)
|
||||
|
||||
// skip uploading if already done
|
||||
if metadata.Uploaded == !forceUpload {
|
||||
log.Printf(
|
||||
"VOD has already been uploaded. " +
|
||||
"Use --force to override, or update the %s.",
|
||||
scanner.METADATA_FILENAME,
|
||||
)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// scan for VOD segments
|
||||
vodFiles, err := scanner.ScanSegments(metadata.FootageDir, segmentExtension)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to fetch VOD filenames: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if len(vodFiles) == 0 {
|
||||
log.Fatal("Directory contained no VOD files (expecting .mkv)")
|
||||
log.Fatalf(
|
||||
"Directory contained no VOD files (expecting .%s)",
|
||||
segmentExtension,
|
||||
)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if verbose {
|
||||
enc := json.NewEncoder(os.Stdout)
|
||||
enc.SetIndent("", "\t")
|
||||
|
|
@ -176,6 +153,7 @@ func main() {
|
|||
enc.Encode(vodFiles)
|
||||
}
|
||||
|
||||
// build video template for upload
|
||||
video, err := yt.BuildVideo(metadata)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to build video template: %v", err)
|
||||
|
|
@ -202,17 +180,76 @@ func main() {
|
|||
)
|
||||
}
|
||||
|
||||
err = vid.ConcatVideo(video, vodFiles)
|
||||
// concatenate VOD segments into full VOD
|
||||
err = vid.ConcatVideo(video, vodFiles, verbose)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to concatenate VOD files: %v", err)
|
||||
log.Fatalf("Failed to concatenate VOD segments: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// okay actual youtube stuff now
|
||||
|
||||
// TODO: tidy up oauth flow with localhost webserver
|
||||
// youtube oauth flow
|
||||
ctx := context.Background()
|
||||
config := &oauth2.Config{
|
||||
token, err := completeOAuth(&ctx, cfg)
|
||||
if err != nil {
|
||||
log.Fatalf("OAuth flow failed: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// okay actually upload now!
|
||||
ytVideo, err := yt.UploadVideo(ctx, token, video)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to upload video: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if verbose {
|
||||
jsonString, err := json.MarshalIndent(ytVideo, "", " ")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to marshal video data json: %v", err)
|
||||
}
|
||||
fmt.Println(string(jsonString))
|
||||
}
|
||||
log.Print("Video uploaded successfully!")
|
||||
|
||||
// update metadata to reflect VOD is uploaded
|
||||
metadata.Uploaded = true
|
||||
err = scanner.WriteMetadata(directory, metadata)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to update metadata: %v", err)
|
||||
}
|
||||
|
||||
// delete full VOD after upload, if requested
|
||||
if deleteFullVod {
|
||||
err = os.Remove(path.Join(directory, scanner.METADATA_FILENAME))
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to delete full VOD: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func initialiseDirectory(directory string) error {
|
||||
dirInfo, err := os.Stat(directory)
|
||||
if err != nil {
|
||||
if err == os.ErrNotExist {
|
||||
return fmt.Errorf("no such directory: %s", directory)
|
||||
}
|
||||
return fmt.Errorf("failed to open directory: %v", err)
|
||||
}
|
||||
if !dirInfo.IsDir() {
|
||||
return fmt.Errorf("not a directory: %s", directory)
|
||||
}
|
||||
|
||||
_, err = os.Stat(path.Join(directory, scanner.METADATA_FILENAME))
|
||||
if err == nil {
|
||||
return fmt.Errorf("directory already initialised: %v", err)
|
||||
}
|
||||
|
||||
err = scanner.WriteMetadata(directory, scanner.DefaultMetadata())
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func completeOAuth(ctx *context.Context, cfg *config.Config) (*oauth2.Token, error) {
|
||||
oauth2Config := &oauth2.Config{
|
||||
ClientID: cfg.Google.ClientID,
|
||||
ClientSecret: cfg.Google.ClientSecret,
|
||||
Endpoint: google.Endpoint,
|
||||
|
|
@ -220,21 +257,25 @@ func main() {
|
|||
RedirectURL: "http://localhost:8090",
|
||||
}
|
||||
verifier := oauth2.GenerateVerifier()
|
||||
url := config.AuthCodeURL("state", oauth2.AccessTypeOffline, oauth2.S256ChallengeOption(verifier))
|
||||
log.Printf("Visit URL to initiate OAuth2: %s", url)
|
||||
|
||||
|
||||
url := oauth2Config.AuthCodeURL("state", oauth2.AccessTypeOffline, oauth2.S256ChallengeOption(verifier))
|
||||
fmt.Printf("Sign in to YouTube: %s\n", url)
|
||||
|
||||
// TODO: tidy up oauth flow with localhost webserver
|
||||
var code string
|
||||
fmt.Print("Enter OAuth2 code: ")
|
||||
if _, err := fmt.Scan(&code); err != nil {
|
||||
log.Fatalf("Failed to read oauth2 code: %v", err)
|
||||
return nil, fmt.Errorf("failed to read code: %v", err)
|
||||
}
|
||||
|
||||
token, err := config.Exchange(ctx, code, oauth2.VerifierOption(verifier))
|
||||
log.Printf("Token expires on %s\n", token.Expiry.Format("02 Jan 2006"))
|
||||
token, err := oauth2Config.Exchange(*ctx, code, oauth2.VerifierOption(verifier))
|
||||
if err != nil {
|
||||
log.Fatalf("Could not exchange OAuth2 code: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
yt.UploadVideo(ctx, token, video)
|
||||
// TODO: save this token; look into token refresh
|
||||
log.Printf("Token expires on %s\n", token.Expiry.Format("02 Jan 2006"))
|
||||
|
||||
return token, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue