code refactor: tidy-up
This commit is contained in:
parent
0ac59c5407
commit
3053c19dd0
3 changed files with 98 additions and 87 deletions
90
main.go
90
main.go
|
|
@ -7,17 +7,16 @@ import (
|
|||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/oauth2/google"
|
||||
"google.golang.org/api/youtube/v3"
|
||||
|
||||
"arimelody.space/vodular/config"
|
||||
"arimelody.space/vodular/oauth"
|
||||
"arimelody.space/vodular/scanner"
|
||||
vid "arimelody.space/vodular/video"
|
||||
yt "arimelody.space/vodular/youtube"
|
||||
|
|
@ -144,7 +143,7 @@ func main() {
|
|||
|
||||
// initialising directory (--init)
|
||||
if initDirectory {
|
||||
err = initialiseDirectory(directory)
|
||||
err = scanner.InitialiseDirectory(directory)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to initialise directory: %v", err)
|
||||
os.Exit(1)
|
||||
|
|
@ -321,7 +320,7 @@ func main() {
|
|||
if cfg.Token != nil {
|
||||
token = cfg.Token
|
||||
} else {
|
||||
token, err = generateOAuthToken(&ctx, oauth2Config, cfg)
|
||||
token, err = oauth.GenerateToken(&ctx, oauth2Config, cfg)
|
||||
if err != nil {
|
||||
log.Fatalf("OAuth flow failed: %v", err)
|
||||
os.Exit(1)
|
||||
|
|
@ -374,86 +373,3 @@ func main() {
|
|||
log.Print("Dry run: Skipping video upload")
|
||||
}
|
||||
}
|
||||
|
||||
func initialiseDirectory(directory string) error {
|
||||
dirInfo, err := os.Stat(directory)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
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: %s", directory)
|
||||
}
|
||||
|
||||
err = scanner.WriteMetadata(directory, scanner.DefaultMetadata())
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func generateOAuthToken(
|
||||
ctx *context.Context,
|
||||
oauth2Config *oauth2.Config,
|
||||
cfg *config.Config,
|
||||
) (*oauth2.Token, error) {
|
||||
verifier := oauth2.GenerateVerifier()
|
||||
|
||||
var token *oauth2.Token
|
||||
wg := sync.WaitGroup{}
|
||||
var server http.Server
|
||||
server.Addr = cfg.Host
|
||||
server.Handler = http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
if !r.URL.Query().Has("code") {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
code := r.URL.Query().Get("code")
|
||||
|
||||
t, err := oauth2Config.Exchange(*ctx, code, oauth2.VerifierOption(verifier))
|
||||
if err != nil {
|
||||
log.Fatalf("Could not exchange OAuth2 code: %v", err)
|
||||
http.Error( w,
|
||||
fmt.Sprintf("Could not exchange OAuth2 code: %v", err),
|
||||
http.StatusBadRequest,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
token = t
|
||||
http.Error(
|
||||
w,
|
||||
"Authentication successful! You may now close this tab.",
|
||||
http.StatusOK,
|
||||
)
|
||||
if f, ok := w.(http.Flusher); ok {
|
||||
f.Flush()
|
||||
}
|
||||
|
||||
wg.Done()
|
||||
server.Close()
|
||||
},
|
||||
)
|
||||
|
||||
url := oauth2Config.AuthCodeURL(
|
||||
"state",
|
||||
oauth2.AccessTypeOffline,
|
||||
oauth2.S256ChallengeOption(verifier),
|
||||
)
|
||||
fmt.Printf("\nSign in to YouTube: %s\n\n", url)
|
||||
|
||||
wg.Add(1)
|
||||
if err := server.ListenAndServe(); err != http.ErrServerClosed {
|
||||
return nil, fmt.Errorf("http: %v", err)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
return token, nil
|
||||
}
|
||||
|
|
|
|||
73
oauth/oauth.go
Normal file
73
oauth/oauth.go
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
package oauth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"arimelody.space/vodular/config"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
func GenerateToken(
|
||||
ctx *context.Context,
|
||||
oauth2Config *oauth2.Config,
|
||||
cfg *config.Config,
|
||||
) (*oauth2.Token, error) {
|
||||
verifier := oauth2.GenerateVerifier()
|
||||
|
||||
var token *oauth2.Token
|
||||
wg := sync.WaitGroup{}
|
||||
var server http.Server
|
||||
server.Addr = cfg.Host
|
||||
server.Handler = http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
if !r.URL.Query().Has("code") {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
code := r.URL.Query().Get("code")
|
||||
|
||||
t, err := oauth2Config.Exchange(*ctx, code, oauth2.VerifierOption(verifier))
|
||||
if err != nil {
|
||||
log.Fatalf("Could not exchange OAuth2 code: %v", err)
|
||||
http.Error( w,
|
||||
fmt.Sprintf("Could not exchange OAuth2 code: %v", err),
|
||||
http.StatusBadRequest,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
token = t
|
||||
http.Error(
|
||||
w,
|
||||
"Authentication successful! You may now close this tab.",
|
||||
http.StatusOK,
|
||||
)
|
||||
if f, ok := w.(http.Flusher); ok {
|
||||
f.Flush()
|
||||
}
|
||||
|
||||
wg.Done()
|
||||
server.Close()
|
||||
},
|
||||
)
|
||||
|
||||
url := oauth2Config.AuthCodeURL(
|
||||
"state",
|
||||
oauth2.AccessTypeOffline,
|
||||
oauth2.S256ChallengeOption(verifier),
|
||||
)
|
||||
fmt.Printf("\nSign in to YouTube: %s\n\n", url)
|
||||
|
||||
wg.Add(1)
|
||||
if err := server.ListenAndServe(); err != http.ErrServerClosed {
|
||||
return nil, fmt.Errorf("http: %v", err)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
return token, nil
|
||||
}
|
||||
|
|
@ -42,6 +42,28 @@ type (
|
|||
|
||||
const METADATA_FILENAME = "metadata.toml"
|
||||
|
||||
func InitialiseDirectory(directory string) error {
|
||||
dirInfo, err := os.Stat(directory)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
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, METADATA_FILENAME))
|
||||
if err == nil {
|
||||
return fmt.Errorf("directory already initialised: %s", directory)
|
||||
}
|
||||
|
||||
err = WriteMetadata(directory, DefaultMetadata())
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func ScanSegments(directory string, extension string) ([]string, error) {
|
||||
entries, err := os.ReadDir(directory)
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue