Skip to content

Commit

Permalink
Merge pull request #102 from CptPie/timeLimit#88
Browse files Browse the repository at this point in the history
Implementation for multi episode time limits (#88)
  • Loading branch information
zorchenhimer authored Feb 2, 2021
2 parents 2457302 + afbdc0e commit e1a7511
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,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},

Expand Down
25 changes: 25 additions & 0 deletions dataimporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"io/ioutil"
"net/http"
"os"
"regexp"
"strconv"
"strings"

"github.com/nfnt/resize"
Expand Down Expand Up @@ -39,6 +41,7 @@ type jikan struct {
excludedTypes []string
resp *map[string]interface{}
maxEpisodes int
maxDuration int
}

func getMovieData(api dataapi) ([]string, error) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
17 changes: 16 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const (
DefaultMaxLinkLength int = 500 // length of all links combined
DefaultMaxRemarksLength int = 200

DefaultMaxMultEpLength int = 120 // length of multiple episode entries in minutes

DefaultLocalSignupEnabled bool = true
DefaultTwitchOauthEnabled bool = false
DefaultTwitchOauthSignupEnabled bool = false
Expand All @@ -51,6 +53,7 @@ const (
DefaultPatreonOauthSignupEnabled bool = false
DefaultPatreonOauthClientID string = ""
DefaultPatreonOauthClientSecret string = ""

)

// configuration keys
Expand All @@ -75,6 +78,8 @@ const (
ConfigMaxLinkLength string = "MaxLinkLength"
ConfigMaxRemarksLength string = "MaxRemarksLength"

ConfigMaxMultEpLength string = "ConfigMaxMultEpLength"

ConfigLocalSignupEnabled string = "LocalSignupEnabled"
ConfigTwitchOauthEnabled string = "TwitchOauthEnabled"
ConfigTwitchOauthSignupEnabled string = "TwitchOauthSignupEnabled"
Expand Down Expand Up @@ -958,7 +963,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)
Expand Down

0 comments on commit e1a7511

Please sign in to comment.