-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.go
104 lines (93 loc) · 3.04 KB
/
search.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
package cinii
import (
"encoding/xml"
"fmt"
"html"
"io/ioutil"
"net/http"
"net/url"
"time"
)
// OpenSaerchEndpoint は、CiNii Books図書・雑誌書誌検索のOpenSearchのURI
const OpenSaerchEndpoint = "http://ci.nii.ac.jp/books/opensearch/search"
// AtomFeed はAtom1.0レスポンス構造体
type AtomFeed struct {
XMLName xml.Name `xml:"http://www.w3.org/2005/Atom feed"`
Title string `xml:"http://www.w3.org/2005/Atom title"`
Links []struct {
Rel string `xml:"rel,attr"`
Type string `xml:"type,attr"`
Href string `xml:"href,attr"`
} `xml:"http://www.w3.org/2005/Atom link"`
ID string `xml:"http://www.w3.org/2005/Atom id"`
Updated customTime `xml:"http://www.w3.org/2005/Atom updated"`
TotalResults int `xml:"http://a9.com/-/spec/opensearch/1.1/ totalResults"`
StartIndex int `xml:"http://a9.com/-/spec/opensearch/1.1/ startIndex"`
ItemsPerPage int `xml:"http://a9.com/-/spec/opensearch/1.1/ itemsPerPage"`
Entries []Entry `xml:"http://www.w3.org/2005/Atom entry"`
}
// HTMLLink はAtomFeedからHTML Linkを返すメソッド
func (f *AtomFeed) HTMLLink() (link string, err error) {
link = html.UnescapeString(f.Links[0].Href)
link, err = url.QueryUnescape(link)
return
}
// Entry はAtomFeedのエントリ構造体
type Entry struct {
Title string `xml:"http://www.w3.org/2005/Atom title"`
ID string `xml:"http://www.w3.org/2005/Atom id"`
Authors []struct {
Name string `xml:"http://www.w3.org/2005/Atom name"`
} `xml:"http://www.w3.org/2005/Atom author"`
Publisher string `xml:"http://purl.org/dc/elements/1.1/ publisher"`
PubDate string `xml:"http://prismstandard.org/namespaces/basic/2.0/ publicationDate"`
IsPartOf []struct {
Title string `xml:"title,attr"`
Link string `xml:",chardata"`
} `xml:"http://purl.org/dc/terms/ isPartOf"`
HasPart []string `xml:"http://purl.org/dc/terms/ hasPart"`
OwnerCount int `xml:"http://ci.nii.ac.jp/ns/1.0/ ownerCount"`
}
type customTime struct {
time.Time
}
func (c *customTime) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var v string
d.DecodeElement(&v, &start)
//parse, err := time.Parse("2006-01-02T15:04:05-0700", v)
// RFC3339: 2006-01-02T15:04:05-07:00
parse, err := time.Parse(time.RFC3339, v)
if err != nil {
return err
}
*c = customTime{parse}
return nil
}
// Search はCiniiBooksをOpenSearchで検索する
func Search(q url.Values) (*AtomFeed, error) {
url := fmt.Sprintf("%s?%s", OpenSaerchEndpoint, q.Encode())
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
feed, err := ParseAtomFeed(body)
if err != nil {
return nil, err
}
return feed, nil
}
// ParseAtomFeed はAtomFeedを含むbyte[]を受け取りAtomFeed構造体のポインタで返す関数
func ParseAtomFeed(body []byte) (*AtomFeed, error) {
// 取得したデータをXMLデコード
feed := &AtomFeed{}
err := xml.Unmarshal(body, feed)
if err != nil {
return nil, err
}
return feed, nil
}