Fixed import cycle

This commit is contained in:
bytedream 2021-09-13 09:36:09 +02:00
parent a28002445a
commit 5921c132e3
3 changed files with 49 additions and 8 deletions

View file

@ -130,12 +130,12 @@ func (l *logger) EndProgress(successful bool, message string) {
l.ErrLog.Print(message) l.ErrLog.Print(message)
} }
return return
} } else if l.progress != nil {
l.progress <- progress{ l.progress <- progress{
status: successful, status: successful,
message: message, message: message,
} }
}
} }
func (l *logger) EndProgressf(successful bool, message string, a ...interface{}) { func (l *logger) EndProgressf(successful bool, message string, a ...interface{}) {

View file

@ -5,10 +5,10 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/ByteDream/crunchyroll-go/utils"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"regexp"
"strings" "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 // FindVideo fins a Video (Season or Movie) by a crunchyroll link
// e.g. https://www.crunchyroll.com/darling-in-the-franxx // e.g. https://www.crunchyroll.com/darling-in-the-franxx
func (c *Crunchyroll) FindVideo(seriesUrl string) (Video, error) { func (c *Crunchyroll) FindVideo(seriesUrl string) (Video, error) {
if series, ok := utils.MatchVideo(seriesUrl); ok { if series, ok := MatchVideo(seriesUrl); ok {
if !ok { if !ok {
return nil, errors.New("series could not be found") 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 // FindEpisode finds an episode by its crunchyroll link
// e.g. https://www.crunchyroll.com/darling-in-the-franxx/episode-1-alone-and-lonesome-759575 // e.g. https://www.crunchyroll.com/darling-in-the-franxx/episode-1-alone-and-lonesome-759575
func (c *Crunchyroll) FindEpisode(url string) ([]*Episode, error) { 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, "-") title = strings.TrimSuffix(title, "-")
video, err := c.FindVideo(fmt.Sprintf("https://www.crunchyroll.com/%s", series)) video, err := c.FindVideo(fmt.Sprintf("https://www.crunchyroll.com/%s", series))
if err != nil { if err != nil {
@ -335,3 +335,32 @@ func (c *Crunchyroll) FindEpisode(url string) ([]*Episode, error) {
return nil, errors.New("invalid url") 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<series>[^/]+)/?$`)
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<series>[^/]+)/episode-\d+-(?P<title>\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
}

View file

@ -43,3 +43,15 @@ func pkcs5UnPadding(origData []byte) []byte {
unPadding := int(origData[length-1]) unPadding := int(origData[length-1])
return origData[:(length - unPadding)] 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
}