Cleaned up the code

This commit is contained in:
David Alasow 2018-01-21 21:54:12 +01:00
parent c7176ea47e
commit 0eb42f24ef
1 changed files with 70 additions and 81 deletions

151
main.go
View File

@ -43,18 +43,10 @@ type AnimeRes struct {
Type string `json:"type"`
}
type CompareAni struct {
AnidbTitle string
MeikanTitle string
AnidbID int
MeikanID int
}
//Animes is a collection of animes
type Animes []MatchedAnime
type resAnimes []CompareAni
//MatchedAnime contains all the neccessary information after matching two animes
type MatchedAnime struct {
MeikanTitle string
AnidbTitle string
@ -69,8 +61,19 @@ func check(e error) {
}
}
func fixAnidbDates(Dates []string) (startDate, endDate time.Time) {
if !strings.ContainsAny(Dates[0], "?") && Dates[0] != "" {
startDate, _ = time.Parse(`02.01.2006`, Dates[0])
}
if len(Dates) == 2 {
if !strings.ContainsAny(Dates[1], "?") && Dates[1] != "" {
endDate, _ = time.Parse(`02.01.2006`, Dates[1])
}
}
return startDate, endDate
}
func parseCsv(file string, c chan MatchedAnime) {
anidbLayout := "02.01.2006"
dat, err := ioutil.ReadFile(file)
check(err)
r := csv.NewReader(strings.NewReader(string(dat)))
@ -94,18 +97,7 @@ func parseCsv(file string, c chan MatchedAnime) {
id, err := strconv.Atoi(record[1])
check(err)
Date := strings.Split(record[12], " till ")
var startDate time.Time
if !strings.ContainsAny(Date[0], "?") && Date[0] != "" {
startDate, err = time.Parse(anidbLayout, Date[0])
check(err)
}
var endDate time.Time
if len(Date) == 2 {
if !strings.ContainsAny(Date[1], "?") && Date[1] != "" {
endDate, err = time.Parse(anidbLayout, Date[1])
check(err)
}
}
startDate, endDate := fixAnidbDates(Date)
a := Anime{
StartDate: startDate,
EndDate: endDate,
@ -129,59 +121,74 @@ func lessType(Type string) string {
return "Shows"
case "OVA", "Specials":
return "Extra"
case "TV Series":
return "TV"
case "TV Special":
return "Special"
case "Web":
return "ONA"
}
return Type
}
func fixType(Type string) string {
switch Type {
case "TV Series":
return "TV"
case "TV Special":
return "Special"
case "Web":
return "ONA"
}
return Type
}
func fixMeikanDates(Date, Date2 string) (date, date2 time.Time) {
if Date != "" {
date, _ = time.Parse(`2006-01-02`, Date)
}
if Date2 != "" {
date2, _ = time.Parse(`2006-01-02`, Date)
}
return date, date2
}
func subAnimeDates(Date, Date2 time.Time) (total int) {
if Date.Year() == 1 && Date2.Year() == 1 {
return 0
}
diff := Date.Sub(Date2)
if diff < 0 {
diff = diff * -1
}
if diff <= 2190*time.Hour {
return 50
}
return -150
}
func checkResults(anime Anime, c chan MatchedAnime) {
var highest int
fmt.Println(`Searching anime`, anime.ID)
var highest, hI int
var err error
anime.Title = strings.Replace(strings.Replace(anime.Title, `\`, `\\`, -1), `"`, `\"`, -1)
var search = bytes.NewBuffer([]byte(`{"title":"` + anime.Title + `", "show_r18": true}`))
resp, err := http.Post("https://api.meikan.moe/v1/anime?incl=start_date,end_date", "application/json", search)
if err != nil {
return
}
check(err)
fmt.Println(`Completed meikan search for`, anime.ID)
defer resp.Body.Close()
var result Result
body, err := ioutil.ReadAll(resp.Body)
check(err)
err = json.NewDecoder(bytes.NewReader(body)).Decode(&result)
if err != nil {
fmt.Println(string(body))
fmt.Println(anime.Title)
return
_ = json.NewDecoder(bytes.NewReader(body)).Decode(&result)
anime.Type = fixType(anime.Type)
if anime.Episodes < 1 {
anime.Episodes = 1
}
switch anime.Type {
case "TV Series":
anime.Type = "TV"
case "TV Special":
anime.Type = "Special"
if anime.Episodes < 1 {
anime.Episodes = 1
}
case "OVA":
if anime.Episodes < 1 {
anime.Episodes = 1
}
case "Web":
anime.Type = "ONA"
}
var hI int
for i := 0; i < len(result.Anime); i++ {
var total int
meikan := result.Anime[i]
var stDate time.Time
if meikan.StartDate != "" {
stDate, err = time.Parse("2006-01-02", meikan.StartDate)
check(err)
}
var enDate time.Time
if meikan.EndDate != "" {
enDate, err = time.Parse("2006-01-02", meikan.EndDate)
check(err)
}
stDate, enDate := fixMeikanDates(meikan.StartDate, meikan.EndDate)
if meikan.Type == anime.Type {
total += 25
} else {
@ -198,25 +205,9 @@ func checkResults(anime Anime, c chan MatchedAnime) {
if meikan.Episodes == anime.Episodes {
total += 25
}
if anime.StartDate.Year() != 1 && stDate.Year() != 1 {
diff := stDate.Sub(anime.StartDate)
if diff < 0 {
diff = diff * -1
}
if diff <= 2190*time.Hour {
total += 50
}
} else {
total = -150
}
if anime.Episodes != 1 && anime.EndDate.Year() != 1 && enDate.Year() != 1 {
diff := enDate.Sub(anime.EndDate)
if diff < 0 {
diff = diff * -1
}
if diff <= 2190*time.Hour {
total += 50
}
total += subAnimeDates(stDate, anime.StartDate)
if anime.Episodes != 1 {
total += subAnimeDates(enDate, anime.EndDate)
}
if total > highest {
highest = total
@ -249,11 +240,9 @@ MainLoop:
break MainLoop
}
}
fmt.Println(animes)
resJson, err := json.MarshalIndent(animes, "", "\t")
resJSON, err := json.MarshalIndent(animes, "", "\t")
check(err)
jsonfile := []byte(resJson)
jsonfile := []byte(resJSON)
err = ioutil.WriteFile("./result.json", jsonfile, 0644)
//fmt.Printf("Matched: %d\nNot Matched: %d\nTotal: %d", matched, notMatched, matched + notMatched)
check(err)
}