-
Notifications
You must be signed in to change notification settings - Fork 13
/
repository.go
74 lines (69 loc) · 1.62 KB
/
repository.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
package metha
import (
"encoding/base64"
"io/ioutil"
"strings"
)
// Repository represents an OAI endpoint.
type Repository struct {
BaseURL string
}
// Formats returns a list of metadata formats.
func (r Repository) Formats() ([]MetadataFormat, error) {
var formats []MetadataFormat
var token string
for {
req := Request{BaseURL: r.BaseURL, Verb: "ListMetadataFormats", ResumptionToken: token}
resp, err := Do(&req)
if err != nil {
return nil, err
}
formats = append(formats, resp.ListMetadataFormats.MetadataFormat...)
if !resp.HasResumptionToken() {
break
}
token = resp.GetResumptionToken()
}
return formats, nil
}
// Sets returns a list of sets.
func (r Repository) Sets() ([]Set, error) {
var sets []Set
var token string
for {
req := Request{BaseURL: r.BaseURL, Verb: "ListSets", ResumptionToken: token}
resp, err := Do(&req)
if err != nil {
return nil, err
}
sets = append(sets, resp.ListSets.Set...)
if !resp.HasResumptionToken() {
break
}
token = resp.GetResumptionToken()
}
return sets, nil
}
// FindRepositoriesByString returns a list of already harvested base URLs given a
// fragment of the base URL.
func FindRepositoriesByString(s string) (urls []string, err error) {
files, err := ioutil.ReadDir(BaseDir)
if err != nil {
return urls, err
}
for _, file := range files {
b, err := base64.RawURLEncoding.DecodeString(file.Name())
if err != nil {
return urls, err
}
parts := strings.SplitN(string(b), "#", 3)
if len(parts) < 3 {
continue
}
baseURL := parts[2]
if strings.Contains(baseURL, s) {
urls = append(urls, baseURL)
}
}
return urls, nil
}