-
Notifications
You must be signed in to change notification settings - Fork 0
/
article.go
191 lines (161 loc) · 4.16 KB
/
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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package kiep
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"strings"
"sync"
"time"
readability "github.com/go-shiori/go-readability"
"github.com/go-shiori/obelisk"
ia "github.com/wabarc/archive.org/pkg"
"golang.org/x/net/html"
)
type article struct {
url string
archivedUrl string
date time.Time
tags []string
author string
title string
}
func ArchiveArticle(url string, tags []string) {
snapShotCh := make(chan string)
go createSnapshot(url, snapShotCh)
fullPageCh := make(chan []byte)
go archiveFullPage(url, fullPageCh)
baseDir := getArchiveDir()
headerPath := path.Join(baseDir, "static/header.html")
headerCh := make(chan string)
go readFile(headerPath, headerCh)
footerPath := path.Join(baseDir, "static/footer.html")
footerCh := make(chan string)
go readFile(footerPath, footerCh)
page := downloadPage(url)
page_reader := bytes.NewReader(page)
metaData := getMetaData(page_reader)
metaData.url = url
metaData.tags = tags
page_reader.Seek(0, 0)
plain, stripped := makeReadable(url, page_reader)
title := "<h1>" + metaData.title + "</h1><hr>"
stripped = fmt.Sprintf(<-headerCh, metaData.title) + title + stripped + <-footerCh
metaData.archivedUrl = <-snapShotCh
fileContent := make(map[string][]byte)
fileContent["plain.txt"] = []byte(plain)
fileContent["stripped.html"] = []byte(stripped)
fileContent["full_page.html"] = <-fullPageCh
fileContent[".meta"] = []byte(createMetaFile(&metaData))
destDir := path.Join(baseDir, metaData.date.Format("2006-02-01")+"-"+metaData.title)
saveToDisk(&fileContent, destDir)
}
func downloadPage(url string) []byte {
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
page, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
return page
}
func getMetaData(page io.Reader) article {
metaData := article{date: time.Now()}
/* Parse HTML */
doc, err := html.Parse(page)
if err != nil {
panic(err)
}
var findMeta func(n *html.Node, metaData *article)
findMeta = func(n *html.Node, metaData *article) {
if n.Type == html.ElementNode {
switch n.Data {
case "meta":
switch n.Attr[0].Val {
case "author":
metaData.author = n.Attr[1].Val
case "og:title":
metaData.author = n.Attr[1].Val
/* Implementation unsure
case "og:url":
metaData.url = n.Attr[1].Val
*/
}
case "title":
if metaData.title == "" {
metaData.title = n.FirstChild.Data
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
findMeta(c, metaData)
}
}
findMeta(doc, &metaData)
return metaData
}
func makeReadable(url string, page io.Reader) (string, string) {
art, err := readability.FromReader(page, url)
if err != nil {
panic(err)
}
return art.TextContent, art.Content
}
func createSnapshot(url string, c chan string) {
var wbrc ia.Archiver
result, err := wbrc.Wayback([]string{url})
if err != nil {
panic(err)
}
c <- result[url]
}
func archiveFullPage(url string, c chan []byte) {
// Create archive
req := obelisk.Request{URL: url}
arc := obelisk.Archiver{EnableLog: true}
arc.Validate()
result, _, err := arc.Archive(context.Background(), req)
if err != nil {
panic(err)
}
c <- result
}
func saveToDisk(fileContent *map[string][]byte, destDir string) {
os.MkdirAll(destDir, os.ModePerm)
// write given files
var wg sync.WaitGroup
wg.Add(len(*fileContent))
for fileName, content := range *fileContent {
go func(fileName string, content []byte, destDir string) {
defer wg.Done()
err := ioutil.WriteFile(path.Join(destDir, fileName), content, 0644)
if err != nil {
panic(err)
}
}(fileName, content, destDir)
}
wg.Wait()
}
func createMetaFile(metaData *article) string {
content := "title=" + metaData.title + "\n" +
"tags=[" + strings.Join(metaData.tags, " | ") + "]\n" +
"date=" + metaData.date.Format("2006-02-01") + "\n" +
"author=" + metaData.author + "\n" +
"url=" + metaData.url + "\n" +
"archived=" + metaData.archivedUrl + "\n"
return content
}
func readFile(fileName string, returnCh chan string) {
cont, err := ioutil.ReadFile(fileName)
if err != nil {
panic(err)
}
returnCh <- string(cont)
}