-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
186 lines (154 loc) · 3.94 KB
/
main.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
package main
import (
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/cheggaaa/pb/v3"
"github.com/gammazero/workerpool"
)
const (
base string = "https://manus.iccu.sbn.it"
fondslist string = "opac_ElencoSchedeDiUnFondo.php"
itempage string = "opac_SchedaScheda.php"
interstitial string = "Backoffice/XML/index_immediato.php"
download string = "Backoffice/XML/dl.php"
)
var (
fonds = flag.Int("fonds-id", 0, "fonds identifier")
)
func unique(s []string) []string {
unique := make(map[string]bool, len(s))
us := make([]string, len(unique))
for _, elem := range s {
if len(elem) != 0 {
if !unique[elem] {
us = append(us, elem)
unique[elem] = true
}
}
}
return us
}
// getPages parse a fonds page to get pagination and items numbers
// example url: https://manus.iccu.sbn.it/opac_ElencoSchedeDiUnFondo.php?ID=485
func getPages(url string) (pages int, items int, err error) {
resp, err := http.Get(url)
if err != nil {
return 0, 0, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
regexp, _ := regexp.Compile(`Pagina (\d+) di (\d+\.?\d*) \(occorrenze (\d+)\)`)
results := regexp.FindStringSubmatch(string(body))
if len(results) > 0 {
pages, _ := strconv.Atoi(results[2])
items, _ := strconv.Atoi(results[3])
return pages, items, nil
}
return 0, 0, errors.New("no results")
}
// getIds returns a unique lists of identifiers for each page of a fonds
// example url: https://manus.iccu.sbn.it/opac_ElencoSchedeDiUnFondo.php?ID=485&page=2
func getIds(url string) (ids []string) {
res, err := http.Get(url)
if err != nil {
log.Println(err)
}
defer res.Body.Close()
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Println(err)
}
doc.Find("a.opac_linkNero").Each(func(i int, s *goquery.Selection) {
ix, _ := s.Attr("href")
id := strings.Split(ix, "=")
ids = append(ids, id[1])
})
return unique(ids)
}
// downloadXML saves a TEI xml file of an item
func downloadXML(item string, bar *pb.ProgressBar) func() {
return func() {
input := fmt.Sprintf("%s/%s?ID=%s", base, itempage, item)
res, err := http.Get(input)
if err != nil {
log.Println(err)
return
}
defer res.Body.Close()
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Println(err)
return
}
filename, _ := doc.Find("input[name=filename]").First().Attr("value")
autore, _ := doc.Find("input[name=autore]").First().Attr("value")
if filename != "" {
res, err = http.PostForm(fmt.Sprintf("%s/%s", base, interstitial),
url.Values{"op": {"manos"},
"cnmdManos": {item},
"autore": {autore},
"filename": {filename},
})
if err != nil {
log.Println(err)
return
}
defer res.Body.Close()
out, err := os.Create(fmt.Sprintf("%s/%s", "./manus-data", filename))
if err != nil {
log.Println(err)
return
}
defer out.Close()
_, err = io.Copy(out, res.Body)
if err != nil {
log.Println(err)
return
}
} else {
log.Printf("error on item: %s", item)
}
bar.Increment()
}
}
func main() {
wp := workerpool.New(8)
// use insecure https - to test with mimtproxy
// http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
flag.Parse()
if *fonds == 0 {
flag.PrintDefaults()
os.Exit(0)
}
if _, err := os.Stat("./manus-data"); os.IsNotExist(err) {
os.Mkdir("./manus-data", 0755)
}
fondo := fmt.Sprintf("%s/%s?ID=%d", base, fondslist, *fonds)
p, i, err := getPages(fondo)
if err != nil {
log.Println(err)
os.Exit(0)
}
fmt.Printf("# fonds: %d — pages: %d — items: %d\n", *fonds, p, i)
bar := pb.StartNew(i)
for i := 0; i < p; i++ {
p0 := fmt.Sprintf("%s/%s?ID=%d&page=%d", base, fondslist, *fonds, i)
ids := getIds(p0)
for _, item := range ids {
wp.Submit(downloadXML(item, bar))
}
}
wp.StopWait()
bar.Finish()
}