Skip to content

Commit 54320cf

Browse files
committed
initial commit
1 parent 1a0b6aa commit 54320cf

File tree

4 files changed

+241
-0
lines changed

4 files changed

+241
-0
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module paperswithcode
2+
3+
go 1.16

internal/client.go

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"errors"
7+
"fmt"
8+
"net/http"
9+
"net/url"
10+
)
11+
12+
type Client struct {
13+
BaseURL string
14+
HTTPClient *http.Client
15+
}
16+
17+
type errorResponse struct {
18+
Code int `json:"code"`
19+
Message string `json:"message"`
20+
}
21+
22+
// Specific Paper's Information by the title
23+
type Paper struct {
24+
ID string `json:"id"`
25+
ArxivID string `json:"arxiv_id,omitempty"`
26+
URLAbs string `json:"url_abs"`
27+
URLPDF string `json:"url_pdf"`
28+
Title string `json:"title"`
29+
Abstract string `json:"abstract"`
30+
Authors []string `json:"authors"`
31+
Published string `json:"published"`
32+
}
33+
34+
func (c *Client) GetPaper(ctx context.Context, id string) (*Paper, error) {
35+
fmt.Println(id)
36+
url := fmt.Sprintf("%s/papers/%s/", c.BaseURL, url.QueryEscape(id))
37+
req, err := http.NewRequest("GET", url, nil)
38+
39+
if err != nil {
40+
return nil, err
41+
}
42+
43+
req = req.WithContext(ctx)
44+
45+
res := Paper{}
46+
if err := c.sendRequest(req, &res); err != nil {
47+
return nil, err
48+
}
49+
50+
return &res, nil
51+
}
52+
53+
// Method list used by Paper's ID
54+
type MethodList struct {
55+
Count int `json:"count"`
56+
Methods []Method `json:"results"`
57+
}
58+
59+
type Method struct {
60+
ID string `json:"id"`
61+
Name string `json:"name"`
62+
FullName string `json:"full_name"`
63+
Description string `json:"description"`
64+
Paper string `json:"paper"`
65+
}
66+
67+
func (c *Client) GetMethodList(ctx context.Context, id string) (*MethodList, error) {
68+
fmt.Println(id)
69+
url := fmt.Sprintf("%s/papers/%s/methods", c.BaseURL, url.QueryEscape(id))
70+
req, err := http.NewRequest("GET", url, nil)
71+
72+
if err != nil {
73+
return nil, err
74+
}
75+
76+
req = req.WithContext(ctx)
77+
78+
res := MethodList{}
79+
if err := c.sendRequest(req, &res); err != nil {
80+
return nil, err
81+
}
82+
83+
return &res, nil
84+
}
85+
86+
// Repository List implementing by Paper's ID
87+
type RepositoryList struct {
88+
Count int `json:"count"`
89+
Results []Repository `json:"results"`
90+
}
91+
92+
type Repository struct {
93+
URL string `json:"url"`
94+
IsOfficial bool `json:"is_official"`
95+
Description string `json:"description"`
96+
Stars int `json:"stars"`
97+
Framework string `json:"framework"`
98+
}
99+
100+
func (c *Client) GetRepositoryList(ctx context.Context, id string) (*RepositoryList, error) {
101+
fmt.Println(id)
102+
url := fmt.Sprintf("%s/papers/%s/repositories", c.BaseURL, url.QueryEscape(id))
103+
req, err := http.NewRequest("GET", url, nil)
104+
105+
if err != nil {
106+
return nil, err
107+
}
108+
109+
req = req.WithContext(ctx)
110+
111+
res := RepositoryList{}
112+
if err := c.sendRequest(req, &res); err != nil {
113+
return nil, err
114+
}
115+
116+
return &res, nil
117+
}
118+
119+
func (c *Client) sendRequest(req *http.Request, v interface{}) error {
120+
req.Header.Set("Content-Type", "application/json; charset=utf-8")
121+
req.Header.Set("Accept", "application/json; charset=utf-8")
122+
123+
res, err := c.HTTPClient.Do(req)
124+
if err != nil {
125+
return err
126+
}
127+
128+
defer res.Body.Close()
129+
130+
if res.StatusCode < http.StatusOK || res.StatusCode >= http.StatusBadRequest {
131+
var errRes errorResponse
132+
if err = json.NewDecoder(res.Body).Decode(&errRes); err == nil {
133+
return errors.New(errRes.Message)
134+
}
135+
136+
return fmt.Errorf("unknown error, status code: %d", res.StatusCode)
137+
}
138+
139+
if err = json.NewDecoder(res.Body).Decode(&v); err != nil {
140+
return err
141+
}
142+
143+
// fmt.Println(fullResponse)
144+
145+
return nil
146+
}
147+
148+
/*
149+
GetPapers API is currently broken. need to wait until it gets fixed
150+
*/
151+
152+
// type PaperList struct {
153+
// Count int `json:"count"`
154+
// NextPage string `json:"next,omitempty"`
155+
// PrePage string `json:"previous,omitempty"`
156+
// Results []struct {
157+
// ID string `json:"id"`
158+
// ArxivID []string `json:"arxiv_id,omitempty"`
159+
// NipsID []string `json:"nips_id,omitempty"`
160+
// URLAbs string `json:"url_abs"`
161+
// URLPdf string `json:"url_pdf"`
162+
// Title string `json:"title"`
163+
// Abstract string `json:"abstract"`
164+
// Authors []string `json:"authors"`
165+
// Published string `json:"published"`
166+
// Conference []string `json:"conference,omitempty"`
167+
// ConferenceURLAbs []string `json:"conference_url_abs,omitempty"`
168+
// ConferenceURLPdf []string `json:"conference_url_pdf,omitempty"`
169+
// Proceeding []string `json:"proceeding,omitempty"`
170+
// } `json:"results"`
171+
// }
172+
173+
// func (c *Client) GetPapers(ctx context.Context, query string, page int, limit int) (*PaperList, error) {
174+
// url := fmt.Sprintf("%s/papers?q=%s&items_per_page=%d&page=%d", c.BaseURL, url.QueryEscape(query), limit, page)
175+
// req, err := http.NewRequest("GET", url, nil)
176+
177+
// if err != nil {
178+
// return nil, err
179+
// }
180+
181+
// req = req.WithContext(ctx)
182+
183+
// res := PaperList{}
184+
// if err := c.sendRequest(req, &res); err != nil {
185+
// return nil, err
186+
// }
187+
188+
// return &res, nil
189+
// }

