-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
143 lines (118 loc) · 3.03 KB
/
main.go
File metadata and controls
143 lines (118 loc) · 3.03 KB
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
package main
import (
"bytes"
"context"
"errors"
"flag"
"fmt"
"log/slog"
"net/url"
"os"
"regexp"
"strconv"
"github.com/Machiel/slugify"
"github.com/PuerkitoBio/goquery"
"github.com/carlmjohnson/requests"
"github.com/sourcegraph/conc"
)
const (
resultsURL = "http://www.internetculturale.it/it/16/search?instance=magindice"
downloadURL = "http://www.internetculturale.it/metaindiceServices/MagExport?id="
output = "./data"
)
func pages(url string) (int, error) {
var body string
err := requests.URL(url).ToString(&body).Fetch(context.Background())
if err != nil {
return 0, err
}
// pagination match, example string: `Pagina 1 di 14.671 (293.410 risultati trovati)`
regexp, _ := regexp.Compile(`Pagina (\d+) di (\d+) \((\d+(\.\d+)?) risultati trovati\)`)
matches := regexp.FindStringSubmatch(body)
if len(matches) >= 3 {
total, err := strconv.ParseFloat(matches[3], 64)
if err != nil {
return 0, err
}
if total == 0 {
return 0, errors.New("0 results")
}
pages, err := strconv.ParseFloat(matches[2], 64)
if err != nil {
return 0, err
}
return int(pages), nil
} else {
return 0, errors.New("0 results")
}
}
func download(oai string) error {
url := fmt.Sprintf("%s%s", downloadURL, oai)
slug := slugify.Slugify(oai)
filename := fmt.Sprintf("%s/%s.xml", output, slug)
err := requests.URL(url).ToFile(filename).Fetch(context.Background())
if err != nil {
return err
}
return nil
}
func main() {
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
var wg conc.WaitGroup
var q string
query := flag.String("query", "", "query string")
queryAll := flag.Bool("all", false, "search all (*)")
biblioType := flag.String("biblio-type", "", "filtery by bibliographic type (eg. 'periodico')")
documentType := flag.String("document-type", "", "filtery by document type (eg. 'manoscritto')")
flag.Parse()
if !*queryAll && *query == "" {
flag.Usage()
os.Exit(1)
}
if _, err := os.Stat(output); os.IsNotExist(err) {
os.Mkdir(output, 0755)
}
if *queryAll {
q = "*"
} else {
q = url.QueryEscape(*query)
}
seed := fmt.Sprintf("%s&q=%s", resultsURL, q)
if *biblioType != "" {
seed = seed + "&__meta_typeLivello=" + *biblioType
}
if *documentType != "" {
seed = seed + "&channel__typeTipo=" + url.QueryEscape(*documentType)
}
logger.Info(seed)
pages, err := pages(seed)
if err != nil {
logger.Warn("0 results")
os.Exit(0)
}
for i := 1; i <= pages; i++ {
url := fmt.Sprintf("%s&pag=%d", seed, i)
var buf bytes.Buffer
err = requests.URL(url).ToBytesBuffer(&buf).Fetch(context.Background())
if err != nil {
logger.Error(err.Error())
}
doc, err := goquery.NewDocumentFromReader(&buf)
if err != nil {
logger.Error(err.Error())
}
doc.Find(".dc_id").Each(func(i int, s *goquery.Selection) {
oai := s.Text()
slug := slugify.Slugify(oai)
wg.Go(func() {
err = download(oai)
if err != nil {
logger.Error(err.Error(), "identifier", oai)
} else {
logger.Info("download", "identifier", oai, "file", slug)
}
})
})
}
wg.Wait()
}