Compare commits

..

2 Commits

Author SHA1 Message Date
7049c22988 make item image field optional 2026-02-19 01:10:46 +03:00
6c169b7fd7 handle case if page responds not ok status 2026-02-19 01:10:21 +03:00

24
main.go
View File

@@ -20,7 +20,7 @@ type Item struct {
URL string `json:"url" db:"url"`
Title string `json:"title" db:"title"`
Description string `json:"description" db:"description"`
ImageURL string `json:"imageUrl" db:"image_url"`
Image *string `json:"image" db:"image"`
CreatedAt time.Time `json:"createdAt" db:"created_at"`
UpdatedAt time.Time `json:"updatedAt" db:"updated_at"`
}
@@ -28,7 +28,7 @@ type Item struct {
type ArticleMetadata struct {
Title string
Description string
ImageURL string
Image *string
}
func getArticleMetadata(url string) (*ArticleMetadata, error) {
@@ -38,6 +38,12 @@ func getArticleMetadata(url string) (*ArticleMetadata, error) {
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 400 {
return &ArticleMetadata{
Title: url,
}, nil
}
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to parse page html: %v", err)
@@ -50,12 +56,12 @@ func getArticleMetadata(url string) (*ArticleMetadata, error) {
description := doc.Find(`head>meta[name="og:description"]`).AttrOr("content", "")
imageUrl := doc.Find(`head>meta[name="og:image"]`).AttrOr("content", "")
if imageUrl == "" {
image := doc.Find(`head>meta[name="og:image"]`).AttrOr("content", "")
if image == "" {
doc.Find("img").Each(func(i int, s *goquery.Selection) {
src := s.AttrOr("src", "")
if src != "" {
imageUrl = src
image = src
return
}
})
@@ -64,7 +70,7 @@ func getArticleMetadata(url string) (*ArticleMetadata, error) {
return &ArticleMetadata{
Title: title,
Description: description,
ImageURL: imageUrl,
Image: &image,
}, nil
}
@@ -91,7 +97,7 @@ CREATE TABLE IF NOT EXISTS items (
url varchar not null unique,
title varchar not null,
description varchar,
image_url varchar not null,
image varchar default null,
created_at datetime default current_timestamp,
updated_at datetime default current_timestamp
);
@@ -127,11 +133,11 @@ END;
return
}
if _, err := db.NamedExec("INSERT INTO items (url, title, description, image_url) VALUES (:url, :title, :description, :image_url)", map[string]any{
if _, err := db.NamedExec("INSERT INTO items (url, title, description, image) VALUES (:url, :title, :description, :image)", map[string]any{
"url": pageUrl,
"title": meta.Title,
"description": meta.Description,
"image_url": meta.ImageURL,
"image": meta.Image,
}); err != nil {
http.Error(w, fmt.Sprintf("failed to add item to db: %v", err), 500)
return