-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnyaaAPI.go
65 lines (58 loc) · 1.67 KB
/
nyaaAPI.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
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"strings"
)
const (
nyaaUser = ""
nyaaPW = ""
)
type nyaaJSON struct {
CreatedOn string `json:"creation_date"`
Description string `json:"description"`
//Files []string `json:"files"`
FileSize int `json:"filesize"`
HashB32 string `json:"hash_b32"`
HashHex string `json:"hash_hex"`
ID int `json:"id"`
Info string `json:"information"`
Complete bool `json:"is_complete"`
Remake bool `json:"is_remake"`
Trusted bool `json:"is_trusted"`
Magnet string `json:"magnet"`
MainCategory string `json:"main_category"`
MainCategoryID int `json:"main_category_id"`
Name string `json:"name"`
Stats nyaaStats `json:"stats"`
SubCategory string `json:"sub_category"`
SubCategoryID int `json:"sub_category_id"`
Uploader string `json:"submitter"`
URL string `json:"url"`
}
//Stats is an internal struct in the NyaaJSON struct
type nyaaStats struct {
Downloads int `json:"downloads"`
Leechers int `json:"leechers"`
Seeders int `json:"seeders"`
}
func nyaaAPI(url string) (n nyaaJSON, err error) {
apiInfo := `/api/info/`
page := strings.Split(url, "/view/")
req, err := http.NewRequest("GET", page[0]+apiInfo+page[1], nil)
if err != nil {
return
}
//Set auth credentials
req.SetBasicAuth(nyaaUser, nyaaPW)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
//Read that garbage, then unwrap it into a usable struct
b, _ := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(b, &n)
return
}