Compare commits
2 Commits
a2a0978f77
...
7049c22988
| Author | SHA1 | Date | |
|---|---|---|---|
| 7049c22988 | |||
| 6c169b7fd7 |
24
main.go
24
main.go
@@ -20,7 +20,7 @@ type Item struct {
|
|||||||
URL string `json:"url" db:"url"`
|
URL string `json:"url" db:"url"`
|
||||||
Title string `json:"title" db:"title"`
|
Title string `json:"title" db:"title"`
|
||||||
Description string `json:"description" db:"description"`
|
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"`
|
CreatedAt time.Time `json:"createdAt" db:"created_at"`
|
||||||
UpdatedAt time.Time `json:"updatedAt" db:"updated_at"`
|
UpdatedAt time.Time `json:"updatedAt" db:"updated_at"`
|
||||||
}
|
}
|
||||||
@@ -28,7 +28,7 @@ type Item struct {
|
|||||||
type ArticleMetadata struct {
|
type ArticleMetadata struct {
|
||||||
Title string
|
Title string
|
||||||
Description string
|
Description string
|
||||||
ImageURL string
|
Image *string
|
||||||
}
|
}
|
||||||
|
|
||||||
func getArticleMetadata(url string) (*ArticleMetadata, error) {
|
func getArticleMetadata(url string) (*ArticleMetadata, error) {
|
||||||
@@ -38,6 +38,12 @@ func getArticleMetadata(url string) (*ArticleMetadata, error) {
|
|||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode < 200 || resp.StatusCode >= 400 {
|
||||||
|
return &ArticleMetadata{
|
||||||
|
Title: url,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
doc, err := goquery.NewDocumentFromReader(resp.Body)
|
doc, err := goquery.NewDocumentFromReader(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to parse page html: %v", err)
|
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", "")
|
description := doc.Find(`head>meta[name="og:description"]`).AttrOr("content", "")
|
||||||
|
|
||||||
imageUrl := doc.Find(`head>meta[name="og:image"]`).AttrOr("content", "")
|
image := doc.Find(`head>meta[name="og:image"]`).AttrOr("content", "")
|
||||||
if imageUrl == "" {
|
if image == "" {
|
||||||
doc.Find("img").Each(func(i int, s *goquery.Selection) {
|
doc.Find("img").Each(func(i int, s *goquery.Selection) {
|
||||||
src := s.AttrOr("src", "")
|
src := s.AttrOr("src", "")
|
||||||
if src != "" {
|
if src != "" {
|
||||||
imageUrl = src
|
image = src
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -64,7 +70,7 @@ func getArticleMetadata(url string) (*ArticleMetadata, error) {
|
|||||||
return &ArticleMetadata{
|
return &ArticleMetadata{
|
||||||
Title: title,
|
Title: title,
|
||||||
Description: description,
|
Description: description,
|
||||||
ImageURL: imageUrl,
|
Image: &image,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,7 +97,7 @@ CREATE TABLE IF NOT EXISTS items (
|
|||||||
url varchar not null unique,
|
url varchar not null unique,
|
||||||
title varchar not null,
|
title varchar not null,
|
||||||
description varchar,
|
description varchar,
|
||||||
image_url varchar not null,
|
image varchar default null,
|
||||||
created_at datetime default current_timestamp,
|
created_at datetime default current_timestamp,
|
||||||
updated_at datetime default current_timestamp
|
updated_at datetime default current_timestamp
|
||||||
);
|
);
|
||||||
@@ -127,11 +133,11 @@ END;
|
|||||||
return
|
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,
|
"url": pageUrl,
|
||||||
"title": meta.Title,
|
"title": meta.Title,
|
||||||
"description": meta.Description,
|
"description": meta.Description,
|
||||||
"image_url": meta.ImageURL,
|
"image": meta.Image,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
http.Error(w, fmt.Sprintf("failed to add item to db: %v", err), 500)
|
http.Error(w, fmt.Sprintf("failed to add item to db: %v", err), 500)
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user