add instagram api and schema

This commit is contained in:
Jay 2019-05-22 22:25:09 +08:00
parent d7d5ace2c0
commit 426bb0fea8
5 changed files with 173 additions and 0 deletions

1
go.sum
View File

@ -110,6 +110,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190329044733-9eb1bfa1ce65 h1:hOY+O8MxdkPV10pNf7/XEHaySCiPKxixMKUshfHsGn0=
golang.org/x/sys v0.0.0-20190329044733-9eb1bfa1ce65/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190402142545-baf5eb976a8c h1:3xiKTkef8QqBJ8q+4fVUDMRoxnI0H/MVNFswa+aExbo=
golang.org/x/sys v0.0.0-20190402142545-baf5eb976a8c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
google.golang.org/appengine v1.5.0 h1:KxkO13IPW4Lslp2bz+KHP2E3gtFlrIGNThxkZQ3g+4c=

74
model/instagram.go Normal file
View File

@ -0,0 +1,74 @@
package model
import (
"database/sql"
"time"
)
type IGGroup struct {
*LineGroup
Tmpl string `db:"tmpl"`
}
//Instagram -
type Instagram struct {
ID string `db:"id" cc:"id"`
LastPost string `db:"lastpost" cc:"lastpost"`
Ctime time.Time `db:"ctime" cc:"ctime"`
Mtime time.Time `db:"mtime" cc:"ctime"`
Groups []*IGGroup `db:"-"`
}
// GetAllInstagram -
func GetAllInstagram() (igs []*Instagram, err error) {
err = x.Select(&igs, `select * from "public"."instagram"`)
if err != nil {
return nil, err
}
return
}
// GetInstagram -
func GetInstagram(id string) (ig *Instagram, err error) {
ig = &Instagram{}
err = x.Get(ig, `select * from "public"."instagram" where "id" = $1`, id)
if err == sql.ErrNoRows {
return nil, nil
}
return
}
// AddIG -
func (p *Instagram) AddIG() (err error) {
stmt, err := x.PrepareNamed(`insert into "public"."instagram" ("id", "lastpost") values (:id, :lastpost) returning *`)
if err != nil {
return err
}
err = stmt.Get(p, p)
return
}
// UpdatePost -
func (p *Instagram) UpdatePost(postID string) (err error) {
query := `update "public"."instagram" set "lastpost" = $1, "mtime" = now() where "id" = $2`
_, err = x.Exec(query, postID, p.ID)
if err != nil {
return
}
p.LastPost = postID
return
}
// GetGroups -
func (p *Instagram) GetGroups() (err error) {
query := `select g.*, rt.tmpl as tmpl from "public"."facebook_page" p
left join "public"."line_fb_rt" rt
on rt."facebook" = p.id
left join "public"."line_group" g
on g.id = rt."line"
where
p.id = $1
and rt.facebook is not null`
err = x.Select(&p.Groups, query, p.ID)
return
}

View File

@ -19,6 +19,12 @@ type LineYoutubeRT struct {
Tmpl string `db:"tmpl" cc:"tmpl"`
}
type LineIGRT struct {
Line string `db:"line" cc:"line"`
IG string `db:"ig" cc:"ig"`
Tmpl string `db:"tmpl" cc:"tmpl"`
}
// AddRT - add facebook line rt
func (p *LineFacebookRT) AddRT() (err error) {
_, err = x.NamedExec(`insert into "public"."line_fb_rt" ("line", "facebook", "tmpl") values (:line, :facebook, :tmpl)`, p)
@ -64,3 +70,15 @@ func (p *LineYoutubeRT) DelRT() (err error) {
_, err = x.NamedExec(`delete from "public"."line_youtube_rt" where "line" = :line and "youtube" = :youtube`, p)
return
}
// AddRT -
func (p *LineIGRT) AddRT() (err error) {
_, err = x.NamedExec(`insert into "public"."line_ig_rt" ("line", "ig", "tmpl") values (:line, :ig, :tmpl)`, p)
return
}
// DelRT -
func (p *LineIGRT) DelRT() (err error) {
_, err = x.NamedExec(`delete from "public"."line_ig_rt" where "line" = :line and "ig" = :ig`, p)
return
}

View File

@ -46,6 +46,24 @@ func GetFacebookPageIDs(c *context.Context) {
})
}
//GetInstagramIDs -
func GetInstagramIDs(c *context.Context) {
igs, err := model.GetAllInstagram()
if err != nil {
c.ServerError(nil)
return
}
ids := make([]string, 0, len(igs))
for _, v := range igs {
ids = append(ids, v.ID)
}
c.Success(map[string]interface{}{
"list": ids,
})
}
// UpdateFacebookPagePost -
func UpdateFacebookPagePost(c *context.Context) {
var err error
@ -106,3 +124,63 @@ func UpdateFacebookPagePost(c *context.Context) {
c.Success(nil)
}
// UpdateInstagramPost -
func UpdateInstagramPost(c *context.Context) {
var err error
type pageStruct struct {
ID string `json:"id"`
PostID string `json:"post_id"`
Link string `json:"link"`
Text string `json:"text"`
}
bodyArg := struct {
IGs []pageStruct `json:"igs"`
}{}
err = c.BindData(&bodyArg)
if err != nil {
c.DataFormat(nil)
return
}
for _, v := range bodyArg.IGs {
if len(v.ID) == 0 || len(v.PostID) == 0 || len(v.Link) == 0 {
continue
}
ig, err := model.GetInstagram(v.ID)
if err != nil {
continue
}
if ig.LastPost == v.PostID {
continue
}
err = ig.UpdatePost(v.PostID)
if err != nil {
continue
}
err = ig.GetGroups()
if err != nil {
continue
}
for _, g := range ig.Groups {
if g.Notify {
tmpl := g.Tmpl
if len(tmpl) > 0 {
tmpl = strings.Replace(tmpl, "{link}", v.Link, -1)
tmpl = strings.Replace(tmpl, "{txt}", v.Text, -1)
} else {
tmpl = fmt.Sprintf("%s\n%s", v.Text, v.Link)
}
msg := line.TextMessage{Text: tmpl}
line.PushMessage(g.ID, msg)
}
}
}
c.Success(nil)
}

View File

@ -91,6 +91,8 @@ func SetRoutes(r *gin.Engine) {
{
privateAPIGroup.GET("/pages", context.PatchCtx(private.GetFacebookPageIDs))
privateAPIGroup.POST("/pageposts", context.PatchCtx(private.UpdateFacebookPagePost))
privateAPIGroup.GET("/ig", context.PatchCtx(private.GetInstagramIDs))
privateAPIGroup.POST("/igposts", context.PatchCtx(private.UpdateInstagramPost))
}
apiTwitchGroup := apiGroup.Group("/twitch", context.PatchCtx(api.CheckSession))