mirror of
https://github.com/crunchy-labs/crunchy-cli.git
synced 2026-01-21 04:02:00 -06:00
Fixed import cycle
This commit is contained in:
parent
a28002445a
commit
5921c132e3
3 changed files with 49 additions and 8 deletions
|
|
@ -130,12 +130,12 @@ func (l *logger) EndProgress(successful bool, message string) {
|
|||
l.ErrLog.Print(message)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
} else if l.progress != nil {
|
||||
l.progress <- progress{
|
||||
status: successful,
|
||||
message: message,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (l *logger) EndProgressf(successful bool, message string, a ...interface{}) {
|
||||
|
|
|
|||
|
|
@ -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<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
|
||||
}
|
||||
|
|
|
|||
12
utils.go
12
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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue