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