forked from arussellsaw/news
/
handle_refresh_article.go
61 lines (54 loc) · 1.4 KB
/
handle_refresh_article.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package handler
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"github.com/RusticPotatoes/news/dao"
"github.com/monzo/slog"
)
func handleRefreshArticle(w http.ResponseWriter, r *http.Request) {
var (
ctx = r.Context()
id = r.URL.Query().Get("id")
)
a, err := dao.GetArticle(ctx, id)
if err != nil {
slog.Error(ctx, "Error decoding JSON: %s", err)
return
}
slogParams := map[string]string{
"url": a.Link,
}
res, err := c.Get(fmt.Sprintf("https://readability-server.russellsaw.io/?url=%s", url.QueryEscape(a.Link)))
if err != nil {
slog.Error(ctx, "Error fetching article: %s", err, slogParams)
return
}
var article = struct {
Body string `json:"body"`
BodyText string `json:"body_text"`
}{}
err = json.NewDecoder(res.Body).Decode(&article)
if err != nil {
slog.Error(ctx, "Error fetching article: %s", err, slogParams)
return
}
// a.Content = toElements(article.BodyText, "\n")
a.SetHTMLContent(article.Body)
// sa, err := swan.FromHTML(a.Link, []byte(article.Body))
// if err != nil {
// slog.Error(ctx, "Error parsing article: %s", err, slogParams)
// return
// }
// if sa.Img != nil {
// a.ImageURL = sa.Img.Src
// }
err = dao.SetArticle(ctx, a)
if err != nil {
slog.Error(ctx, "Error storing article: %s", err, slogParams)
return
}
slog.Info(ctx, "Stored article: %s - %s", a.ID, a.Title)
http.Redirect(w, r, fmt.Sprintf("/article?id=%s", a.ID), 307)
}