Fix crunchyroll api changes

This commit is contained in:
bytedream 2022-08-08 22:20:25 +02:00
parent a64981930b
commit 6239d10d22
3 changed files with 22 additions and 55 deletions

View file

@ -627,6 +627,12 @@ func archiveExtractEpisodes(url string) ([][]utils.FormatInformation, error) {
} }
} }
if _, ok := crunchyroll.ParseBetaEpisodeURL(url); ok {
return nil, fmt.Errorf("archiving episodes by url is no longer supported (thx crunchyroll). use the series url instead and filter after the given episode (https://github.com/crunchy-labs/crunchy-cli/wiki/Cli#filter)")
} else if _, _, _, _, ok := crunchyroll.ParseEpisodeURL(url); ok {
return nil, fmt.Errorf("archiving episodes by url is no longer supported (thx crunchyroll). use the series url instead and filter after the given episode (https://github.com/crunchy-labs/crunchy-cli/wiki/Cli#filter)")
}
episodes, err := utils.ExtractEpisodes(url, languagesAsLocale...) episodes, err := utils.ExtractEpisodes(url, languagesAsLocale...)
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -14,13 +14,11 @@ import (
"os/signal" "os/signal"
"path/filepath" "path/filepath"
"runtime" "runtime"
"sort"
"strconv" "strconv"
"strings" "strings"
) )
var ( var (
downloadAudioFlag string
downloadSubtitleFlag string downloadSubtitleFlag string
downloadDirectoryFlag string downloadDirectoryFlag string
@ -49,12 +47,10 @@ var Cmd = &cobra.Command{
} }
} }
if !crunchyUtils.ValidateLocale(crunchyroll.LOCALE(downloadAudioFlag)) { if downloadSubtitleFlag != "" && !crunchyUtils.ValidateLocale(crunchyroll.LOCALE(downloadSubtitleFlag)) {
return fmt.Errorf("%s is not a valid audio locale. Choose from: %s", downloadAudioFlag, strings.Join(utils.LocalesAsStrings(), ", "))
} else if downloadSubtitleFlag != "" && !crunchyUtils.ValidateLocale(crunchyroll.LOCALE(downloadSubtitleFlag)) {
return fmt.Errorf("%s is not a valid subtitle locale. Choose from: %s", downloadSubtitleFlag, strings.Join(utils.LocalesAsStrings(), ", ")) return fmt.Errorf("%s is not a valid subtitle locale. Choose from: %s", downloadSubtitleFlag, strings.Join(utils.LocalesAsStrings(), ", "))
} }
utils.Log.Debug("Locales: audio: %s / subtitle: %s", downloadAudioFlag, downloadSubtitleFlag) utils.Log.Debug("Subtitle locale: %s", downloadSubtitleFlag)
switch downloadResolutionFlag { switch downloadResolutionFlag {
case "1080p", "720p", "480p", "360p": case "1080p", "720p", "480p", "360p":
@ -81,10 +77,6 @@ var Cmd = &cobra.Command{
} }
func init() { func init() {
Cmd.Flags().StringVarP(&downloadAudioFlag, "audio",
"a",
string(utils.SystemLocale(false)),
"The locale of the audio. Available locales: "+strings.Join(utils.LocalesAsStrings(), ", "))
Cmd.Flags().StringVarP(&downloadSubtitleFlag, Cmd.Flags().StringVarP(&downloadSubtitleFlag,
"subtitle", "subtitle",
"s", "s",
@ -280,49 +272,13 @@ func downloadInfo(info utils.FormatInformation, file *os.File) error {
} }
func downloadExtractEpisodes(url string) ([][]utils.FormatInformation, error) { func downloadExtractEpisodes(url string) ([][]utils.FormatInformation, error) {
episodes, err := utils.ExtractEpisodes(url, crunchyroll.JP, crunchyroll.LOCALE(downloadAudioFlag)) episodes, err := utils.ExtractEpisodes(url)
if err != nil { if err != nil {
return nil, err return nil, err
} }
japanese := episodes[0]
custom := episodes[1]
sort.Sort(crunchyUtils.EpisodesByNumber(japanese))
sort.Sort(crunchyUtils.EpisodesByNumber(custom))
var errMessages []string
var final []*crunchyroll.Episode
if len(japanese) == 0 || len(japanese) == len(custom) {
final = custom
} else {
for _, jp := range japanese {
before := len(final)
for _, episode := range custom {
if jp.SeasonNumber == episode.SeasonNumber && jp.EpisodeNumber == episode.EpisodeNumber {
final = append(final, episode)
}
}
if before == len(final) {
errMessages = append(errMessages, fmt.Sprintf("%s has no %s audio, using %s as fallback", jp.Title, crunchyroll.LOCALE(downloadAudioFlag), crunchyroll.JP))
final = append(final, jp)
}
}
}
if len(errMessages) > 10 {
for _, msg := range errMessages[:10] {
utils.Log.SetProcess(msg)
}
utils.Log.SetProcess("... and %d more", len(errMessages)-10)
} else {
for _, msg := range errMessages {
utils.Log.SetProcess(msg)
}
}
var infoFormat [][]utils.FormatInformation var infoFormat [][]utils.FormatInformation
for _, season := range crunchyUtils.SortEpisodesBySeason(final) { for _, season := range crunchyUtils.SortEpisodesBySeason(episodes[0]) {
tmpFormatInformation := make([]utils.FormatInformation, 0) tmpFormatInformation := make([]utils.FormatInformation, 0)
for _, episode := range season { for _, episode := range season {
format, err := episode.GetFormat(downloadResolutionFlag, crunchyroll.LOCALE(downloadSubtitleFlag), true) format, err := episode.GetFormat(downloadResolutionFlag, crunchyroll.LOCALE(downloadSubtitleFlag), true)

View file

@ -28,7 +28,6 @@ func ExtractEpisodes(url string, locales ...crunchyroll.LOCALE) ([][]*crunchyrol
url = url[:lastOpen] url = url[:lastOpen]
} }
final := make([][]*crunchyroll.Episode, len(locales))
episodes, err := Crunchy.ExtractEpisodesFromUrl(url, locales...) episodes, err := Crunchy.ExtractEpisodesFromUrl(url, locales...)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to get episodes: %v", err) return nil, fmt.Errorf("failed to get episodes: %v", err)
@ -82,6 +81,9 @@ func ExtractEpisodes(url string, locales ...crunchyroll.LOCALE) ([][]*crunchyrol
} }
} }
var final [][]*crunchyroll.Episode
if len(locales) > 0 {
final = make([][]*crunchyroll.Episode, len(locales))
localeSorted, err := utils.SortEpisodesByAudio(episodes) localeSorted, err := utils.SortEpisodesByAudio(episodes)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to get audio locale: %v", err) return nil, fmt.Errorf("failed to get audio locale: %v", err)
@ -89,6 +91,9 @@ func ExtractEpisodes(url string, locales ...crunchyroll.LOCALE) ([][]*crunchyrol
for i, locale := range locales { for i, locale := range locales {
final[i] = append(final[i], localeSorted[locale]...) final[i] = append(final[i], localeSorted[locale]...)
} }
} else {
final = [][]*crunchyroll.Episode{episodes}
}
return final, nil return final, nil
} }