Refactoring, added remaining custom errors
This commit is contained in:
parent
424e912f6c
commit
ee3518ab5c
5 changed files with 77 additions and 36 deletions
|
@ -35,7 +35,7 @@ type FileExistsError struct {
|
|||
}
|
||||
|
||||
func (err *FileExistsError) Error() string {
|
||||
return "File '" + err.Filename + "' already exists. See the available options on how to proceed."
|
||||
return "file '" + err.Filename + "' already exists - see the available options on how to proceed"
|
||||
}
|
||||
|
||||
type FormatNotFoundError struct {
|
||||
|
@ -43,7 +43,7 @@ type FormatNotFoundError struct {
|
|||
}
|
||||
|
||||
func (err *FormatNotFoundError) Error() string {
|
||||
return "Format " + err.FormatName + " is not available."
|
||||
return "format " + err.FormatName + " is not available"
|
||||
}
|
||||
|
||||
type ChapterNotFoundError struct {
|
||||
|
@ -51,5 +51,27 @@ type ChapterNotFoundError struct {
|
|||
}
|
||||
|
||||
func (err *ChapterNotFoundError) Error() string {
|
||||
return fmt.Sprintf("Chapter %v not found.", err.ChapterNum)
|
||||
return fmt.Sprintf("chapter %v not found", err.ChapterNum)
|
||||
}
|
||||
|
||||
type VideoCategoryUnsupportedError struct {
|
||||
Category string
|
||||
}
|
||||
|
||||
func (err *VideoCategoryUnsupportedError) Error() string {
|
||||
return fmt.Sprintf("video category '%v' not supported", err.Category)
|
||||
}
|
||||
|
||||
type GtvVideoUrlParseError struct {
|
||||
Url string
|
||||
}
|
||||
|
||||
func (err *GtvVideoUrlParseError) Error() string {
|
||||
return fmt.Sprintf("Could not parse URL %v", err.Url)
|
||||
}
|
||||
|
||||
type DownloadInfoFileReadError struct {}
|
||||
|
||||
func (err *DownloadInfoFileReadError) Error() string {
|
||||
return "could not read download info file, can't continue download"
|
||||
}
|
||||
|
|
|
@ -4,7 +4,6 @@ package core
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"iter"
|
||||
|
@ -113,7 +112,7 @@ func GetStreamChunkList(video VideoFormat) (ChunkList, error) {
|
|||
return chunklist, err
|
||||
}
|
||||
|
||||
func DownloadEpisode(
|
||||
func DownloadStreamEpisode(
|
||||
ep StreamEpisode,
|
||||
chapter Chapter,
|
||||
formatName string,
|
||||
|
@ -128,7 +127,7 @@ func DownloadEpisode(
|
|||
return func (yield func(DownloadProgress) bool) {
|
||||
// Set automatic values
|
||||
if outputFile == "" {
|
||||
outputFile = ep.GetProposedFilename(chapter.Index)
|
||||
outputFile = ep.GetProposedFilename(chapter)
|
||||
}
|
||||
if chapter.Index >= 0 {
|
||||
if startDuration < 0 {
|
||||
|
@ -167,7 +166,7 @@ func DownloadEpisode(
|
|||
if continueDl {
|
||||
infoFileData, err := os.ReadFile(infoFilename)
|
||||
if err != nil {
|
||||
yield(DownloadProgress{Error: errors.New("could not access download info file, can't continue download")})
|
||||
yield(DownloadProgress{Error: &DownloadInfoFileReadError{}})
|
||||
return
|
||||
}
|
||||
i, err := strconv.ParseInt(string(infoFileData), 10, 32)
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
package core
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"time"
|
||||
|
@ -40,10 +39,13 @@ func ParseGtvVideoUrl(url string) (GtvVideo, error) {
|
|||
video := GtvVideo{}
|
||||
match := videoUrlRegex.FindStringSubmatch(url)
|
||||
if len(match) < 2 {
|
||||
return video, errors.New("Could not parse URL " + url)
|
||||
return video, &GtvVideoUrlParseError{Url: url}
|
||||
}
|
||||
video.Category = match[1]
|
||||
video.Id = match[2]
|
||||
if video.Category != "streams" {
|
||||
return video, &VideoCategoryUnsupportedError{Category: video.Category}
|
||||
}
|
||||
return video, nil
|
||||
}
|
||||
|
||||
|
@ -109,9 +111,21 @@ func (ep *StreamEpisode) GetFormatByName(formatName string) (VideoFormat, error)
|
|||
}
|
||||
}
|
||||
|
||||
func (ep *StreamEpisode) GetProposedFilename(chapterIdx int) string {
|
||||
if chapterIdx >= 0 && chapterIdx < len(ep.Chapters) {
|
||||
return fmt.Sprintf("GTV%04s - %v. %s.ts", ep.Episode, chapterIdx+1, sanitizeUnicodeFilename(ep.Chapters[chapterIdx].Title))
|
||||
func (ep *StreamEpisode) GetChapterByNumber(number int) (Chapter, error) {
|
||||
chapter := Chapter{Index: -1} // set Index to -1 for noop
|
||||
idx := number-1
|
||||
if idx >= 0 && idx >= len(ep.Chapters) {
|
||||
return chapter, &ChapterNotFoundError{ChapterNum: number}
|
||||
}
|
||||
if len(ep.Chapters) > 0 && idx >= 0 {
|
||||
chapter = ep.Chapters[idx]
|
||||
}
|
||||
return chapter, nil
|
||||
}
|
||||
|
||||
func (ep *StreamEpisode) GetProposedFilename(chapter Chapter) string {
|
||||
if chapter.Index >= 0 && chapter.Index < len(ep.Chapters) {
|
||||
return fmt.Sprintf("GTV%04s - %v. %s.ts", ep.Episode, chapter.Index, sanitizeUnicodeFilename(ep.Chapters[chapter.Index].Title))
|
||||
} else {
|
||||
return sanitizeUnicodeFilename(ep.Title) + ".ts"
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue