Added utils for url parsing

This commit is contained in:
bytedream 2021-09-11 15:01:32 +02:00
parent 9ae41e78e2
commit 2f5d3ea07b
3 changed files with 51 additions and 29 deletions

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,15 +283,12 @@ 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) {
pattern := regexp.MustCompile(`(?m)^https?://(www\.)?crunchyroll\.com(/\w{2}(-\w{2})?)?/(?P<series>[^/]+)/?$`) if series, ok := utils.MatchVideo(seriesUrl); ok {
if urlMatch := pattern.FindAllStringSubmatch(seriesUrl, -1); len(urlMatch) != 0 {
groups := regexGroups(urlMatch, pattern.SubexpNames()...)
title, ok := groups["series"]
if !ok { if !ok {
return nil, errors.New("series could not be found") return nil, errors.New("series could not be found")
} }
s, m, err := c.Search(title, 1) s, m, err := c.Search(series, 1)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -310,16 +307,9 @@ 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) {
pattern := regexp.MustCompile(`(?m)^https?://(www\.)?crunchyroll\.com(/\w{2}(-\w{2})?)?/(?P<series>[^/]+)/episode-\d+-(?P<title>\D+).*`) if series, title, ok := utils.MatchEpisode(url); ok {
if urlMatch := pattern.FindAllStringSubmatch(url, -1); len(urlMatch) != 0 { title = strings.TrimSuffix(title, "-")
groups := regexGroups(urlMatch, pattern.SubexpNames()...) video, err := c.FindVideo(fmt.Sprintf("https://www.crunchyroll.com/%s", series))
var slugTitle string
var ok bool
if slugTitle, ok = groups["title"]; !ok {
return nil, errors.New("invalid url")
}
slugTitle = strings.TrimSuffix(slugTitle, "-")
video, err := c.FindVideo(fmt.Sprintf("https://www.crunchyroll.com/%s", groups["series"]))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -335,7 +325,7 @@ func (c *Crunchyroll) FindEpisode(url string) ([]*Episode, error) {
return nil, err return nil, err
} }
for _, episode := range episodes { for _, episode := range episodes {
if episode.SlugTitle == slugTitle { if episode.SlugTitle == title {
matchingEpisodes = append(matchingEpisodes, episode) matchingEpisodes = append(matchingEpisodes, episode)
} }
} }

View file

@ -43,15 +43,3 @@ 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
}

44
utils/match.go Normal file
View file

@ -0,0 +1,44 @@
package utils
import "regexp"
// 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 = groups["title"]
if seriesName != "" && title != "" {
ok = true
}
}
return
}
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
}