Skip to content

Commit

Permalink
Create InstagramPostData to replace tweet data
Browse files Browse the repository at this point in the history
  • Loading branch information
wazho committed Aug 12, 2023
1 parent 92dad8b commit c415d46
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 61 deletions.
22 changes: 11 additions & 11 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func WebhookFunction(w http.ResponseWriter, req *http.Request) {
client,
fmt.Sprintf("%s: %s", profile.DisplayName, message.Text),
)

messages := mt.GenerateAnsweringMessage(profile.DisplayName)
replyMessageCall := client.ReplyMessage(event.ReplyToken, messages...)
if _, err := replyMessageCall.Do(); err != nil {
Expand Down Expand Up @@ -208,19 +208,19 @@ func PostbackReply(client *linebot.Client, replyToken string, qs url.Values) {
}
} else if qs.Get("graphics") != "" {
// Refresh cache about data from cloud.
if time.Since(cache.TweetListUpdatedAt).Minutes() > 1 {
gd.LoadTweetList(&cache.TweetList)
cache.TweetListUpdatedAt = time.Now()
if time.Since(cache.InstagramPostListUpdatedAt).Minutes() > 1 {
gd.LoadInstagramPostList(&cache.InstagramPostList)
cache.InstagramPostListUpdatedAt = time.Now()
}

selectedTwitterUser := qs.Get("graphics")
selectedTweetID := qs.Get("tweetId")
selectedUserTweets := gd.FindUserTweets(cache.TweetList, selectedTwitterUser)
if selectedTweetID == "" {
messages = mt.GenerateGraphicCatalogMessages(selectedUserTweets)
selectedInstagramUser := qs.Get("graphics")
selectedInstagramPostID := qs.Get("instagramPostId")
selectedUserInstagramPosts := gd.FindUserInstagramPosts(cache.InstagramPostList, selectedInstagramUser)
if selectedInstagramPostID == "" {
messages = mt.GenerateGraphicCatalogMessages(selectedUserInstagramPosts)
} else {
selectedTweet := gd.FindTweet(selectedUserTweets.Tweets, selectedTweetID)
messages = mt.GenerateGraphicDetailMessages(selectedTweet, selectedTwitterUser)
selectedPost := gd.FindInstagramPost(selectedUserInstagramPosts.Posts, selectedInstagramPostID)
messages = mt.GenerateGraphicDetailMessages(selectedPost, selectedInstagramUser)
}
} else if qs.Get("channel") != "" {
// Refresh cache about data from cloud.
Expand Down
68 changes: 36 additions & 32 deletions gamedata/Gamedata.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,46 @@ import (

// DataCache has all remote data and last updated time
type DataCache struct {
OfficialPosts []OfficialPost
OfficialPostsUpdatedAt time.Time
RaidBosses []RaidBoss
RaidBossesUpdatedAt time.Time
Eggs []Egg
EggsUpdatedAt time.Time
Researches []Research
ResearchesUpdatedAt time.Time
RocketInvasions []RocketInvasion
RocketInvasionsUpdatedAt time.Time
Events []Event
EventsUpdatedAt time.Time
TweetList []UserTweets
TweetListUpdatedAt time.Time
Channels []Channel
ChannelsUpdatedAt time.Time
OfficialPosts []OfficialPost
OfficialPostsUpdatedAt time.Time
RaidBosses []RaidBoss
RaidBossesUpdatedAt time.Time
Eggs []Egg
EggsUpdatedAt time.Time
Researches []Research
ResearchesUpdatedAt time.Time
RocketInvasions []RocketInvasion
RocketInvasionsUpdatedAt time.Time
Events []Event
EventsUpdatedAt time.Time
TweetList []UserTweets
TweetListUpdatedAt time.Time
InstagramPostList []UserInstagramPosts
InstagramPostListUpdatedAt time.Time
Channels []Channel
ChannelsUpdatedAt time.Time
}

// GetCache stores data from remote
func GetCache() *DataCache {
return &DataCache{
OfficialPosts: []OfficialPost{},
OfficialPostsUpdatedAt: time.Now().AddDate(0, 0, -1),
RaidBosses: []RaidBoss{},
RaidBossesUpdatedAt: time.Now().AddDate(0, 0, -1),
Eggs: []Egg{},
EggsUpdatedAt: time.Now().AddDate(0, 0, -1),
Researches: []Research{},
ResearchesUpdatedAt: time.Now().AddDate(0, 0, -1),
RocketInvasions: []RocketInvasion{},
RocketInvasionsUpdatedAt: time.Now().AddDate(0, 0, -1),
Events: []Event{},
EventsUpdatedAt: time.Now().AddDate(0, 0, -1),
TweetList: []UserTweets{},
TweetListUpdatedAt: time.Now().AddDate(0, 0, -1),
Channels: []Channel{},
ChannelsUpdatedAt: time.Now().AddDate(0, 0, -1),
OfficialPosts: []OfficialPost{},
OfficialPostsUpdatedAt: time.Now().AddDate(0, 0, -1),
RaidBosses: []RaidBoss{},
RaidBossesUpdatedAt: time.Now().AddDate(0, 0, -1),
Eggs: []Egg{},
EggsUpdatedAt: time.Now().AddDate(0, 0, -1),
Researches: []Research{},
ResearchesUpdatedAt: time.Now().AddDate(0, 0, -1),
RocketInvasions: []RocketInvasion{},
RocketInvasionsUpdatedAt: time.Now().AddDate(0, 0, -1),
Events: []Event{},
EventsUpdatedAt: time.Now().AddDate(0, 0, -1),
TweetList: []UserTweets{},
TweetListUpdatedAt: time.Now().AddDate(0, 0, -1),
InstagramPostList: []UserInstagramPosts{},
InstagramPostListUpdatedAt: time.Now().AddDate(0, 0, -1),
Channels: []Channel{},
ChannelsUpdatedAt: time.Now().AddDate(0, 0, -1),
}
}
58 changes: 58 additions & 0 deletions gamedata/UserInstagramPosts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package gamedata

import (
"encoding/json"
"io/ioutil"
"net/http"

"github.com/thoas/go-funk"
)

// InstagramPostMediaData is pre-processing data from Picuki scraping
type InstagramPostMediaData struct {
URL string `json:"url"`
}

// InstagramPostData is pre-processing data from Picuki scraping
type InstagramPostData struct {
ID string `json:"id"`
Text string `json:"text"`
MediaList []InstagramPostMediaData `json:"mediaList"`
CreatedAt string `json:"createdAt"`
}

// UserInstagramPosts is pre-processing data from Picuki scraping
type UserInstagramPosts struct {
Username string `json:"username"`
Posts []InstagramPostData `json:"posts"`
}

// LoadInstagramPostList load data from remote JSON
func LoadInstagramPostList(cacheData *[]UserInstagramPosts) {
resp, fetchErr := http.Get("https://pmgo-professor-willow.github.io/data-instagram-posts/instagram-posts.min.json")

if fetchErr == nil {
defer resp.Body.Close()
bodyBuf, readErr := ioutil.ReadAll(resp.Body)

if readErr == nil {
postList := []UserInstagramPosts{}
json.Unmarshal(bodyBuf, &postList)
*cacheData = postList
}
}
}

// FindUserInstagramPosts finds user Instagram posts by specified user
func FindUserInstagramPosts(postList []UserInstagramPosts, instagramUser string) UserInstagramPosts {
return funk.Find(postList, func(instagramPosts UserInstagramPosts) bool {
return instagramPosts.Username == instagramUser
}).(UserInstagramPosts)
}

// FindInstagramPost finds Instagram post by specified ID
func FindInstagramPost(posts []InstagramPostData, picukiMediaID string) InstagramPostData {
return funk.Find(posts, func(post InstagramPostData) bool {
return post.ID == picukiMediaID
}).(InstagramPostData)
}
36 changes: 18 additions & 18 deletions messageTemplate/GraphicMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,42 @@ import (
"github.com/thoas/go-funk"
)

// GenerateGraphicCatalogMessages converts user tweets to LINE template messages
func GenerateGraphicCatalogMessages(userTweets gd.UserTweets) []linebot.SendingMessage {
if utils.IsEmpty(userTweets) {
// GenerateGraphicCatalogMessages converts user Instagram posts to LINE template messages
func GenerateGraphicCatalogMessages(instagramPosts gd.UserInstagramPosts) []linebot.SendingMessage {
if utils.IsEmpty(instagramPosts) {
return utils.GenerateEmptyReasonMessage()
}

tweetChunks := funk.Chunk(userTweets.Tweets, 10).([][]gd.TweetData)
postChunks := funk.Chunk(instagramPosts.Posts, 10).([][]gd.InstagramPostData)

return funk.Map(tweetChunks, func(tweetChunk []gd.TweetData) linebot.SendingMessage {
return funk.Map(postChunks, func(postChunk []gd.InstagramPostData) linebot.SendingMessage {
return linebot.NewTemplateMessage(
"近期的活動圖文資訊",
&linebot.ImageCarouselTemplate{
Columns: funk.Map(
userTweets.Tweets,
func(tweet gd.TweetData) *linebot.ImageCarouselColumn {
return GenerateGraphicColumn(userTweets.Name, tweet)
instagramPosts.Posts,
func(post gd.InstagramPostData) *linebot.ImageCarouselColumn {
return GenerateGraphicColumn(instagramPosts.Username, post)
},
).([]*linebot.ImageCarouselColumn),
},
)
}).([]linebot.SendingMessage)
}

// GenerateGraphicDetailMessages converts tweet to LINE messages
func GenerateGraphicDetailMessages(tweet gd.TweetData, userName string) []linebot.SendingMessage {
// GenerateGraphicDetailMessages converts Instagram post to LINE messages
func GenerateGraphicDetailMessages(post gd.InstagramPostData, username string) []linebot.SendingMessage {
text := fmt.Sprintf(
"此則圖文訊息由 %s 整理\n\n%s",
userName,
tweet.Text,
username,
post.Text,
)

messages := []linebot.SendingMessage{
linebot.NewTextMessage(text),
}

for _, media := range tweet.MediaList {
for _, media := range post.MediaList {
messages = append(
messages,
linebot.NewImageMessage(media.URL, media.URL),
Expand All @@ -60,15 +60,15 @@ func GenerateGraphicDetailMessages(tweet gd.TweetData, userName string) []linebo
return messages
}

// GenerateGraphicColumn converts user tweets to LINE column
func GenerateGraphicColumn(twitterUserName string, tweet gd.TweetData) *linebot.ImageCarouselColumn {
// GenerateGraphicColumn converts user Instagram post to LINE column
func GenerateGraphicColumn(instagramUsername string, post gd.InstagramPostData) *linebot.ImageCarouselColumn {
return linebot.NewImageCarouselColumn(
tweet.MediaList[0].URL,
post.MediaList[0].URL,
&linebot.PostbackAction{
Label: "檢視完整資訊",
Data: fmt.Sprintf(
"graphics=%s&tweetId=%s",
twitterUserName, tweet.ID,
"graphics=%s&instagramPostId=%s",
instagramUsername, post.ID,
),
},
)
Expand Down

0 comments on commit c415d46

Please sign in to comment.