diff --git a/cmd/crunchyroll-go/cmd/utils.go b/cmd/crunchyroll-go/cmd/utils.go index 2afe2f8..37f8524 100644 --- a/cmd/crunchyroll-go/cmd/utils.go +++ b/cmd/crunchyroll-go/cmd/utils.go @@ -130,11 +130,11 @@ func (l *logger) EndProgress(successful bool, message string) { l.ErrLog.Print(message) } return - } - - l.progress <- progress{ - status: successful, - message: message, + } else if l.progress != nil { + l.progress <- progress{ + status: successful, + message: message, + } } } diff --git a/crunchyroll.go b/crunchyroll.go index 70ec77e..d8e74e9 100644 --- a/crunchyroll.go +++ b/crunchyroll.go @@ -5,10 +5,10 @@ import ( "encoding/json" "errors" "fmt" - "github.com/ByteDream/crunchyroll-go/utils" "io/ioutil" "net/http" "net/url" + "regexp" "strings" ) @@ -283,7 +283,7 @@ func (c *Crunchyroll) Search(query string, limit uint) (s []*Series, m []*Movie, // FindVideo fins a Video (Season or Movie) by a crunchyroll link // e.g. https://www.crunchyroll.com/darling-in-the-franxx func (c *Crunchyroll) FindVideo(seriesUrl string) (Video, error) { - if series, ok := utils.MatchVideo(seriesUrl); ok { + if series, ok := MatchVideo(seriesUrl); ok { if !ok { return nil, errors.New("series could not be found") } @@ -307,7 +307,7 @@ func (c *Crunchyroll) FindVideo(seriesUrl string) (Video, error) { // FindEpisode finds an episode by its crunchyroll link // e.g. https://www.crunchyroll.com/darling-in-the-franxx/episode-1-alone-and-lonesome-759575 func (c *Crunchyroll) FindEpisode(url string) ([]*Episode, error) { - if series, title, ok := utils.MatchEpisode(url); ok { + if series, title, ok := MatchEpisode(url); ok { title = strings.TrimSuffix(title, "-") video, err := c.FindVideo(fmt.Sprintf("https://www.crunchyroll.com/%s", series)) if err != nil { @@ -335,3 +335,32 @@ func (c *Crunchyroll) FindEpisode(url string) ([]*Episode, error) { return nil, errors.New("invalid url") } + +// MatchVideo tries to extract the crunchyroll series / movie name out of the given url +func MatchVideo(url string) (seriesName string, ok bool) { + pattern := regexp.MustCompile(`(?m)^https?://(www\.)?crunchyroll\.com(/\w{2}(-\w{2})?)?/(?P[^/]+)/?$`) + if urlMatch := pattern.FindAllStringSubmatch(url, -1); len(urlMatch) != 0 { + groups := regexGroups(urlMatch, pattern.SubexpNames()...) + seriesName = groups["series"] + + if seriesName != "" { + ok = true + } + } + return +} + +// MatchEpisode tries to extract the crunchyroll series name and title out of the given url +func MatchEpisode(url string) (seriesName, title string, ok bool) { + pattern := regexp.MustCompile(`(?m)^https?://(www\.)?crunchyroll\.com(/\w{2}(-\w{2})?)?/(?P[^/]+)/episode-\d+-(?P\D+).*`) + if urlMatch := pattern.FindAllStringSubmatch(url, -1); len(urlMatch) != 0 { + groups := regexGroups(urlMatch, pattern.SubexpNames()...) + seriesName = groups["series"] + title = strings.TrimSuffix(groups["title"], "-") + + if seriesName != "" && title != "" { + ok = true + } + } + return +} diff --git a/utils.go b/utils.go index 142fdd5..5983a60 100644 --- a/utils.go +++ b/utils.go @@ -43,3 +43,15 @@ func pkcs5UnPadding(origData []byte) []byte { unPadding := int(origData[length-1]) return origData[:(length - unPadding)] } + +func regexGroups(parsed [][]string, subexpNames ...string) map[string]string { + groups := map[string]string{} + for _, match := range parsed { + for i, content := range match { + if subexpName := subexpNames[i]; subexpName != "" { + groups[subexpName] = content + } + } + } + return groups +}