Add support for Dublin Core

This commit is contained in:
Joakim Hellsén 2024-02-18 01:05:30 +01:00
commit 8f7690d8f4
9 changed files with 800 additions and 173 deletions

View file

@ -1,7 +1,6 @@
package main
import (
"context"
"encoding/json"
"fmt"
"log"
@ -55,93 +54,3 @@ func makeCreateFeedParams(feedURL string, feed *gofeed.Feed) db.CreateFeedParams
return params
}
func addFeedExtensionToDB(ctx context.Context, feed *gofeed.Feed, newFeed db.Feed) {
// 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)
}
}
}
}
}
func addFeedAuthors(ctx context.Context, feed *gofeed.Feed, newFeed db.Feed) {
if feed.Authors == nil {
log.Printf("No authors to add to database")
return
}
// Add authors to the database
for _, author := range feed.Authors {
_, err := DB.CreateFeedAuthor(ctx, db.CreateFeedAuthorParams{
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: author.Name, Valid: author.Name != ""},
FeedID: newFeed.ID,
})
if err != nil {
log.Printf("Error adding author %s (%s) to database: %s", author.Name, author.Email, err)
continue
}
log.Printf("Author %s (%s) added to database", author.Name, author.Email)
}
}
func addFeedImages(ctx context.Context, feed *gofeed.Feed, newFeed db.Feed) {
if feed.Image == nil {
log.Printf("No image to add to database")
return
}
// TODO: Download the image and store it on the server
_, err := DB.CreateFeedImage(ctx, db.CreateFeedImageParams{
CreatedAt: pgtype.Timestamptz{Time: time.Now(), Valid: true},
UpdatedAt: pgtype.Timestamptz{Time: time.Now(), Valid: true},
DeletedAt: pgtype.Timestamptz{Valid: false},
Url: pgtype.Text{String: feed.Image.URL, Valid: feed.Image.URL != ""},
Title: pgtype.Text{String: feed.Image.Title, Valid: feed.Image.Title != ""},
FeedID: newFeed.ID,
})
if err != nil {
log.Printf("Error adding image to database: %s", err)
return
}
log.Printf("Image added to database: %s", feed.Image.URL)
}