/
vatican_parser.go
215 lines (179 loc) · 6.45 KB
/
vatican_parser.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
package main
import (
"fmt"
md "github.com/JohannesKaufmann/html-to-markdown"
"github.com/JohannesKaufmann/html-to-markdown/plugin"
"github.com/gocolly/colly"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"time"
)
func main() {
// parsePopes()
parseCommissions()
}
func parseCommissions() {
c := getCollector()
c.AllowedDomains = []string{"vatican.va", "www.vatican.va"}
// Parcourir tous les dicastères
c.OnHTML("#accordionmenu a" , func(e *colly.HTMLElement) {
// fmt.Println("Dicastère:", strings.TrimSpace(e.Text))
e.Request.Visit(e.Attr("href"))
})
// Page listant les "Documents"
c.OnHTML(".documenti h2 > a", func(e *colly.HTMLElement) {
e.Request.Visit(e.Attr("href"))
})
// Documents en Français
c.OnHTML(".documento > .testo > .text ul > li b > a", func(e *colly.HTMLElement) {
// docUrl := e.Request.URL.String()
fmt.Println("Document:", e.Text)
e.Request.Visit(e.Attr("href"))
})
// Documents en Latin
c.OnHTML(".documento > .testo > .text > ul > li > a", func(e *colly.HTMLElement) {
if ("Latin" == e.Text) {
fmt.Println("Document latin:", e.Text)
e.Request.Visit(e.Attr("href"))
}
})
converter := getConverter()
c.OnHTML(".documento .testo", func(e *colly.HTMLElement) {
docUrl := e.Request.URL.String()
docContent := converter.Convert(e.DOM)
fileName := getFileName(docUrl) // 1901-06-29-en-tout-temps.md
filePath := getFilePath(docUrl) // leo-xiii/fr/letters/documents
os.MkdirAll(filePath, 0750)
// fmt.Println("fileName: ", fileName)
if err := os.WriteFile(filepath.Join(filePath, fileName), []byte(docContent), 0666); err != nil {
fmt.Println(err)
}
})
c.Visit(curie)
}
func parsePopes() {
c := getCollector()
c.AllowedDomains = []string{"vatican.va", "www.vatican.va"}
// Parcourir tous les papes
c.OnHTML("#corpo > table > tbody > tr:nth-child(2) > td > table > tbody > tr:nth-child(2) > td:nth-child(1) > table > tbody", func(e *colly.HTMLElement) {
e.ForEach("tr td a", func(_ int, el *colly.HTMLElement) {
fmt.Println("Pape:", el.Text)
el.Request.Visit(el.Attr("href"))
})
})
// Parcourir tous les types de documents
c.OnHTML("#accordionmenu > ul > li", func(e *colly.HTMLElement) {
//fmt.Println("Doc type:", e.ChildText("a[0]"))
e.Request.Visit(e.ChildAttr("a", "href"))
// Parcourir les sous sections (années et mois) d'un type de document
e.ForEach("ul li a", func(_ int, el *colly.HTMLElement) {
//fmt.Println("subDoc type:", strings.TrimSpace(e.Text))
el.Request.Visit(el.Attr("href"))
})
})
// Parcourir les documents d'un type donné
c.OnHTML(".vaticanindex h1 a", func(e *colly.HTMLElement) {
e.Request.Visit(e.Attr("href"))
})
c.OnHTML(".vaticanindex h2 a", func(e *colly.HTMLElement) {
if ("Latin" == e.Text) {
e.Request.Visit(e.Attr("href"))
}
})
converter := getConverter()
c.OnHTML(".documento .testo", func(e *colly.HTMLElement) {
docUrl := e.Request.URL.String()
docContent := converter.Convert(e.DOM)
fileName := getFileName(docUrl) // 1901-06-29-en-tout-temps.md
filePath := getFilePath(docUrl) // leo-xiii/fr/letters/documents
os.MkdirAll(filePath, 0750)
// fmt.Println("fileName: ", fileName)
if err := os.WriteFile(filepath.Join(filePath, fileName), []byte(docContent), 0666); err != nil {
fmt.Println(err)
}
})
// Parse all eleven previous popes :
c.Visit(popes)
}
var (
reName = regexp.MustCompile(`([0-9]{8})(.*)`)
// 1 : pope name and doc type (skipped as already in the path)
// 2 : doc date
// 3 : _
// 4 : doc name
reDate = regexp.MustCompile(`([0-9]{8})`)
popes = "https://www.vatican.va/holy_father/index_fr.htm"
curie = "https://www.vatican.va/content/romancuria/fr/segreteria-di-stato/segreteria-di-stato.index.html"
)
func getCollector() *colly.Collector {
c := colly.NewCollector()
c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL)
})
c.OnError(func(r *colly.Response, err error) {
fmt.Println("Request URL:", r.Request.URL, "failed with response:", r, "\nError:", err)
})
return c
}
func getConverter() *md.Converter {
opt := &md.Options{
CodeBlockStyle: "fenced", // default: indented
EmDelimiter: "*", // default: _
}
converter := md.NewConverter("", true, opt)
converter.AddRules(getPunctuationRule())
converter.Use(plugin.GitHubFlavored())
return converter
}
func getOriginalName(url string) string {
// url example : https://www.vatican.va/content/leo-xiii/fr/letters/documents/hf_l-xiii_let_19010629_en-tout-temps.html
baseUrl := path.Base(url) // hf_l-xiii_let_19010629_en-tout-temps.html
return strings.TrimSuffix(baseUrl, filepath.Ext(baseUrl)) // hf_l-xiii_let_19010629_en-tout-temps
}
func getDocName(url string) string {
matchedName := reName.FindStringSubmatch(getOriginalName(url))
if (2 > len(matchedName)) {
return ""
} else {
return strings.Trim(matchedName[2], "-_")
}
}
func getDocDate(url string) string {
dateStringInURL := reDate.FindString(url)
if ("" != dateStringInURL) {
docDate, _ := time.Parse("20060102", dateStringInURL)
if ("0001-01-01" == docDate.Format("2006-01-02")) {
docDate, _ = time.Parse("02012006", dateStringInURL)
}
return docDate.Format("2006-01-02")
} else {
return ""
}
}
func getFileName(url string) string {
ext := ".md"
if (strings.Contains(url, "/la/")) {
ext = ".latin.md"
}
docName := getDocName(url)
docDate := getDocDate(url)
if ("" == docName && "" == docDate) {
return getOriginalName(url) + ext
}
if ("" == docName) {
return docDate + ext
}
if ("" == docDate) {
return docName + ext
}
return docDate + "-" + docName + ext
}
func getFilePath(docUrl string) string {
cleanedPath := strings.TrimSuffix(path.Dir(docUrl), "documents")
cleanedPath = strings.Replace(cleanedPath, "/fr/", "/", 1)
cleanedPath = strings.Replace(cleanedPath, "/la/", "/", 1)
return strings.TrimPrefix(cleanedPath, "https:/www.vatican.va/content/")
}