mtfosbot/router/google/youtube.go

152 lines
2.8 KiB
Go
Raw Permalink Normal View History

2018-09-15 15:09:41 +00:00
package google
import (
"encoding/xml"
"fmt"
"io/ioutil"
2018-12-06 02:19:28 +00:00
"log"
2018-09-15 15:09:41 +00:00
"strings"
"time"
"git.trj.tw/golang/mtfosbot/model"
lineapi "git.trj.tw/golang/mtfosbot/module/apis/line"
"git.trj.tw/golang/mtfosbot/module/context"
2018-12-06 02:19:28 +00:00
"git.trj.tw/golang/mtfosbot/module/utils"
2018-09-15 15:09:41 +00:00
)
type feed struct {
XMLName xml.Name `xml:"feed"`
Entry []entry `xml:"entry"`
}
type entry struct {
XMLName xml.Name `xml:"entry"`
Title string `xml:"title"`
ID string `xml:"id"`
Link link `xml:"link"`
Author []author `xml:"author"`
}
type link struct {
XMLName xml.Name `xml:"link"`
Href string `xml:"href,attr"`
}
type author struct {
XMLName xml.Name `xml:"author"`
Name string `xml:"name"`
URI string `xml:"uri"`
}
// VerifyWebhook -
func VerifyWebhook(c *context.Context) {
hubMode, ok := c.GetQuery("hub.mode")
if !ok {
c.DataFormat(nil)
return
}
challenge, ok := c.GetQuery("hub.challenge")
if !ok {
c.DataFormat(nil)
return
}
id, ok := c.GetQuery("id")
if !ok {
c.DataFormat(nil)
return
}
if hubMode == "subscribe" {
t := time.Now().Unix() + 86400
yt, err := model.GetYoutubeChannelWithID(id)
if err != nil {
c.ServerError(nil)
return
}
if yt == nil {
c.NotFound("channel not found")
return
}
err = yt.UpdateExpire(t)
if err != nil {
c.ServerError(nil)
}
}
c.String(200, challenge)
}
// GetNotifyWebhook -
func GetNotifyWebhook(c *context.Context) {
byteBody, err := ioutil.ReadAll(c.Request.Body)
defer c.Request.Body.Close()
if err != nil {
c.DataFormat(nil)
return
}
id, ok := c.GetQuery("id")
if !ok {
c.DataFormat(nil)
return
}
hook := &feed{}
err = xml.Unmarshal(byteBody, &hook)
if err != nil {
c.DataFormat(nil)
return
}
2018-12-06 02:19:28 +00:00
log.Println("hook data", utils.ToMap(hook))
2018-09-15 15:09:41 +00:00
if len(hook.Entry) == 0 {
c.Success(nil)
return
}
yt, err := model.GetYoutubeChannelWithID(id)
2018-12-06 02:19:28 +00:00
log.Println("youtube and error", yt, err)
2018-09-15 15:09:41 +00:00
if err != nil || yt == nil {
c.ServerError(nil)
return
}
if hook.Entry[0].ID == yt.LastVideo {
c.Success(nil)
return
}
err = yt.UpdateLastVideo(hook.Entry[0].ID)
if err != nil {
c.ServerError(nil)
return
}
2018-12-06 02:19:28 +00:00
err = yt.GetGroups()
if err != nil {
log.Println("get groups error ::::", err)
c.ServerError(nil)
return
}
2018-12-06 02:28:35 +00:00
log.Println("yt groups ::::: ", yt.Groups)
2018-12-06 02:19:28 +00:00
2018-09-15 15:09:41 +00:00
for _, v := range yt.Groups {
2018-12-06 03:14:21 +00:00
log.Println("group data :::: ", v, v.Notify, v.Name, v.ID)
2018-12-06 03:00:51 +00:00
if v.Notify == true {
2018-09-15 15:09:41 +00:00
str := v.Tmpl
2018-12-06 02:56:20 +00:00
log.Println("template :::: ", str)
2018-09-15 15:09:41 +00:00
if len(str) == 0 {
str = fmt.Sprintf("%s\n%s", hook.Entry[0].Title, hook.Entry[0].Link.Href)
} else {
str = strings.Replace(str, "{link}", hook.Entry[0].Link.Href, -1)
str = strings.Replace(str, "{txt}", hook.Entry[0].Title, -1)
}
2018-12-06 03:31:24 +00:00
msg := lineapi.TextMessage{
2018-09-15 15:09:41 +00:00
Text: str,
}
2018-12-06 02:56:20 +00:00
log.Println("msg ::::: ", msg)
2018-09-15 15:09:41 +00:00
lineapi.PushMessage(v.ID, msg)
}
}
c.Success(nil)
}