Add feed and item extensions

This commit is contained in:
Joakim Hellsén 2024-02-17 21:17:58 +01:00
commit 3113b844bc
9 changed files with 448 additions and 54 deletions

108
feeds.go
View file

@ -57,6 +57,15 @@ func makeCreateFeedParams(feedURL string, feed *gofeed.Feed) db.CreateFeedParams
}
func makeCreateItemParams(item *gofeed.Item, feedID int64) db.CreateItemParams {
var updatedTime time.Time
if item.UpdatedParsed != nil {
updatedTime = *item.UpdatedParsed
}
var publishedTime time.Time
if item.PublishedParsed != nil {
publishedTime = *item.PublishedParsed
}
itemCustom := []byte("{}")
if item.Custom != nil {
var err error
@ -77,9 +86,9 @@ func makeCreateItemParams(item *gofeed.Item, feedID int64) db.CreateItemParams {
Link: pgtype.Text{String: item.Link, Valid: item.Link != ""},
Links: item.Links,
Updated: pgtype.Text{String: item.Updated, Valid: item.Updated != ""},
UpdatedParsed: pgtype.Timestamp{Time: *item.UpdatedParsed, Valid: item.UpdatedParsed != nil},
UpdatedParsed: pgtype.Timestamptz{Time: updatedTime, Valid: !updatedTime.IsZero()},
Published: pgtype.Text{String: item.Published, Valid: item.Published != ""},
PublishedParsed: pgtype.Timestamp{Time: *item.PublishedParsed, Valid: item.PublishedParsed != nil},
PublishedParsed: pgtype.Timestamptz{Time: publishedTime, Valid: !publishedTime.IsZero()},
Guid: pgtype.Text{String: item.GUID, Valid: item.GUID != ""},
Categories: item.Categories,
Custom: itemCustom,
@ -110,14 +119,105 @@ func AddFeedToDB(feedURL string) error {
if err != nil {
return fmt.Errorf("Error adding feed to database: %s", err)
}
log.Printf("Added feed to database: %+v", newFeed)
log.Printf("Feed added to database")
// Add the items to the database
for _, item := range feed.Items {
_, err := DB.CreateItem(ctx, makeCreateItemParams(item, newFeed.ID))
newItem, err := DB.CreateItem(ctx, makeCreateItemParams(item, newFeed.ID))
if err != nil {
log.Printf("Error adding item to database: %s", err)
}
log.Printf("Item added to database")
// Add extensions to the database
for _, ext := range item.Extensions {
for _, exts := range ext {
for _, e := range exts {
attrsCustom := []byte("{}")
if e.Attrs != nil {
var err error
attrsCustom, err = json.Marshal(e.Attrs)
if err != nil {
fmt.Println("Error marshalling extension attributes:", err)
attrsCustom = []byte("{}")
}
log.Printf("Extension attributes: %s", attrsCustom)
}
childrenCustom := []byte("{}")
if e.Children != nil {
var err error
childrenCustom, err = json.Marshal(e.Children)
if err != nil {
fmt.Println("Error marshalling extension children:", err)
childrenCustom = []byte("{}")
}
log.Printf("Extension children: %s", childrenCustom)
}
_, err := DB.CreateItemExtension(ctx, db.CreateItemExtensionParams{
CreatedAt: pgtype.Timestamptz{Time: time.Now(), Valid: true},
UpdatedAt: pgtype.Timestamptz{Time: time.Now(), Valid: true},
DeletedAt: pgtype.Timestamptz{Valid: false},
Name: pgtype.Text{String: e.Name, Valid: e.Name != ""},
Value: pgtype.Text{String: e.Value, Valid: e.Value != ""},
Attrs: attrsCustom,
Children: childrenCustom,
ItemID: newItem.ID,
})
if err != nil {
log.Printf("Error adding extension to database: %s", err)
}
log.Printf("Extension added to database")
}
}
}
}
// Add extensions to the database
// TODO: Check if this is correct and works
for _, ext := range feed.Extensions {
for _, exts := range ext {
for _, e := range exts {
attrsCustom := []byte("{}")
if e.Attrs != nil {
var err error
attrsCustom, err = json.Marshal(e.Attrs)
if err != nil {
fmt.Println("Error marshalling extension attributes:", err)
attrsCustom = []byte("{}")
}
}
childrenCustom := []byte("{}")
if e.Children != nil {
var err error
childrenCustom, err = json.Marshal(e.Children)
if err != nil {
fmt.Println("Error marshalling extension children:", err)
childrenCustom = []byte("{}")
}
}
_, err := DB.CreateFeedExtension(ctx, db.CreateFeedExtensionParams{
CreatedAt: pgtype.Timestamptz{Time: time.Now(), Valid: true},
UpdatedAt: pgtype.Timestamptz{Time: time.Now(), Valid: true},
DeletedAt: pgtype.Timestamptz{Valid: false},
Name: pgtype.Text{String: e.Name, Valid: e.Name != ""},
Value: pgtype.Text{String: e.Value, Valid: e.Value != ""},
Attrs: attrsCustom,
Children: childrenCustom,
FeedID: newFeed.ID,
})
if err != nil {
log.Printf("Error adding extension to database: %s", err)
}
}
}
}
fmt.Println(feed.Title)