-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
117 lines (97 loc) · 2.37 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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"github.com/atomotic/go-openuri"
)
const lang = "en"
const separator = "|"
// Manifest ...
type Manifest struct {
ID string `json:"@id"`
Label string `json:"label"`
Attribution string `json:"attribution"`
Description string `json:"description"`
Metadata []struct {
Label interface{} `json:"label"`
Value interface{} `json:"value"`
} `json:"metadata"`
Thumbnail struct {
ID string `json:"@id"`
} `json:"thumbnail"`
}
// Label ...
type Label map[string]interface{}
func getMetadataLabel(label interface{}) (string, error) {
switch label.(type) {
case string:
if label.(string) != "" {
return label.(string), nil
}
return "_Untitled", nil
case []interface{}:
for _, labels := range label.([]interface{}) {
resultLabel := Label(labels.(map[string]interface{}))
if resultLabel["@language"] == lang {
return resultLabel["@value"].(string), nil
} else {
return "", nil
}
}
}
return "", errors.New("errors")
}
func getMetadataValue(value interface{}) (string, error) {
switch value.(type) {
case string:
return value.(string), nil
case []interface{}:
ret := []string{}
for _, values := range value.([]interface{}) {
ret = append(ret, fmt.Sprintf("%s", values))
}
return strings.Join(ret, separator), nil
}
return "", errors.New("errors")
}
func main() {
var manifest Manifest
metadata := make(map[string]string)
if len(os.Args) == 1 {
fmt.Println("USAGE: iiif-flat-metadata {file.json | http://remote.manifest.json}")
os.Exit(2)
}
o, err := openuri.Open(os.Args[1])
if err != nil {
log.Fatal(err)
}
defer o.Close()
m, _ := ioutil.ReadAll(o)
json.Unmarshal(m, &manifest)
metadata["@id"] = manifest.ID
metadata["Label"] = manifest.Label
metadata["Attribution"] = manifest.Attribution
metadata["Description"] = manifest.Description
metadata["Thumbnail"] = manifest.Thumbnail.ID
for _, property := range manifest.Metadata {
label, err := getMetadataLabel(property.Label)
if err != nil {
fmt.Println("err")
}
value, _ := getMetadataValue(property.Value)
if label != "" {
if metadata[label] != "" {
metadata[label] = fmt.Sprintf("%s %s %s", metadata[label], separator, value)
} else {
metadata[label] = value
}
}
}
output, _ := json.Marshal(metadata)
fmt.Println(string(output))
}