2018-01-18 08:42:43 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/csv"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-01-20 00:43:28 +01:00
|
|
|
//"gopkg.in/cheggaaa/pb.v1"
|
2018-01-18 08:42:43 +01:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2018-01-19 15:45:43 +01:00
|
|
|
"github.com/agext/levenshtein"
|
2018-01-18 08:42:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
//Anime is AniDB structure
|
|
|
|
type Anime struct {
|
|
|
|
ID int
|
|
|
|
Type string
|
|
|
|
Episodes int
|
|
|
|
Title string
|
|
|
|
StartDate time.Time
|
|
|
|
EndDate time.Time
|
|
|
|
}
|
|
|
|
//Result contains the results from the meikan search
|
|
|
|
type Result struct {
|
|
|
|
Total int `json:"total"`
|
|
|
|
Anime []AnimeRes `json:"data"`
|
|
|
|
}
|
|
|
|
//AnimeRes is how the search results from meikan are built up in the JSON
|
|
|
|
type AnimeRes struct {
|
|
|
|
EndDate string `json:"end_date"`
|
|
|
|
Episodes int `json:"episodes"`
|
|
|
|
ID int `json:"id"`
|
|
|
|
Rating string `json:"rating"`
|
|
|
|
StartDate string `json:"start_date"`
|
|
|
|
State string `json:"state"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type CompareAni struct {
|
2018-01-19 15:45:43 +01:00
|
|
|
AnidbTitle string
|
|
|
|
MeikanTitle string
|
2018-01-18 08:42:43 +01:00
|
|
|
AnidbID int
|
|
|
|
MeikanID int
|
|
|
|
}
|
|
|
|
//Animes is a collection of animes
|
2018-01-20 00:43:28 +01:00
|
|
|
type Animes []MatchedAnime
|
2018-01-18 08:42:43 +01:00
|
|
|
|
|
|
|
type resAnimes []CompareAni
|
|
|
|
|
2018-01-20 00:43:28 +01:00
|
|
|
type MatchedAnime struct {
|
|
|
|
MeikanTitle string
|
|
|
|
AnidbTitle string
|
|
|
|
MeikanID int
|
|
|
|
AnidbID int
|
|
|
|
Score int
|
|
|
|
}
|
|
|
|
|
2018-01-18 08:42:43 +01:00
|
|
|
func check(e error) {
|
|
|
|
if e != nil {
|
|
|
|
panic(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-20 00:43:28 +01:00
|
|
|
func parseCsv (file string, c chan MatchedAnime) {
|
2018-01-18 08:42:43 +01:00
|
|
|
anidbLayout := "02.01.2006"
|
|
|
|
dat, err := ioutil.ReadFile(file)
|
|
|
|
check(err)
|
|
|
|
r := csv.NewReader(strings.NewReader(string(dat)))
|
|
|
|
_, er := r.Read()
|
2018-01-20 00:43:28 +01:00
|
|
|
check(er)
|
2018-01-18 08:42:43 +01:00
|
|
|
for {
|
|
|
|
record, err := r.Read()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
2018-01-20 00:43:28 +01:00
|
|
|
|
2018-01-18 08:42:43 +01:00
|
|
|
}
|
|
|
|
EpRegex := regexp.MustCompile(`.*, (\d*) .*`)
|
|
|
|
|
|
|
|
ep := strings.Split(record[11], ",")
|
|
|
|
eps := 1
|
|
|
|
|
|
|
|
episodes, err := strconv.Atoi(EpRegex.ReplaceAllString(record[11], "$1"))
|
|
|
|
if err == nil {
|
|
|
|
eps = episodes
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
a := Anime{
|
|
|
|
StartDate: startDate,
|
|
|
|
EndDate: endDate,
|
|
|
|
ID: id,
|
|
|
|
Type: strings.TrimSpace(ep[0]),
|
|
|
|
Episodes: eps,
|
|
|
|
Title: strings.Replace(record[3], "Anime: ", "", 1),
|
|
|
|
}
|
|
|
|
if a.Title == "ERROR" {
|
|
|
|
continue
|
|
|
|
}
|
2018-01-20 00:43:28 +01:00
|
|
|
go checkResults(a, c)
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
2018-01-18 08:42:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func lessType (Type string) (string) {
|
|
|
|
switch (Type) {
|
|
|
|
case "TV", "ONA":
|
|
|
|
return "Shows"
|
|
|
|
case "OVA", "Specials":
|
|
|
|
return "Extra"
|
|
|
|
}
|
|
|
|
return Type
|
|
|
|
}
|
|
|
|
|
2018-01-20 00:43:28 +01:00
|
|
|
func checkResults(anime Anime, c chan MatchedAnime) {
|
|
|
|
var highest int
|
2018-01-18 08:42:43 +01:00
|
|
|
//var re = regexp.MustCompile(`\s\(\d*\)`)
|
2018-01-20 00:43:28 +01:00
|
|
|
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)
|
|
|
|
check(err)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
var result Result
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
check(err)
|
|
|
|
er := json.NewDecoder(bytes.NewReader(body)).Decode(&result)
|
|
|
|
if er != nil {
|
|
|
|
fmt.Println(string(body))
|
|
|
|
}
|
|
|
|
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"
|
|
|
|
}
|
2018-01-18 14:22:37 +01:00
|
|
|
var hI int
|
2018-01-20 00:43:28 +01:00
|
|
|
for i := 0;i<len(result.Anime); i++ {
|
2018-01-18 08:42:43 +01:00
|
|
|
var total int
|
2018-01-20 00:43:28 +01:00
|
|
|
meikan := result.Anime[i]
|
2018-01-18 08:42:43 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
if meikan.Type == anime.Type {
|
|
|
|
total += 25
|
|
|
|
} else {
|
|
|
|
MType := lessType(meikan.Type)
|
|
|
|
AType := lessType(anime.Type)
|
|
|
|
if MType == AType {
|
|
|
|
total += 12
|
|
|
|
}
|
|
|
|
}
|
2018-01-19 15:45:43 +01:00
|
|
|
score := levenshtein.Match(meikan.Title, anime.Title, nil)
|
|
|
|
//fmt.Print(score)
|
|
|
|
if score > 0.95 {
|
2018-01-18 08:42:43 +01:00
|
|
|
total += 50
|
|
|
|
}
|
|
|
|
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 += 50
|
|
|
|
}
|
|
|
|
if total > highest {
|
|
|
|
highest = total
|
|
|
|
hI = i
|
|
|
|
}
|
|
|
|
}
|
2018-01-18 14:22:37 +01:00
|
|
|
//return highest, hI
|
2018-01-20 00:43:28 +01:00
|
|
|
if len(result.Anime) != 0 {
|
|
|
|
c <- MatchedAnime{
|
|
|
|
MeikanTitle: result.Anime[hI].Title,
|
|
|
|
AnidbTitle: anime.Title,
|
|
|
|
MeikanID: result.Anime[hI].ID,
|
|
|
|
AnidbID: anime.ID,
|
|
|
|
Score: highest,
|
|
|
|
}
|
|
|
|
}
|
2018-01-18 08:42:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2018-01-20 00:43:28 +01:00
|
|
|
c := make(chan MatchedAnime)
|
|
|
|
//var resultCompare resAnimes
|
|
|
|
var animes Animes
|
|
|
|
parseCsv("/home/trac/coding/compareID/1.-Main-data.csv", c)
|
|
|
|
//count := len(animes)
|
|
|
|
//bar := pb.StartNew(count - 1)
|
|
|
|
test := 0
|
|
|
|
for {
|
|
|
|
/*anime := animes[i]
|
2018-01-18 14:22:37 +01:00
|
|
|
highest, hI := <-c, <-c
|
|
|
|
//highest, hI := checkResults(result, anime, 0)
|
2018-01-18 08:42:43 +01:00
|
|
|
if highest < 100 {
|
|
|
|
check(err)
|
2018-01-19 15:45:43 +01:00
|
|
|
if len(result.Anime) > 0 {
|
|
|
|
fmt.Printf("Found no match for %s (Best result: %s)\n", anime.Title, result.Anime[hI].Title)
|
|
|
|
//fmt.Println(highest)
|
|
|
|
//fmt.Printf("Title: %s, Episodes: %d, Type: %s, ID: %d, StartDate: %s, EndDate: %s\n", anime.Title, anime.Episodes, anime.Type, anime.ID, anime.StartDate, anime.EndDate)
|
|
|
|
//fmt.Printf("Title: %s, Episodes: %d, Type: %s, ID: %d, StartDate: %s, EndDate: %s\n", result.Anime[hI].Title, result.Anime[hI].Episodes, result.Anime[hI].Type, result.Anime[hI].ID, result.Anime[hI].StartDate, result.Anime[hI].EndDate)
|
2018-01-18 08:42:43 +01:00
|
|
|
} else {
|
2018-01-19 15:45:43 +01:00
|
|
|
fmt.Printf("Found no match for %s\n", anime.Title)
|
2018-01-18 08:42:43 +01:00
|
|
|
fmt.Println("No search results returned")
|
2018-01-19 15:45:43 +01:00
|
|
|
}
|
2018-01-18 14:22:37 +01:00
|
|
|
notMatched++
|
2018-01-18 08:42:43 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
check(err)
|
|
|
|
/*fmt.Println(highest, hI)
|
2018-01-20 00:43:28 +01:00
|
|
|
fmt.Printf("Matched %s -> %s\n", anime.Title, result.Anime[hI].Title)
|
2018-01-18 08:42:43 +01:00
|
|
|
//fmt.Printf("Title: %s, Episodes: %d, Type: %s, ID: %d, StartDate: %s, EndDate: %s\n", result.Anime[hI].Title, result.Anime[hI].Episodes, result.Anime[hI].Type, result.Anime[hI].ID, result.Anime[hI].StartDate, result.Anime[hI].EndDate)
|
|
|
|
a := CompareAni{
|
2018-01-19 15:45:43 +01:00
|
|
|
AnidbTitle: anime.Title,
|
|
|
|
MeikanTitle: result.Anime[hI].Title,
|
2018-01-18 08:42:43 +01:00
|
|
|
AnidbID: anime.ID,
|
|
|
|
MeikanID: result.Anime[hI].ID,
|
|
|
|
}
|
2018-01-18 14:22:37 +01:00
|
|
|
matched++
|
2018-01-18 08:42:43 +01:00
|
|
|
resultCompare = append(resultCompare, a)
|
|
|
|
}
|
|
|
|
bar.Increment()
|
|
|
|
//fmt.Printf("ID: %d, Type: %s, Episodes: %d, Title: %s\n", a.Id, a.Type, a.Episodes, a.Title)
|
2018-01-20 00:43:28 +01:00
|
|
|
*/
|
|
|
|
select {
|
|
|
|
case m := <- c:
|
|
|
|
animes = append(animes, m)
|
|
|
|
test = 0
|
|
|
|
default:
|
|
|
|
time.Sleep(1000 * time.Millisecond)
|
|
|
|
if (test > 2) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
test = test + 1
|
|
|
|
}
|
2018-01-18 08:42:43 +01:00
|
|
|
}
|
2018-01-20 00:43:28 +01:00
|
|
|
resJson, err := json.MarshalIndent(animes, "", "\t")
|
2018-01-18 08:42:43 +01:00
|
|
|
check(err)
|
|
|
|
jsonfile := []byte(resJson)
|
|
|
|
err = ioutil.WriteFile("./result.json", jsonfile, 0644)
|
2018-01-20 00:43:28 +01:00
|
|
|
//fmt.Printf("Matched: %d\nNot Matched: %d\nTotal: %d", matched, notMatched, matched + notMatched)
|
2018-01-18 08:42:43 +01:00
|
|
|
check(err)
|
|
|
|
}
|