Fix pinned tweets

(added to the beginning of the timeline)
Close #23
This commit is contained in:
Alexander Sheiko 2021-01-06 21:19:12 +02:00
parent 142387af97
commit 1e048200bc
2 changed files with 29 additions and 2 deletions

View file

@ -146,6 +146,19 @@ type (
} `json:"content,omitempty"`
} `json:"entries"`
} `json:"addEntries"`
PinEntry struct {
Entry struct {
Content struct {
Item struct {
Content struct {
Tweet struct {
ID string `json:"id"`
} `json:"tweet"`
} `json:"content"`
} `json:"item"`
} `json:"content"`
} `json:"entry"`
} `json:"pinEntry,omitempty"`
} `json:"instructions"`
} `json:"timeline"`
}

18
util.go
View file

@ -84,8 +84,13 @@ func getTimeline(ctx context.Context, query string, maxTweetsNbr int, fetchFunc
}
if tweetsNbr < maxTweetsNbr {
if tweet.IsPin && nextCursor != "" {
continue
}
nextCursor = next
channel <- &Result{Tweet: *tweet}
} else {
break
}
tweetsNbr++
}
@ -192,9 +197,15 @@ func parseTimeline(timeline *timeline) ([]*Tweet, string) {
}
var cursor string
var pinnedTweet *Tweet
var orderedTweets []*Tweet
if len(timeline.Timeline.Instructions) > 0 {
for _, entry := range timeline.Timeline.Instructions[0].AddEntries.Entries {
for _, instruction := range timeline.Timeline.Instructions {
if instruction.PinEntry.Entry.Content.Item.Content.Tweet.ID != "" {
if tweet, ok := tweets[instruction.PinEntry.Entry.Content.Item.Content.Tweet.ID]; ok {
pinnedTweet = &tweet
}
}
for _, entry := range instruction.AddEntries.Entries {
if tweet, ok := tweets[entry.Content.Item.Content.Tweet.ID]; ok {
orderedTweets = append(orderedTweets, &tweet)
}
@ -203,5 +214,8 @@ func parseTimeline(timeline *timeline) ([]*Tweet, string) {
}
}
}
if pinnedTweet != nil && len(orderedTweets) > 0 {
orderedTweets = append([]*Tweet{pinnedTweet}, orderedTweets...)
}
return orderedTweets, cursor
}