package main
import (
"encoding/json"
"fmt"
"github.com/garyburd/redigo/redis"
"net/http"
"net/url"
"strings"
"sync"
"time"
)
type Slack struct {
Channel string `json:"channel"`
IconEmoji string `json:"icon_emoji"`
IconUrl string `json:"icon_url"`
Text string `json:"text"`
Username string `json:"username"`
}
type NihonHikikomoriKyokai struct {
List List `json:"list"`
}
type List struct {
G1 []Program `json:"g1"`
G2 []Program `json:"g2"`
E1 []Program `json:"e1"`
E2 []Program `json:"e2"`
E3 []Program `json:"e3"`
E4 []Program `json:"e4"`
S1 []Program `json:"s1"`
S2 []Program `json:"s2"`
S3 []Program `json:"s3"`
S4 []Program `json:"s4"`
R1 []Program `json:"r1"`
R2 []Program `json:"r2"`
R3 []Program `json:"r3"`
N1 []Program `json:"n1"`
N2 []Program `json:"n2"`
N3 []Program `json:"n3"`
}
type Program struct {
Area struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"area"`
EndTime string `json:"end_time"`
EventID string `json:"event_id"`
Genres []string `json:"genres"`
ID string `json:"id"`
Service struct {
ID string `json:"id"`
LogoL struct {
Height string `json:"height"`
URL string `json:"url"`
Width string `json:"width"`
} `json:"logo_l"`
LogoM struct {
Height string `json:"height"`
URL string `json:"url"`
Width string `json:"width"`
} `json:"logo_m"`
LogoS struct {
Height string `json:"height"`
URL string `json:"url"`
Width string `json:"width"`
} `json:"logo_s"`
Name string `json:"name"`
} `json:"service"`
StartTime string `json:"start_time"`
Subtitle string `json:"subtitle"`
Title string `json:"title"`
}
const (
ApiKey = "{YOUR_NHKWEBAPI_TOKEN}"
)
func main() {
genreCodes := []string{
"0400",
"0401",
"0402",
"0403",
"0404",
"0405",
"0406",
"0407",
"0408",
"0409",
"0410",
}
for i := 0; i < len(genreCodes); i++ {
endpoint := fmt.Sprintf("http://api.nhk.or.jp/v1/pg/genre/130/tv/%s/%s.json?key=%s", genreCodes[i], time.Now().AddDate(0, 0, 1).Format("2006-01-02"), ApiKey)
resp, _ := http.Get(endpoint)
defer resp.Body.Close()
var nhk NihonHikikomoriKyokai
decoder := json.NewDecoder(resp.Body)
decoder.Decode(&nhk)
c, _ := redis.Dial("tcp", ":6379")
defer c.Close()
giveMeRevolution(nhk.List.G1, c)
giveMeRevolution(nhk.List.E1, c)
giveMeRevolution(nhk.List.S1, c)
giveMeRevolution(nhk.List.S3, c)
}
}
func giveMeRevolution(programs []Program, c redis.Conn) {
var wg sync.WaitGroup
for _, program := range programs {
if false == isChecked("nhk_"+program.ID, c) {
keyword := "アニメ"
if strings.Contains(program.Title, keyword) || strings.Contains(program.Subtitle, keyword) {
var s Slack
format := "2006-01-02T15:04:05+09:00"
startTime, _ := time.Parse(format, program.StartTime)
endTime, _ := time.Parse(format, program.EndTime)
s.Text = fmt.Sprintf(
"(\\( ⁰⊖⁰)/) タッタラタッタ、タッタ!\n```%s```\n```%s```\n```%s ~ %s```",
program.Title,
program.Subtitle,
startTime.Format("2006-01-02(Mon) 15:04:05"),
endTime.Format("2006-01-02(Mon) 15:04:05"),
)
j, _ := json.Marshal(s)
wg.Add(1)
go func(j []byte) {
defer wg.Done()
http.PostForm(
"https://hooks.slack.com/services/{YOUR_WEBHOOK_URL}",
url.Values{
"payload": {
string(j),
},
},
)
fmt.Printf("POSTED!! ID:%s Title:%s\n", program.ID, program.Title)
}(j)
} else {
fmt.Printf("new program was found. but not contains keyword. ID:%s Title:%s\n", program.ID, program.Title)
}
} else {
fmt.Printf("already checked. ID:%s Title:%s\n", program.ID, program.Title)
}
}
wg.Wait()
}
func isChecked(key string, c redis.Conn) bool {
//return false
checked, err := redis.Bool(c.Do("GET", key))
if err != nil || checked == false {
c.Do("EXPIRE", key, 60*60*24)
c.Do("SET", key, true)
return false
} else {
return true
}
}