-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnime.go
124 lines (99 loc) · 2.88 KB
/
Anime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package shoboi
import (
"errors"
"net/http"
"regexp"
"sort"
"strconv"
"strings"
)
var episodeNameRegex = regexp.MustCompile(`\*(\d+)\*(.*)`)
// Anime ...
type Anime struct {
ID string `xml:"TID" json:"TID"`
CategoryID string `xml:"Cat" json:"Cat"`
Comment string `xml:"Comment" json:"Comment"`
FirstChannel string `xml:"FirstCh" json:"FirstCh"`
FirstEndMonth string `xml:"FirstEndMonth" json:"FirstEndMonth"`
FirstEndYear string `xml:"FirstEndYear" json:"FirstEndYear"`
FirstMonth string `xml:"FirstMonth" json:"FirstMonth"`
FirstYear string `xml:"FirstYear" json:"FirstYear"`
Keywords string `xml:"Keywords" json:"Keywords"`
ShortTitle string `xml:"ShortTitle" json:"ShortTitle"`
SubTitles string `xml:"SubTitles" json:"SubTitles"`
TitleJapanese string `xml:"Title" json:"Title"`
TitleEnglish string `xml:"TitleEN" json:"TitleEN"`
TitleHiragana string `xml:"TitleYomi" json:"TitleYomi"`
TitleFlag string `xml:"TitleFlag" json:"TitleFlag"`
UpdatedAt string `xml:"LastUpdate" json:"LastUpdate"`
UserPoint string `xml:"UserPoint" json:"UserPoint"`
UserPointRank string `xml:"UserPointRank" json:"UserPointRank"`
}
// GetAnime ...
func GetAnime(tid string) (*Anime, error) {
titleFull := &TitleFull{}
resp, err := get("http://cal.syoboi.jp/json.php?Req=TitleFull&TID=" + tid)
if resp.StatusCode() != http.StatusOK {
return nil, errors.New("Invalid status code: " + strconv.Itoa(resp.StatusCode()))
}
if err != nil {
return nil, err
}
err = resp.Unmarshal(titleFull)
if err != nil {
return nil, err
}
if titleFull.Titles == nil || titleFull.Titles[tid] == nil {
return nil, errors.New("Invalid data: Titles is nil")
}
return titleFull.Titles[tid], nil
}
// Episodes ...
func (anime *Anime) Episodes() []*Episode {
episodes := anime.NamedEpisodes()
episodeNumber := 1
index := 0
if len(episodes) > 0 {
episodeNumber = episodes[0].Number
}
for {
airingDate := GetAiringDate(anime.ID, episodeNumber)
if index >= len(episodes) {
if airingDate == nil {
break
}
episodes = append(episodes, &Episode{
anime: anime,
Number: episodeNumber,
})
}
episodes[index].AiringDate = airingDate
episodeNumber++
index++
}
sort.Slice(episodes, func(i, j int) bool {
return episodes[i].Number < episodes[j].Number
})
return episodes
}
// NamedEpisodes ...
func (anime *Anime) NamedEpisodes() []*Episode {
if anime.SubTitles == "" {
return nil
}
episodes := []*Episode{}
episodeNames := strings.Split(anime.SubTitles, "\r\n")
for _, episode := range episodeNames {
episode = strings.TrimSpace(episode)
matches := episodeNameRegex.FindStringSubmatch(episode)
if len(matches) > 2 {
episodeNumber, _ := strconv.Atoi(matches[1])
episodes = append(episodes, &Episode{
Number: episodeNumber,
TitleJapanese: matches[2],
anime: anime,
})
}
}
return episodes
}