Skip to content

Commit

Permalink
Add disable-save-state option
Browse files Browse the repository at this point in the history
Closes #177
  • Loading branch information
Kethsar committed May 16, 2024
1 parent 6f2ac6f commit 1b69799
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 44 deletions.
29 changes: 15 additions & 14 deletions Info.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,20 @@ type DownloadInfo struct {
CookiesURL *url.URL
Ytcfg *YTCFG

Stopping bool
InProgress bool
Live bool
VP9 bool
H264 bool
Unavailable bool
GVideoDDL bool
FragFiles bool
LiveURL bool
AudioOnly bool
VideoOnly bool
MembersOnly bool
InfoPrinted bool
Stopping bool
InProgress bool
Live bool
VP9 bool
H264 bool
Unavailable bool
GVideoDDL bool
FragFiles bool
LiveURL bool
AudioOnly bool
VideoOnly bool
MembersOnly bool
InfoPrinted bool
DisableSaveState bool

Thumbnail string
VideoID string
Expand Down Expand Up @@ -413,7 +414,7 @@ func (di *DownloadInfo) PrintStatus() {
}

func (di *DownloadInfo) SaveState(itag int) {
if len(di.DLState[itag].File) == 0 {
if di.DisableSaveState || len(di.DLState[itag].File) == 0 {
return
}

Expand Down
69 changes: 41 additions & 28 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ var (
vp9 bool
h264 bool
membersOnly bool
disableSaveState bool

cancelled = false
)
Expand Down Expand Up @@ -461,6 +462,7 @@ func init() {
cliFlags.BoolVar(&separateAudio, "separate-audio", false, "Save a copy of the audio separately along with the muxed file.")
cliFlags.BoolVar(&monitorChannel, "monitor-channel", false, "Continually monitor a channel for streams.")
cliFlags.BoolVar(&membersOnly, "members-only", false, "Only download members-only streams when waiting on a channel URL such as /live.")
cliFlags.BoolVar(&disableSaveState, "disable-save-state", false, "Disable resumable download state.")
cliFlags.StringVar(&cookieFile, "c", "", "Cookies to be used when downloading.")
cliFlags.StringVar(&cookieFile, "cookies", "", "Cookies to be used when downloading.")
cliFlags.StringVar(&fnameFormat, "o", DefaultFilenameFormat, "Filename output format.")
Expand Down Expand Up @@ -546,6 +548,7 @@ func run() int {
info.MembersOnly = membersOnly
info.FileMode = os.FileMode(filePerms)
info.DirMode = os.FileMode(dirPerms)
info.DisableSaveState = disableSaveState

if doWait {
info.Wait = ActionDo
Expand All @@ -565,10 +568,10 @@ func run() int {
saveFilesOnCancel = ActionDoNot
}

if doSaveState {
saveStateOnCancel = ActionDo
} else if noSaveState {
if noSaveState || disableSaveState {
saveStateOnCancel = ActionDoNot
} else if doSaveState {
saveStateOnCancel = ActionDo
}

if audioOnly {
Expand Down Expand Up @@ -705,24 +708,26 @@ func run() int {
}
}

info.DLState[AudioItag].File = filepath.Join(tempDir, fmt.Sprintf("%s.f%d.state", info.VideoID, AudioItag))
info.DLState[info.Quality].File = filepath.Join(tempDir, fmt.Sprintf("%s.f%d.state", info.VideoID, info.Quality))
if Exists(info.DLState[AudioItag].File) {
stateData, err := os.ReadFile(info.DLState[AudioItag].File)
if err == nil {
err = json.Unmarshal(stateData, info.DLState[AudioItag])
}
if err == nil {
tmpDir = info.DLState[AudioItag].TempDir
}
}
if Exists(info.DLState[info.Quality].File) {
stateData, err := os.ReadFile(info.DLState[info.Quality].File)
if err == nil {
err = json.Unmarshal(stateData, info.DLState[info.Quality])
if !disableSaveState {
info.DLState[AudioItag].File = filepath.Join(tempDir, fmt.Sprintf("%s.f%d.state", info.VideoID, AudioItag))
info.DLState[info.Quality].File = filepath.Join(tempDir, fmt.Sprintf("%s.f%d.state", info.VideoID, info.Quality))
if Exists(info.DLState[AudioItag].File) {
stateData, err := os.ReadFile(info.DLState[AudioItag].File)
if err == nil {
err = json.Unmarshal(stateData, info.DLState[AudioItag])
}
if err == nil {
tmpDir = info.DLState[AudioItag].TempDir
}
}
if err == nil && len(tmpDir) == 0 {
tmpDir = info.DLState[AudioItag].TempDir
if Exists(info.DLState[info.Quality].File) {
stateData, err := os.ReadFile(info.DLState[info.Quality].File)
if err == nil {
err = json.Unmarshal(stateData, info.DLState[info.Quality])
}
if err == nil && len(tmpDir) == 0 {
tmpDir = info.DLState[AudioItag].TempDir
}
}
}

Expand All @@ -739,8 +744,10 @@ func run() int {
tmpDir = fdir
}

for _, state := range info.DLState {
state.TempDir = tmpDir
if !disableSaveState {
for _, state := range info.DLState {
state.TempDir = tmpDir
}
}
}

Expand Down Expand Up @@ -936,16 +943,20 @@ func run() int {
os.RemoveAll(tmpDir)
}

for _, state := range info.DLState {
TryDelete(state.File)
if !disableSaveState {
for _, state := range info.DLState {
TryDelete(state.File)
}
}
} else if !saveState {
if tmpDir != fdir {
os.RemoveAll(tmpDir)
}

for _, state := range info.DLState {
TryDelete(state.File)
if !disableSaveState {
for _, state := range info.DLState {
TryDelete(state.File)
}
}
}

Expand All @@ -961,8 +972,10 @@ func run() int {
}

signal.Reset(os.Interrupt)
for _, state := range info.DLState {
TryDelete(state.File)
if !disableSaveState {
for _, state := range info.DLState {
TryDelete(state.File)
}
}
if loglevel > LoglevelQuiet {
fmt.Fprintln(os.Stderr)
Expand Down
6 changes: 4 additions & 2 deletions make.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash
if [[ -n "$1" ]]; then
if [[ "$1" = "t" ]]; then
go build -race -ldflags "-X main.Commit=-$(git rev-parse --short HEAD)"
elif [[ -n "$1" ]]; then
CGO_ENABLED=0 go build -ldflags "-X main.Commit=-$(git rev-parse --short HEAD)"
GOOS=windows GOARCH=amd64 go build -ldflags "-X main.Commit=-$(git rev-parse --short HEAD)"
else
Expand All @@ -10,4 +12,4 @@ fi
zip ytarchive_linux_amd64.zip ytarchive
zip ytarchive_windows_amd64.zip ytarchive.exe

sha256sum ytarchive_linux_amd64.zip ytarchive_windows_amd64.zip > SHA2-256SUMS
sha256sum ytarchive_linux_amd64.zip ytarchive_windows_amd64.zip > SHA2-256SUMS

0 comments on commit 1b69799

Please sign in to comment.