Compare commits

...

2 Commits

2 changed files with 6 additions and 14 deletions

View File

@@ -24,7 +24,8 @@ func main() {
var posts []models.Post = nil
err, posts := appContext.PostService.GetPostsFromApi(ctx, appContext.Tag, appContext.Instance)
if err != nil {
log.Fatal(err)
log.Println(err)
return
}
var existingPostIds = appContext.PostService.GetExistingPostIds()
@@ -33,18 +34,14 @@ func main() {
var newAccounts = appContext.PostService.GetNewAccounts(existingAccountIds, newPosts)
// Save to database
log.Printf("Number of existing posts: %d, existing accounts: %d, new posts: %d, new accounts: %d\n", len(existingPostIds), len(existingAccountIds), len(newPosts), len(newAccounts))
log.Printf("Fetched %d posts, %d accounts; %d new posts and %d new accounts\n", len(posts), len(existingAccountIds), len(newPosts), len(newAccounts))
// Additional logging
if newAccounts != nil {
log.Println("Number of inserted accounts: ", appContext.PostService.InsertNewAccounts(newAccounts))
} else {
log.Print(" - No new accounts inserted")
log.Printf("Inserted %d accounts\n", appContext.PostService.InsertNewAccounts(newAccounts))
}
if newPosts != nil {
log.Print(" - Number of inserted posts: ", appContext.PostService.InsertNewPosts(newPosts))
} else {
log.Print(" - No new posts inserted")
log.Printf("Inserted %d posts\n", appContext.PostService.InsertNewPosts(newPosts))
}
}

View File

@@ -5,7 +5,6 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
@@ -26,24 +25,20 @@ func (*PostService) GetPostsFromApi(ctx context.Context, tag string, instance st
var requestUrl = instance + "/api/v1/timelines/tag/" + tag + "?limit=40"
req, err := http.NewRequestWithContext(ctx, "GET", requestUrl, nil)
if err != nil {
log.Fatal(err)
return err, nil
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
return err, nil
}
if resp.StatusCode != 200 || strings.Split(strings.ToLower(resp.Header.Get("Content-Type")), ";")[0] != "application/json" {
log.Fatal("Status code:", resp.StatusCode, " Content-Type:", resp.Header.Get("Content-Type"))
return err, nil
return fmt.Errorf("Status code:", resp.StatusCode, " Content-Type:", resp.Header.Get("Content-Type")), nil
}
var posts []models.Post = nil
err = json.NewDecoder(resp.Body).Decode(&posts)
if err != nil {
log.Fatal(err)
return err, nil
}
// defer: it basically means "do this later when the function returns"