/
arxiv.go
180 lines (158 loc) · 3.87 KB
/
arxiv.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package papernet
import (
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"time"
"github.com/bobinette/papernet/errors"
)
var (
arxivURL = "http://export.arxiv.org/api/query"
arxivSummaryPipe = CleaningPipe(
strings.TrimSpace,
OneLine,
strings.TrimSpace,
)
)
func init() {
// Check if arxiv URL is valid
_, err := url.Parse(arxivURL)
if err != nil {
panic(err)
}
}
type ArxivSearch struct {
Q string
IDs []string
Start int
MaxResults int
}
type ArxivResult struct {
Papers []*Paper
Pagination Pagination
}
type ArxivSpider struct {
Client *http.Client
}
func (s *ArxivSpider) Search(search ArxivSearch) (ArxivResult, error) {
// No need to check for error
u, _ := url.Parse(arxivURL)
query := u.Query()
if search.Q != "" {
re, _ := regexp.Compile("[A-Za-z0-9]+")
matches := re.FindAllStringSubmatch(search.Q, -1)
q := make([]string, len(matches))
for i, match := range matches {
q[i] = match[0]
}
query.Add("search_query", fmt.Sprintf("all:%s", strings.Join(q, " AND ")))
}
if len(search.IDs) > 0 {
query.Add("id_list", strings.Join(search.IDs, ","))
}
if search.Start > 0 {
query.Add("start", strconv.Itoa(search.Start))
}
if search.MaxResults > 0 {
query.Add("max_results", strconv.Itoa(search.MaxResults))
}
query.Add("sortBy", "submittedDate")
query.Add("sortOrder", "descending")
u.RawQuery = query.Encode()
if s.Client == nil {
s.Client = &http.Client{Timeout: 20 * time.Second}
}
resp, err := s.Client.Get(u.String())
if err != nil {
return ArxivResult{}, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return ArxivResult{}, err
}
r := struct {
Title string `xml:"title"`
ID string `xml:"id"`
Total struct {
Value uint64 `xml:",chardata"`
} `xml:"totalResults"`
Offset struct {
Value uint64 `xml:",chardata"`
} `xml:"startIndex"`
Limit struct {
Value uint64 `xml:",chardata"`
} `xml:"itemsPerPage"`
Entries []struct {
Title string `xml:"title"`
ID string `xml:"id"`
Summary string `xml:"summary"`
Links []struct {
HRef string `xml:"href,attr"`
Type string `xml:"type,attr"`
} `xml:"link"`
Categories []struct {
Term string `xml:"term,attr"`
} `xml:"category"`
Published time.Time `xml:"published"`
Updated time.Time `xml:"updated"`
} `xml:"entry"`
}{}
err = xml.Unmarshal(data, &r)
if err != nil {
return ArxivResult{}, err
}
papers := make([]*Paper, len(r.Entries))
for i, entry := range r.Entries {
tags := make([]string, 0, len(entry.Categories))
for _, cat := range entry.Categories {
tag, ok := arxivCategories[cat.Term]
if ok {
tags = append(tags, tag)
}
}
// entryID is in the form "http://arxiv.org/abs/1234.5678v5" => extract 1234.5678
urlParts := strings.Split(entry.ID, "/")
arxivID := strings.Split(urlParts[len(urlParts) - 1], "v")[0]
papers[i] = &Paper{
Title: entry.Title,
Summary: arxivSummaryPipe(entry.Summary),
References: []string{
entry.Links[0].HRef, // link to arXiv
entry.Links[1].HRef, // PDF
},
Tags: tags,
CreatedAt: entry.Published,
UpdatedAt: entry.Updated,
ArxivID: arxivID,
}
}
return ArxivResult{
Papers: papers,
Pagination: Pagination{
Total: r.Total.Value,
Limit: r.Limit.Value,
Offset: r.Offset.Value,
},
}, nil
}
func (s *ArxivSpider) Import(url string) (*Paper, error) {
// The url is in the form "http://arxiv.org/pdf/1234.5678v5" => extract 1234.5678
urlParts := strings.Split(url, "/")
arxivID := strings.Split(urlParts[len(urlParts) - 1], "v")[0]
res, err := s.Search(ArxivSearch{
IDs: []string{arxivID},
})
if err != nil {
return nil, err
}
if len(res.Papers) == 0 {
return nil, errors.New(fmt.Sprintf("no paper found for id %s", arxivID), errors.WithCode(http.StatusNotFound))
}
return res.Papers[0], nil
}