Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Commit

Permalink
Merge pull request #34 from dbarbuzzi/bugfix/change-concat-approach
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarbuzzi authored Jan 8, 2021
2 parents 991e85a + a919146 commit 4a7eda8
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 227 deletions.
206 changes: 206 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"regexp"

"github.com/BurntSushi/toml"
"github.com/pkg/errors"
)

// Config represents a config object containing everything needed to download a VOD
type Config struct {
AuthToken string
ClientID string
Quality string
StartTime string
StartSec int
EndTime string
EndSec int
Length string
VodID int
FilePrefix string
OutputFolder string
Workers int
}

// Privatize returns a copy of the struct with the ClientID field censored (e.g. for logging)
func (c Config) Privatize() Config {
c2 := c
c2.AuthToken = "********"
c2.ClientID = "********"
return c2
}

// Update replaces any config values in the base object with those present in the passed argument
func (c *Config) Update(c2 Config) {
if c2.AuthToken != "" {
c.AuthToken = c2.AuthToken
}
if c2.ClientID != "" {
c.ClientID = c2.ClientID
}
if c2.Quality != "" {
c.Quality = c2.Quality
}
if c2.StartTime != "" {
c.StartTime = c2.StartTime
}
if c2.EndTime != "" {
c.EndTime = c2.EndTime
}
if c2.Length != "" {
c.EndTime = c2.Length
}
if c2.VodID != 0 {
c.VodID = c2.VodID
}
if c2.FilePrefix != "" {
c.FilePrefix = c2.FilePrefix
}
if c2.OutputFolder != "" {
c.OutputFolder = c2.OutputFolder
}
if c2.Workers != 0 {
c.Workers = c2.Workers
}
}

// Validate checks if the config object appears valid. Required attributes must
// be present and appear correct. Optional values are validated if present.
//
// Currently, "OutputFolder" is not validated (needs logic to support Windows paths)
func (c Config) Validate() error {
if len(c.AuthToken) == 0 {
return fmt.Errorf("error: AuthToken missing")
}
if len(c.ClientID) == 0 {
return fmt.Errorf("error: ClientID missing")
}

if c.VodID < 1 {
return fmt.Errorf("error: VodID missing")
}

timePattern := `\d+ \d+ \d+`
timeRegex := regexp.MustCompile(timePattern)
if c.StartTime != "start" && !timeRegex.MatchString(c.StartTime) {
return fmt.Errorf("error: StartTime must be 'start' or in format '%s'; got '%s'", timePattern, c.StartTime)
}
if c.EndTime == "" && c.Length == "" {
return errors.New("error: must specify either EndTime or Length")
}
if c.Length == "" && c.EndTime != "end" && !timeRegex.MatchString(c.EndTime) {
return fmt.Errorf("error: EndTime must be 'end' or in format '%s'; got '%s'", timePattern, c.EndTime)
}
if c.EndTime == "" && c.Length != "full" && !timeRegex.MatchString(c.Length) {
return fmt.Errorf("error: Length must be 'full' or in format '%s'; got '%s'", timePattern, c.Length)
}

qualityPattern := `\d{3,4}p[36]0`
qualityRegex := regexp.MustCompile(qualityPattern)
if c.Quality != "best" && c.Quality != "chunked" && !qualityRegex.MatchString(c.Quality) {
return fmt.Errorf("error: Quality must be 'best', 'chunked', or in format '%s'; got '%s'", qualityPattern, c.Quality)
}

if c.FilePrefix != "" && !isValidFilename(c.FilePrefix) {
return fmt.Errorf("error: FilePrefix contains invalid characters; got '%s'", c.FilePrefix)
}

if c.Workers < 1 {
return fmt.Errorf("error: Worker must be an integer greater than 0; got '%d'", c.Workers)
}

return nil
}

// ResolveEndTime is a hacky way of supporting organically support the new
// 'length' option. If that prop is included, it is used to calculate & update
// EndTime prop (it is intended to override)
func (c *Config) ResolveEndTime() error {
startAt, err := timeInputToSeconds(c.StartTime)
if err != nil {
return err
}
c.StartSec = startAt

if c.Length != "" {
if c.Length == "full" {
c.EndSec = -1
} else {
endAt, err := timeInputToSeconds(c.Length)
if err != nil {
return err
}
c.EndSec = startAt + endAt
}
} else {
if c.EndTime == "end" {
c.EndSec = -1
} else {
endAt, err := timeInputToSeconds(c.EndTime)
if err != nil {
return err
}
c.EndSec = endAt
}
}

return nil
}

func loadConfig(f string) (Config, error) {
log.Printf("loading config file <%s>\n", f)
var config Config

configData, err := ioutil.ReadFile(f)
if err != nil {
return config, errors.Wrap(err, "failed to load config file")
}

err = toml.Unmarshal(configData, &config)
if err != nil {
return config, errors.Wrap(err, "failed to parse config file")
}

return config, nil
}

func buildConfigFromFlags() (Config, error) {
var config Config

if *authToken != "" {
config.AuthToken = *authToken
}
if *clientID != "" {
config.ClientID = *clientID
}
if *quality != "" {
config.Quality = *quality
}
if *startTime != "" {
config.StartTime = *startTime
}
if *endTime != "" {
config.EndTime = *endTime
}
if *length != "" {
config.Length = *length
}
if *prefix != "" {
config.FilePrefix = *prefix
}
if *folder != "" {
config.OutputFolder = *folder
}
if *workers != 0 {
config.Workers = *workers
}
if *vodID != 0 {
config.VodID = *vodID
}

return config, nil
}
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ require (
github.com/BurntSushi/toml v0.3.0
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
github.com/cheggaaa/pb/v3 v3.0.5
github.com/fatih/color v1.9.0 // indirect
github.com/mattn/go-colorable v0.1.7 // indirect
github.com/mattn/go-runewidth v0.0.2 // indirect
github.com/pkg/errors v0.8.0
github.com/stretchr/testify v1.6.1 // indirect
gopkg.in/alecthomas/kingpin.v2 v2.2.6
gopkg.in/cheggaaa/pb.v1 v1.0.25
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
github.com/BurntSushi/toml v0.3.0 h1:e1/Ivsx3Z0FVTV0NSOv/aVgbUWyQuzj7DDnFblkRvsY=
github.com/BurntSushi/toml v0.3.0/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/VividCortex/ewma v1.1.1 h1:MnEK4VOv6n0RSY4vtRe3h11qjxL3+t0B8yOL8iMXdcM=
github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/cheggaaa/pb v1.0.29 h1:FckUN5ngEk2LpvuG0fw1GEFx6LtyY2pWI/Z2QgCnEYo=
github.com/cheggaaa/pb/v3 v3.0.5 h1:lmZOti7CraK9RSjzExsY53+WWfub9Qv13B5m4ptEoPE=
github.com/cheggaaa/pb/v3 v3.0.5/go.mod h1:X1L61/+36nz9bjIsrDU52qHKOQukUQe2Ge+YvGuquCw=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.7 h1:bQGKb3vps/j0E9GfJQ03JyhRuxsvdAanXlT9BTw3mdw=
github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
Expand All @@ -17,6 +24,8 @@ github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHX
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-runewidth v0.0.2 h1:UnlwIPBGaTZfPQ6T1IGzPI0EkYAQmT9fAEJ/poFC63o=
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
Loading

0 comments on commit 4a7eda8

Please sign in to comment.