-
Notifications
You must be signed in to change notification settings - Fork 0
/
readability.go
100 lines (94 loc) · 2.47 KB
/
readability.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
package utils
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os/exec"
"github.com/astaxie/beego"
"github.com/missdeer/kelly/setting"
)
func renderPythonReadability(content string) string {
cmd := exec.Command("python", "-m", "readability.readability", "-u", content)
stdout, err := cmd.StdoutPipe()
if err != nil {
beego.Error("creating stdout pipe failed: ", err)
return ""
}
if err := cmd.Start(); err != nil {
beego.Error("starting command failed: ", err)
return ""
}
raw, err := ioutil.ReadAll(stdout)
if err != nil {
beego.Error("reading stdout from pipe failed: ", err)
return ""
}
if err := cmd.Wait(); err != nil {
beego.Error("waiting from command failed:", err)
return ""
}
return string(raw)
}
func renderReadability(content string) string {
appkey := setting.ReadabilityAppKey
req := fmt.Sprintf("https://www.readability.com/api/content/v1/parser?url=%s&token=%s", url.QueryEscape(content), appkey)
resp, err := http.Get(req)
if err != nil {
beego.Error("read response error: ", err)
return ""
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
var res map[string]interface{}
json.Unmarshal(body, &res)
if str, ok := res["content"].(string); ok {
return str
} else {
beego.Error("converting content to string failed")
return ""
}
}
func renderEmbedlyExtract(content string) string {
appkey := setting.EmbedlyAppKey
req := fmt.Sprintf("http://api.embed.ly/1/extract?key=%s&url=%s", appkey, url.QueryEscape(content))
resp, err := http.Get(req)
if err != nil {
beego.Error("read response error: ", err)
return ""
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
var res map[string]interface{}
err = json.Unmarshal(body, &res)
if err != nil {
beego.Error("json unmarshalling failed: ", err)
return ""
}
fmt.Printf("%v\n", res)
if str, ok := res["content"].(string); ok {
return str
} else {
beego.Error("converting content to string failed")
return ""
}
}
func RenderReadability(content string, interpreter string) string {
if len(interpreter) == 0 {
interpreter = setting.ReadabilityBackend
}
switch interpreter {
case "readability", "r":
beego.Info("readability backend")
return renderReadability(content)
case "embedly", "e":
beego.Info("embedly extract backend")
return renderEmbedlyExtract(content)
default:
beego.Info("default backend")
return renderPythonReadability(content)
}
beego.Info("default backend")
return renderPythonReadability(content)
}