-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch.go
76 lines (65 loc) · 1.96 KB
/
Search.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
package shoboi
import (
"errors"
"net/http"
"strconv"
"strings"
)
// SearchResult ...
type SearchResult struct {
Titles map[string]*AnimeSearchResult `json:"Titles"`
}
// AnimeSearchResult ...
type AnimeSearchResult struct {
TID string `json:"TID"`
TitleJapanese string `json:"Title"`
ShortTitle string `json:"ShortTitle"`
TitleHiragana string `json:"TitleYomi"`
TitleEnglish string `json:"TitleEN"`
Cat string `json:"Cat"`
FirstChannel string `json:"FirstCh"`
FirstYear string `json:"FirstYear"`
FirstMonth string `json:"FirstMonth"`
FirstEndYear string `json:"FirstEndYear"`
FirstEndMonth string `json:"FirstEndMonth"`
TitleFlag string `json:"TitleFlag"`
Comment string `json:"Comment"`
Search int `json:"Search"`
Programs []struct {
PID string `json:"PID"`
TID string `json:"TID"`
StTime string `json:"StTime"`
EdTime string `json:"EdTime"`
ChID string `json:"ChID"`
StOffset string `json:"StOffset"`
Count interface{} `json:"Count"`
ProgComment string `json:"ProgComment"`
SubTitle string `json:"SubTitle"`
ChName string `json:"ChName"`
} `json:"Programs"`
}
// SearchAnime ...
func SearchAnime(title string) (anime *AnimeSearchResult, err error) {
title = strings.ToLower(title)
searchResult := &SearchResult{}
resp, err := get("http://cal.syoboi.jp/json?Req=TitleSearch&Search=" + title + "&Limit=15")
if err != nil {
return nil, err
}
if resp.StatusCode() != http.StatusOK {
return nil, errors.New("Invalid status code: " + strconv.Itoa(resp.StatusCode()))
}
err = resp.Unmarshal(searchResult)
if err != nil {
return nil, err
}
if searchResult.Titles == nil {
return nil, nil
}
for _, anime := range searchResult.Titles {
if strings.ToLower(anime.TitleJapanese) == title || strings.ToLower(anime.TitleEnglish) == title {
return anime, nil
}
}
return nil, nil
}