This repository has been archived by the owner on Jul 29, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postscraper.go
262 lines (214 loc) · 7.14 KB
/
postscraper.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package insidescraper
import (
"fmt"
"io/ioutil"
"math"
"net/http"
"path"
"reflect"
"strings"
"github.com/PuerkitoBio/goquery"
)
// DataType is the type of the site data.
type DataType int
const (
// SectionType refers to a section.
SectionType DataType = iota
// LessonType refers to a lesson.
LessonType
// MediaType refers to a media.
MediaType
)
// PostScraper goes over the scraped data and fixes it up as much as possible.
type PostScraper struct {
Site Site
Missing map[string]Correction
Empty map[string]Correction
}
// Correction is a (possible) correction for a missing link.
type Correction struct {
Guesses []string
// The sections which reference the bad URL.
Parents []string
Is404 bool
IsConfirmed bool
WasCorrected bool
Source DataType
}
// FixSite applies fixes to the site data.
func (cleaner *PostScraper) FixSite() {
cleaner.Missing = cleaner.GetMissingCorrections()
cleaner.Empty = cleaner.GetEmptyCorrections()
for badID, correction := range cleaner.Missing {
cleaner.applyFix(badID, &correction)
cleaner.Missing[badID] = correction
}
for badID, correction := range cleaner.Empty {
cleaner.applyFix(badID, &correction)
cleaner.Empty[badID] = correction
}
}
// GetMissingCorrections attempts to find as many missing sections as possible.
func (cleaner *PostScraper) GetMissingCorrections() map[string]Correction {
corrections := make(map[string]Correction, 10)
for parentID, section := range cleaner.Site.Sections {
for _, subSectionID := range section.Sections {
// Don't find correction twice.
if _, exists := corrections[subSectionID]; exists {
addParent(corrections, subSectionID, parentID)
} else if _, exists := cleaner.Site.Sections[subSectionID]; !exists {
corrections[subSectionID] = cleaner.getPossibleMatches(subSectionID, parentID)
// No need to test for lessons here. If this sectionID really references a lesson,
// it would already have been converted in the scraper.
// And if it incorrectly references a lesson, that will get picked up in getPossibleMatches, also.
}
}
}
return corrections
}
// addParent registers the given section as referencing the given bad section/lesson ID.
func addParent(corrections map[string]Correction, badID, parentID string) {
correction, _ := corrections[badID]
correction.Parents = append(correction.Parents, parentID)
corrections[badID] = correction
}
// GetEmptyCorrections finds all empty sections (no lessons or subsections) and tries to correct.
func (cleaner *PostScraper) GetEmptyCorrections() map[string]Correction {
corrections := make(map[string]Correction, 10)
for parentID, section := range cleaner.Site.Sections {
for _, subSectionID := range section.Sections {
// Don't try to correct the same thing twice.
if _, exists := corrections[subSectionID]; exists {
addParent(corrections, subSectionID, parentID)
} else if subSection, exists := cleaner.Site.Sections[subSectionID]; exists {
if len(subSection.Sections) == 0 && len(subSection.Lessons) == 0 {
corrections[subSectionID] = cleaner.getPossibleMatches(subSectionID, parentID)
}
}
}
for _, lessonID := range section.Lessons {
// Don't try to correct the same thing twice.
if _, exists := corrections[lessonID]; exists {
addParent(corrections, lessonID, parentID)
} else if lesson, exists := cleaner.Site.Lessons[lessonID]; exists {
if len(lesson.Pdf) == 0 && len(lesson.Audio) == 0 {
corrections[lessonID] = cleaner.getPossibleMatches(lessonID, parentID)
}
}
}
}
return corrections
}
// applyFix fixes up the site based on the correction. If the correction is executed, marked as such.
func (cleaner *PostScraper) applyFix(badID string, correction *Correction) {
if len(correction.Guesses) > 0 && (correction.IsConfirmed || correction.Is404) {
if _, exists := cleaner.Site.Sections[badID]; exists {
delete(cleaner.Site.Sections, badID)
}
if _, exists := cleaner.Site.Lessons[badID]; exists {
delete(cleaner.Site.Lessons, badID)
}
for sectionID, section := range cleaner.Site.Sections {
// Keep track of the good sections.
// "sectionIDs" which actually reference lessons aren't added.
goodSections := make([]string, 0, len(section.Sections))
for _, subSectionID := range section.Sections {
if subSectionID == badID {
if correction.Source == SectionType {
goodSections = append(goodSections, correction.Guesses[0])
} else {
section.Lessons = append(section.Lessons, correction.Guesses[0])
}
} else {
goodSections = append(goodSections, subSectionID)
}
}
// Work on lessons which were converted from sections.
for i, lessonID := range section.Lessons {
if lessonID == badID && correction.Source == LessonType {
section.Lessons[i] = correction.Guesses[0]
}
}
section.Sections = goodSections
cleaner.Site.Sections[sectionID] = section
}
correction.WasCorrected = true
}
}
// Create corrections for a parents reference to a bad ID.
func (cleaner *PostScraper) getPossibleMatches(id, parentID string) Correction {
correction := Correction{
Parents: []string{parentID},
}
if response, err := http.Head(id); err == nil {
if response.StatusCode == http.StatusNotFound {
correction.Is404 = true
}
}
correction.Guesses, correction.Source = cleaner.getPossibleIdsFromSite(id)
if correction.Guesses != nil {
doc1, err := goquery.NewDocument(id)
if err != nil {
fmt.Println("Error in get pos: ", err)
return correction
}
doc2, err := goquery.NewDocument(correction.Guesses[0])
if err != nil {
fmt.Println("Error in get pos: ", err)
return correction
}
content1 := doc1.Find("#main_container")
content2 := doc2.Find("#main_container")
html1, _ := content1.Html()
html2, _ := content2.Html()
if html1 != "" && html1 == html2 {
correction.IsConfirmed = true
}
}
return correction
}
// Searches lessons and sections for matching IDs.
func (cleaner *PostScraper) getPossibleIdsFromSite(id string) ([]string, DataType) {
if matches := getPossibleIDFromSiteDataType(cleaner.Site.Sections, id); matches != nil {
return matches, SectionType
}
if matches := getPossibleIDFromSiteDataType(cleaner.Site.Lessons, id); matches != nil {
return matches, LessonType
}
return nil, 0
}
// Searches one data type (either lessons or sections) for matching IDs.
func getPossibleIDFromSiteDataType(data interface{}, badID string) (matches []string) {
for _, key := range reflect.ValueOf(data).MapKeys() {
if isPossibleMatch(badID, key.String()) {
matches = append(matches, key.String())
}
}
return
}
func isPossibleMatch(badID, testID string) bool {
if badID == testID {
return false
}
badBase := path.Base(badID)
testBase := path.Base(testID)
if badBase == testBase {
return true
} else if strings.Contains(testBase, badBase) || strings.Contains(badBase, testBase) &&
math.Abs(float64(len(testBase))-float64(len(badBase))) < 6 {
return true
}
return false
}
func getBody(url string) string {
response, err := http.Get(url)
if err != nil {
return ""
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return ""
}
return string(body)
}