-
Notifications
You must be signed in to change notification settings - Fork 7
/
data.go
46 lines (40 loc) · 1.3 KB
/
data.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
package imslp
import (
"encoding/base64"
"fmt"
"github.com/beevik/etree"
"github.com/miku/span/formats/finc"
)
// SourceIdentifier of IMSLP.
const SourceIdentifier = "15"
// Data is just the raw bytes.
type Data []byte
// UnmarshalText unmarshals textual representation of itself.
func (data *Data) UnmarshalText(text []byte) error {
*data = append(*data, text...)
return nil
}
// ToIntermediateSchema converts record to intermediate schema.
func (data *Data) ToIntermediateSchema() (*finc.IntermediateSchema, error) {
doc := etree.NewDocument()
if err := doc.ReadFromBytes([]byte(*data)); err != nil {
return nil, err
}
output := finc.NewIntermediateSchema()
output.SourceID = SourceIdentifier
for _, t := range doc.FindElements("//var/recordId") {
encoded := base64.RawURLEncoding.EncodeToString([]byte(t.Text()))
output.ID = fmt.Sprintf("ai-%s-%s", SourceIdentifier, encoded)
output.RecordID = t.Text()
}
for _, t := range doc.FindElements("//var[@name='Work Title']/string") {
output.ArticleTitle = t.Text()
}
for _, t := range doc.FindElements("//var[@name='permlink']/string") {
output.URL = append(output.URL, t.Text())
}
for _, t := range doc.FindElements("//var[@name='composer']/string") {
output.Authors = append(output.Authors, finc.Author{Name: t.Text()})
}
return output, nil
}