main

6.21 MB
Binary file not shown.

main.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"strings"
8+
"time"
9+
10+
client "paperswithcode/internal"
11+
)
12+
13+
const (
14+
BASE_URL = "https://paperswithcode.com/api/v1"
15+
)
16+
17+
func NewClient() *client.Client {
18+
return &client.Client{
19+
BaseURL: BASE_URL,
20+
HTTPClient: &http.Client{
21+
Timeout: time.Minute,
22+
},
23+
}
24+
}
25+
26+
func main() {
27+
c := NewClient()
28+
ctx := context.Background()
29+
paper := "generative adversarial networks"
30+
paper = strings.ReplaceAll(paper, " ", "-")
31+
32+
paperList, _ := c.GetPaper(ctx, paper)
33+
fmt.Println(paperList)
34+
fmt.Println()
35+
fmt.Println()
36+
fmt.Println()
37+
38+
methodList, _ := c.GetMethodList(ctx, paper)
39+
fmt.Println(methodList)
40+
fmt.Println()
41+
fmt.Println()
42+
fmt.Println()
43+
44+
repositoryList, _ := c.GetRepositoryList(ctx, paper)
45+
fmt.Println(repositoryList)
46+
fmt.Println()
47+
fmt.Println()
48+
fmt.Println()
49+
}

0 commit comments

Comments
 (0)