-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathoutline.go
50 lines (43 loc) · 1.49 KB
/
outline.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
package responsemodifers
import (
"bytes"
"encoding/json"
//"github.com/go-shiori/dom"
"github.com/markusmobius/go-trafilatura"
//"golang.org/x/net/html"
"io"
"ladder/proxychain"
"ladder/proxychain/responsemodifers/api"
)
// APIOutline creates an JSON representation of the article and returns it as an API response.
func APIOutline() proxychain.ResponseModification {
return func(chain *proxychain.ProxyChain) error {
// we set content-type twice here, in case another response modifier
// tries to forward over the original headers
chain.Context.Set("content-type", "application/json")
chain.Response.Header.Set("content-type", "application/json")
// extract dom contents
opts := trafilatura.Options{
IncludeImages: true,
IncludeLinks: true,
//FavorPrecision: true,
FallbackCandidates: nil, // TODO: https://github.com/markusmobius/go-trafilatura/blob/main/examples/chained/main.go
// implement fallbacks from "github.com/markusmobius/go-domdistiller" and "github.com/go-shiori/go-readability"
OriginalURL: chain.Request.URL,
}
result, err := trafilatura.Extract(chain.Response.Body, opts)
if err != nil {
chain.Response.Body = api.CreateAPIErrReader(err)
return nil
}
doc := api.ExtractResultToAPIResponse(result)
jsonData, err := json.MarshalIndent(doc, "", "\t")
if err != nil {
chain.Response.Body = api.CreateAPIErrReader(err)
return nil
}
buf := bytes.NewBuffer(jsonData)
chain.Response.Body = io.NopCloser(buf)
return nil
}
}