From e8f0292e35884519ded266507fb5f58548607086 Mon Sep 17 00:00:00 2001 From: CptPie <23438606+CptPie@users.noreply.github.com> Date: Mon, 25 Jan 2021 02:22:12 +0100 Subject: [PATCH 1/2] Added a new config field "MaxMultEpLength" --- admin.go | 1 + server.go | 2 ++ 2 files changed, 3 insertions(+) diff --git a/admin.go b/admin.go index d377551..7f47de5 100644 --- a/admin.go +++ b/admin.go @@ -385,6 +385,7 @@ func (s *Server) handlerAdminConfig(w http.ResponseWriter, r *http.Request) { configValue{Key: ConfigMaxDescriptionLength, Default: DefaultMaxDescriptionLength, Type: ConfigInt}, configValue{Key: ConfigMaxLinkLength, Default: DefaultMaxLinkLength, Type: ConfigInt}, configValue{Key: ConfigMaxRemarksLength, Default: DefaultMaxRemarksLength, Type: ConfigInt}, + configValue{Key: ConfigMaxMultEpLength, Default: DefaultMaxMultEpLength, Type: ConfigInt}, configValue{Key: ConfigUnlimitedVotes, Default: DefaultUnlimitedVotes, Type: ConfigBool}, }, diff --git a/server.go b/server.go index 0c40024..b9753d7 100644 --- a/server.go +++ b/server.go @@ -37,6 +37,7 @@ const ( DefaultMaxDescriptionLength int = 1000 DefaultMaxLinkLength int = 500 // length of all links combined DefaultMaxRemarksLength int = 200 + DefaultMaxMultEpLength int = 120 // length of multiple episode entries in minutes ) // configuration keys @@ -60,6 +61,7 @@ const ( ConfigMaxDescriptionLength string = "MaxDescriptionLength" ConfigMaxLinkLength string = "MaxLinkLength" ConfigMaxRemarksLength string = "MaxRemarksLength" + ConfigMaxMultEpLength string = "ConfigMaxMultEpLength" ) type Options struct { From f28eb0891220c3161150a7de34baf95f74fe5c4d Mon Sep 17 00:00:00 2001 From: CptPie <23438606+CptPie@users.noreply.github.com> Date: Mon, 25 Jan 2021 03:38:32 +0100 Subject: [PATCH 2/2] Implemented max duration for jikan --- dataimporter.go | 25 +++++++++++++++++++++++++ server.go | 12 +++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/dataimporter.go b/dataimporter.go index 30dc5c0..03204fe 100644 --- a/dataimporter.go +++ b/dataimporter.go @@ -10,6 +10,8 @@ import ( "io/ioutil" "net/http" "os" + "regexp" + "strconv" "strings" "github.com/nfnt/resize" @@ -39,6 +41,7 @@ type jikan struct { excludedTypes []string resp *map[string]interface{} maxEpisodes int + maxDuration int } func getMovieData(api dataapi) ([]string, error) { @@ -229,6 +232,8 @@ func (t *tmdb) getTags() (string, error) { return strings.Join(tags, ","), nil } +var re_duration = regexp.MustCompile(`([0-9]{1,3}) min`) + func (j *jikan) requestResults() error { resp, err := http.Get("https://api.jikan.moe/v3/anime/" + j.id) if err != nil || resp.StatusCode != 200 { @@ -263,6 +268,26 @@ func (j *jikan) requestResults() error { return fmt.Errorf("The episode count of this anime has not been published yet. Therefore this anime can not be added.") } + if dat["duration"] != nil && dat["duration"] != "Unknown" && j.maxDuration >= 0 { + match := re_duration.FindStringSubmatch(dat["duration"].(string)) + if len(match) < 2 { + j.l.Error("Could not detect episode duration.") + return fmt.Errorf("The episode duration of this anime has not been published or has an unexpected format. Therefore this anime can not be added.") + } + duration, err := strconv.Atoi(match[1]) + + if err != nil { + j.l.Error("Could not convert duration %v to int", match[1]) + return fmt.Errorf("The episode duration of this anime has not been published or has an unexpected format. Therefore this anime can not be added.") + } + + // this looks stupid but it works lul + if (duration * int(dat["episodes"].(float64))) > j.maxDuration { + j.l.Error("Duration of the anime %s is too long: %d", dat["title"].(string), (duration * int(dat["episodes"].(float64)))) + return fmt.Errorf("The duration of this series (episode duration * episodes) is longer than the maximum duration defined by the admin. Therefore this anime can not be added.") + } + } + j.resp = &dat return nil } diff --git a/server.go b/server.go index b9753d7..a09451b 100644 --- a/server.go +++ b/server.go @@ -923,7 +923,17 @@ func (s *Server) handleJikan(data *dataAddMovie, w http.ResponseWriter, r *http. return nil, fmt.Errorf("Error while retriving config value 'JikanMaxEpisodes':\n %v", err) } - sourceAPI := jikan{id: id, l: s.l, excludedTypes: bannedTypes, maxEpisodes: maxEpisodes} + maxDuration, err := s.data.GetCfgInt(ConfigMaxMultEpLength, DefaultMaxMultEpLength) + + if err != nil { + s.doError( + http.StatusInternalServerError, + "something went wrong :C", + w, r) + return nil, fmt.Errorf("Error while retriving config value 'MaxMultEpLength':\n %v", err) + } + + sourceAPI := jikan{id: id, l: s.l, excludedTypes: bannedTypes, maxEpisodes: maxEpisodes, maxDuration: maxDuration} // Request data from API results, err := getMovieData(&sourceAPI)