forked from go-shiori/go-readability
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.go
140 lines (118 loc) · 3.4 KB
/
export.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
/*
#include <stdlib.h>
*/
import "C"
import (
"encoding/json"
"fmt"
nurl "net/url"
"strings"
"sync"
"unsafe"
"github.com/go-shiori/dom"
readability "github.com/go-shiori/go-readability"
"github.com/google/uuid"
)
var unsafePointers = make(map[string]*C.char)
var unsafePointersLock = sync.Mutex{}
var errorFormat = "{\"id\": \"%v\", \"error\": \"%v\"}"
var sessionsPool = make(map[string]*sync.Pool)
var sessionsPoolLock = sync.Mutex{}
func return_safe_result(result string, outputId string) *C.char {
resultString := C.CString(result)
unsafePointersLock.Lock()
unsafePointers[outputId] = resultString
defer unsafePointersLock.Unlock()
return resultString
}
//export parse
func parse(htmlContent *C.char, pageURL *C.char) *C.char {
outputId := uuid.New().String()
defer func() {
if r := recover(); r != nil {
fmt.Println("Go-readability recovered from panic:", r)
}
}()
// Convert C strings to Go strings
htmlStr := C.GoString(htmlContent)
urlStr := C.GoString(pageURL)
// Parse URL
parsedURL, err := nurl.ParseRequestURI(urlStr)
if err != nil {
return return_safe_result(fmt.Sprintf(errorFormat, outputId, "Error parsing URL: "+err.Error()), outputId)
}
// Read HTML content
reader := strings.NewReader(htmlStr)
doc, err := dom.Parse(reader)
if err != nil {
return return_safe_result(fmt.Sprintf(errorFormat, outputId, "Error parsing HTML content: "+err.Error()), outputId)
}
// Extract readable content
article, err := readability.FromDocument(doc, parsedURL)
if err != nil {
return return_safe_result(fmt.Sprintf(errorFormat, outputId, "Error extracting content: "+err.Error()), outputId)
}
// Prepare output
output := struct {
ID string `json:"id"`
HTML string `json:"html"`
ERROR string `json:"error"`
Metadata struct {
Title string `json:"title,omitempty"`
Byline string `json:"byline,omitempty"`
Excerpt string `json:"excerpt,omitempty"`
Language string `json:"language,omitempty"`
SiteName string `json:"siteName,omitempty"`
Readerable bool `json:"readerable"`
} `json:"metadata"`
}{
ID: outputId,
HTML: dom.OuterHTML(article.Node),
ERROR: "",
Metadata: struct {
Title string `json:"title,omitempty"`
Byline string `json:"byline,omitempty"`
Excerpt string `json:"excerpt,omitempty"`
Language string `json:"language,omitempty"`
SiteName string `json:"siteName,omitempty"`
Readerable bool `json:"readerable"`
}{
Title: article.Title,
Byline: article.Byline,
Excerpt: article.Excerpt,
Language: article.Language,
SiteName: article.SiteName,
Readerable: readability.CheckDocument(doc),
},
}
// Serialize to JSON
result, err := json.Marshal(output)
if err != nil {
return return_safe_result(fmt.Sprintf(errorFormat, outputId, "Error serializing output: "+err.Error()), outputId)
}
// Return result as C string
return return_safe_result(string(result), outputId)
}
//export freeMemory
func freeMemory(responseId *C.char) {
responseIdString := C.GoString(responseId)
unsafePointersLock.Lock()
defer unsafePointersLock.Unlock()
ptr, ok := unsafePointers[responseIdString]
if !ok {
fmt.Println("freeMemory:", ok)
return
}
if ptr != nil {
defer C.free(unsafe.Pointer(ptr))
}
delete(unsafePointers, responseIdString)
}
func main() {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered from panic:", r)
}
}()
}