Skip to content

Commit

Permalink
feat: Refactor Blogger fetchNewPosts function
Browse files Browse the repository at this point in the history
Use `fetchPosts`
  • Loading branch information
slashtechno authored Jun 26, 2024
1 parent 6faebeb commit d72a5c3
Showing 1 changed file with 4 additions and 17 deletions.
21 changes: 4 additions & 17 deletions internal/platforms/blogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,24 +287,13 @@ func (b *Blogger) fetchPosts(blogId, accessToken string) ([]map[string]interface
// Get posts that haven't been seen before and return them
func (b *Blogger) fetchNewPosts(options PushPullOptions) ([]PostData, error) {
// Get the list of posts
client := resty.New()
resp, err := client.R().
SetHeader("Authorization", fmt.Sprintf("Bearer %s", options.AccessToken)).
SetResult(&map[string]interface{}{}).
SetQueryParam("fetchBodies", "true").
SetQueryParam("status", "LIVE").
Get("https://www.googleapis.com/blogger/v3/blogs/" + options.BlogId + "/posts")
posts, err := b.fetchPosts(options.BlogId, options.AccessToken)
if err != nil {
return nil, err
}
// Assert that posts is a pointer to a map. However, dereference it to get the map
posts := (*resp.Result().(*map[string]interface{}))["items"].([]interface{})
// log.Debug("", "posts", posts)
// Check if the list of known posts is empty
if len(b.knownPosts) == 0 {
// Add all the posts to the list of known posts
for _, p := range posts {
post := p.(map[string]interface{})
for _, post := range posts {
b.knownPosts = append(b.knownPosts, post["id"].(string))
}
// Return an empty slice because there are no new posts
Expand All @@ -319,8 +308,7 @@ func (b *Blogger) fetchNewPosts(options PushPullOptions) ([]PostData, error) {
// If a post is no longer live, remove it from the list of known posts
for _, knownPost := range b.knownPosts {
found := false
for _, p := range posts {
post := p.(map[string]interface{})
for _, post := range posts {
if post["id"].(string) == knownPost {
found = true
break
Expand All @@ -334,8 +322,7 @@ func (b *Blogger) fetchNewPosts(options PushPullOptions) ([]PostData, error) {
}

// Get the postData for new posts
for _, p := range posts {
post := p.(map[string]interface{})
for _, post := range posts {
// Check if the post is new
if !utils.ContainsString(b.knownPosts, post["id"].(string)) {
// Add the post to the list of known posts
Expand Down

0 comments on commit d72a5c3

Please sign in to comment.