-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
89 lines (77 loc) · 1.73 KB
/
main.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
package main
import (
"encoding/xml"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
)
// Prog は各番組の情報を格納する
type Prog struct {
XMLName xml.Name `xml:"prog"`
Title string `xml:"title"`
Pfm string `xml:"pfm"`
}
// Radiko はRadiko週間番組表(局ごと)を格納する
type Radiko struct {
XMLName xml.Name `xml:"radiko"`
Stations *struct {
Station *struct {
Name string `xml:"name"`
Days []*struct {
Date string `xml:"date"`
Programs []*Prog `xml:"prog"`
} `xml:"progs"`
} `xml:"station"`
} `xml:"stations"`
}
func main() {
os.Exit(run())
}
func run() (exitCode int) {
exitCode = 0
client := http.Client{}
resp, err := client.Get("https://radiko.jp/v3/program/station/weekly/TBS.xml")
if err != nil {
log.Printf("alert: Radiko番組表を読み込めませんでした:%s", err)
exitCode = 1
return
}
defer resp.Body.Close()
var table Radiko
if err := xml.NewDecoder(resp.Body).Decode(&table); err != nil {
log.Printf("alert: xmlデータを構造体に読み込めませんでした:%v", err)
exitCode = 1
return
}
// 今日の該当番組のゲスト名抽出
ts := (time.Now()).Format("20060102")
guest := ""
for _, day := range table.Stations.Station.Days {
if strings.Contains(day.Date, ts) {
for _, prog := range day.Programs {
if strings.Contains(prog.Title, "伊集院光とらじおと") {
guest = getGuest(prog)
if guest != "" {
break
}
}
}
break
}
}
fmt.Printf("%s", guest)
return
}
func getGuest(prog *Prog) (gst string) {
pfm := prog.Pfm
gsl := strings.Split(pfm, "ゲスト:")
if len(gsl) == 1 {
return
}
gst = gsl[len(gsl)-1]
gst = strings.ReplaceAll(gst, "/", "/")
return
}