Skip to content

Commit

Permalink
Feat: Fetch next episode link for faster playback
Browse files Browse the repository at this point in the history
  • Loading branch information
Wraient authored Feb 5, 2025
2 parents 3b5b246 + b698507 commit ee5a90c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
27 changes: 27 additions & 0 deletions internal/curd.go
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,10 @@ func WriteTokenToFile(token string, filePath string) error {

func StartCurd(userCurdConfig *CurdConfig, anime *Anime, logFile string) string {

if anime.Ep.NextEpisode.Number == anime.Ep.Number {
anime.Ep.Links = anime.Ep.NextEpisode.Links
fmt.Println("using prefetched next episode link")
} else {
// Get episode link
link, err := GetEpisodeURL(*userCurdConfig, anime.AllanimeId, anime.Ep.Number)
if err != nil {
Expand Down Expand Up @@ -1008,6 +1012,7 @@ func StartCurd(userCurdConfig *CurdConfig, anime *Anime, logFile string) string
// anime.Ep.Links = link
}
anime.Ep.Links = link
}

if len(anime.Ep.Links) == 0 {
CurdOut("No episode links found")
Expand All @@ -1016,6 +1021,28 @@ func StartCurd(userCurdConfig *CurdConfig, anime *Anime, logFile string) string

Log(anime, logFile)

// Modify the goroutine in main.go where next episode links are fetched
// Get next episode link in parallel
go func() {
nextEpNum := anime.Ep.Number + 1
if nextEpNum <= anime.TotalEpisodes {
// Get next canon episode number if filler skip is enabled
if userCurdConfig.SkipFiller && IsEpisodeFiller(anime.FillerEpisodes, anime.Ep.Number) {
nextEpNum = GetNextCanonEpisode(anime.FillerEpisodes, nextEpNum)
}
nextLinks, err := GetEpisodeURL(*userCurdConfig, anime.AllanimeId, nextEpNum)
if err != nil {
Log("Error getting next episode link: "+err.Error(), logFile)
} else {
anime.Ep.NextEpisode = NextEpisode{
Number: nextEpNum,
Links: nextLinks,
}
Log(fmt.Sprintf("Next episode link fetched for ep %d", nextEpNum), logFile)
}
}
}()

// Write anime.AnilistId to curd_id in the storage path
idFilePath := filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "curd_id")
Log(fmt.Sprintf("idFilePath: %v", idFilePath), logFile)
Expand Down
6 changes: 6 additions & 0 deletions internal/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Episode struct {
Started bool `json:"started"`
Duration int `json:"duration"`
Links []string `json:"links"`
NextEpisode NextEpisode `json:"next_episode"`
IsFiller bool `json:"filler"`
IsRecap bool `json:"recap"`
Aired string `json:"aired"`
Expand All @@ -46,6 +47,11 @@ type Episode struct {
IsCompleted bool
}

type NextEpisode struct {
Number int
Links []string
}

type playingVideo struct {
Url string
Speed float64 `json:"speed"`
Expand Down

0 comments on commit ee5a90c

Please sign in to comment.