-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAiringDate.go
72 lines (58 loc) · 1.61 KB
/
AiringDate.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
package shoboi
import (
"math"
"net/http"
"strconv"
"time"
)
// AiringDate ...
type AiringDate struct {
Start string `json:"startDate"`
End string `json:"endDate"`
ProgramID string `json:"programId"`
ChannelID string `json:"channelId"`
}
// GetAiringDate ...
func GetAiringDate(animeID string, episodeNumber int) *AiringDate {
programList := &ProgramList{}
resp, err := get("http://cal.syoboi.jp/json.php?Req=ProgramByCount&TID=" + animeID + "&Count=" + strconv.Itoa(episodeNumber))
if err != nil {
return nil
}
if resp.StatusCode() != http.StatusOK {
return nil
}
err = resp.Unmarshal(programList)
if err != nil {
return nil
}
if programList.Programs == nil {
return nil
}
// Find earliest airing date
var endTimeErr error
earliestPID := ""
earliestChannelID := ""
earliestStartTime := int64(math.MaxInt64)
earliestEndTime := int64(math.MaxInt64)
for pid, airingDateInfo := range programList.Programs {
startTime, err := strconv.ParseInt(airingDateInfo.StartTime, 10, 64)
if err == nil && startTime != 0 && startTime < earliestStartTime {
earliestPID = pid
earliestChannelID = airingDateInfo.ChannelID
earliestStartTime = startTime
earliestEndTime, endTimeErr = strconv.ParseInt(airingDateInfo.EndTime, 10, 64)
}
}
airingDate := &AiringDate{
ProgramID: earliestPID,
ChannelID: earliestChannelID,
Start: time.Unix(earliestStartTime, 0).In(time.UTC).Format(time.RFC3339),
}
if endTimeErr != nil || earliestEndTime == 0 {
airingDate.End = ""
} else {
airingDate.End = time.Unix(earliestEndTime, 0).In(time.UTC).Format(time.RFC3339)
}
return airingDate
}