-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractors.go
203 lines (184 loc) · 5.2 KB
/
extractors.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
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"fmt"
"golang.org/x/net/html"
"regexp"
"strconv"
"io/ioutil"
"path/filepath"
"net/http"
"io"
"bytes"
"strings"
"sort"
"time"
"path"
"errors"
"os"
"net/url"
"github.com/schollz/progressbar/v3"
"github.com/PuerkitoBio/goquery"
)
type ExtractorType struct {
Validator *regexp.Regexp
Name, ContentSelector string
Clean func(*goquery.Document, string)
MustSkip func([]*html.Node) bool
IMGProcessor func(n *goquery.Selection)
}
type ThumbnailType struct {
sort.IntSlice
Href string
X, Y int
Pass bool
}
var extractors = []ExtractorType{wiki}
var local = ExtractorType{
Name: "local",
ContentSelector: "body",
Clean: func(doc *goquery.Document, lang string) {},
MustSkip: func(TitleStack []*html.Node) bool {return false},
}
var wiki = ExtractorType{
Name: "Wikipedia",
Validator: regexp.MustCompile(`https?://([a-z]+).wikipedia.org/wiki/([^?]+)`), // TODO https://en.wikipedia.org/w/index.php?title=xxxxxxxxxxx
ContentSelector: ".mw-content-ltr",
Clean: func(doc *goquery.Document, lang string) {
doc.Find(".sistersitebox, #toc, table.navbox-inner").Remove()
doc.Find("table.metadata, span.mw-editsection").Remove()
doc.Find("table.mw-collapsible").Children().First().Unwrap()
doc.Find("h1").Remove()
doc.Find("a").Each(func(i int, s *goquery.Selection) {
href, found := s.Attr("href")
if found {
s.SetAttr("href", "https://"+lang + ".wikipedia.org" +href)
}
})
doc.Find("h2,h3,h4,h5,h6").Each(func(i int, s *goquery.Selection) {
h := s.Nodes[0]
x, _ := strconv.Atoi(h.Data[1:])
s.Nodes[0].Data = fmt.Sprint("h", x-1)
})
},
MustSkip: func(TitleStack []*html.Node) bool {
if contains([]string{"Notes", "See also", "External links", "References" , "Citations", "Bibliography"}, Text(TitleStack[1])) {
return true
}
return false
},
IMGProcessor: func(n *goquery.Selection) {
imgs := n.Find("img")
bar := progressbar.DefaultBytes(
-1,
"Downloading images...",
)
imgs.Each(func(i int, s *goquery.Selection) {
href, found := s.Parent().Attr("href")
if !found {
return
}
//pp.Println(RenderNode(s.Nodes[0]))
href = wikiPrefForHiRes(href)
//log.Debug().Msg("\n"+src+" →→→→→→→ "+path.Base(href))
s.RemoveAttr("width")
s.RemoveAttr("height")
s.RemoveAttr("srcset")
s.RemoveAttr("decoding")
s.RemoveAttr("class")
s.RemoveAttr("data-file-height")
s.RemoveAttr("data-file-width")
filename, _ := url.QueryUnescape(path.Base(href))
s.SetAttr("src", filename)
s.Unwrap()
p := pref.CollectionMedia+filename
if _, err := os.Stat(p); errors.Is(err, os.ErrNotExist) {
//log.Debug().Msg("\nDownloading https://"+strings.TrimPrefix(href, "//"))
DownloadFile(p, "https://"+strings.TrimPrefix(href, "//"), bar)
currentTime := time.Now().Local()
_ = os.Chtimes(p, currentTime, currentTime)
}
bar.Add(1)
})
fmt.Print("\n")
},
}
func (Extractor ExtractorType) TakeImgAlong(n *goquery.Selection) {
if Extractor.Name == "local" {
files, _ := ioutil.ReadDir(filepath.Dir(*inFile))
var total int
for _, file := range files {
for _, ext := range []string{".jpg", ".jpeg", ".png", ".tif", ".tiff", ".gif", ".svg", ".webp", ".avif"} {
fpath := filepath.Dir(*inFile) + string(os.PathSeparator) + file.Name()
_, err := os.Stat(pref.CollectionMedia + file.Name())
if ext == filepath.Ext(file.Name()) && errors.Is(err, os.ErrNotExist) {
from, err := os.Open(fpath)
check(err)
to, err := os.OpenFile(pref.CollectionMedia + file.Name(), os.O_CREATE|os.O_WRONLY, 0644)
check(err)
_, err = io.Copy(to, from)
check(err)
from.Close()
to.Close()
total += 1
}
}
}
log.Info().Msg(fmt.Sprint(total, " images copied."))
} else {
Extractor.IMGProcessor(n)
}
}
func wikiPrefForHiRes(href string) string {
resp, err := http.Get(href)
check(err)
if resp.StatusCode != http.StatusOK {
log.Error().Str("IMG: Received response status", resp.Status).Msg("HTTP")
}
file, err := io.ReadAll(resp.Body)
check(err)
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(file))
check(err)
var Thumbnails []ThumbnailType
var xs []int
doc.Find("a.mw-thumbnail-link").Each(func(i int, s *goquery.Selection) {
href, _ := s.Attr("href")
Thumbnail := ThumbnailType{Href: href}
sz := strings.ReplaceAll(strings.TrimSuffix(s.Text(), " pixels"), ",", "")
xstr, ystr, _ := strings.Cut(sz, " × ")
x, _ := strconv.Atoi(xstr)
y, _ := strconv.Atoi(ystr)
Thumbnail.Pass = true
if pref.ResXMax < x {
Thumbnail.Pass = false
}
if pref.ResYMax < y {
Thumbnail.Pass = false
}
Thumbnail.X = x
Thumbnail.Y = y
xs = append(xs, x)
Thumbnails = append(Thumbnails, Thumbnail)
})
Rating := RatingType{sort.IntSlice(xs), Thumbnails}
sort.Stable(Rating)
for _, Thumbnail := range Thumbnails {
if Thumbnail.Pass {
href = Thumbnail.Href
}
}
return href
}
func DownloadFile(filepath string, url string, bar *progressbar.ProgressBar) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(io.MultiWriter(out, bar), resp.Body)
return err
}