-
Notifications
You must be signed in to change notification settings - Fork 0
/
autonews.go
90 lines (74 loc) · 1.7 KB
/
autonews.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package autonews
import (
"context"
"strings"
"time"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/proto"
"github.com/lemon-mint/coord/llm"
"golang.org/x/net/html"
)
func NewAutoNews(model llm.LLM) *AutoNews {
return &AutoNews{
model: model,
}
}
type AutoNews struct {
model llm.LLM
}
func (g *AutoNews) Crawl(ctx context.Context, url string) (html string, err error) {
r := rod.New()
defer r.Close()
err = r.Connect()
if err != nil {
return
}
page, err := r.Page(proto.TargetCreateTarget{
URL: url,
})
if err != nil {
return
}
page = page.Context(ctx)
page = page.Timeout(time.Second * 30)
err = page.WaitDOMStable(time.Second, 0.01)
if err != nil {
return
}
html, err = page.HTML()
return
}
func (g *AutoNews) GeneratePost(ctx context.Context, url string) (title string, summary string, err error) {
orightml, err := g.Crawl(ctx, url)
if err != nil {
return
}
htmlnode, err := html.Parse(strings.NewReader(orightml))
if err != nil {
return
}
distillPipeline(htmlnode)
var sb strings.Builder
err = html.Render(&sb, htmlnode)
if err != nil {
return
}
summary, err = generateSummary(ctx, g.model, sb.String())
if err != nil {
return
}
title, err = generateTitle(ctx, g.model, sb.String())
return
}
func (g *AutoNews) Translate(ctx context.Context, text string) (translated string, err error) {
translated, err = generateTranslation(ctx, g.model, text)
return
}
func (g *AutoNews) Title(ctx context.Context, text string) (title string, err error) {
title, err = generateTitle(ctx, g.model, text)
return
}
func (g *AutoNews) NewsTitle(ctx context.Context, text string) (title string, err error) {
title, err = generateNewsTitle(ctx, g.model, text)
return
}