This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
forked from darenliang/jikan-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
100 lines (89 loc) · 2.45 KB
/
common.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
package jikan
import (
"encoding/json"
"github.com/aerogo/http/client"
"time"
)
type News struct {
Articles []struct {
URL string `json:"url"`
Title string `json:"title"`
Date time.Time `json:"date"`
AuthorName string `json:"author_name"`
AuthorURL string `json:"author_url"`
ForumURL string `json:"forum_url"`
ImageURL string `json:"image_url"`
Comments int `json:"comments"`
Intro string `json:"intro"`
} `json:"articles"`
}
type Pictures struct {
Pictures []struct {
Large string `json:"large"`
Small string `json:"small"`
} `json:"pictures"`
}
type Forum struct {
Topics []struct {
TopicID int `json:"topic_id"`
URL string `json:"url"`
Title string `json:"title"`
DatePosted time.Time `json:"date_posted"`
AuthorName string `json:"author_name"`
AuthorURL string `json:"author_url"`
Replies int `json:"replies"`
LastPost struct {
URL string `json:"url"`
AuthorName string `json:"author_name"`
AuthorURL string `json:"author_url"`
DatePosted time.Time `json:"date_posted"`
} `json:"last_post"`
} `json:"topics"`
}
type Moreinfo struct {
Moreinfo string `json:"moreinfo"`
}
type Recommendations struct {
Recommendations []struct {
MalID int `json:"mal_id"`
URL string `json:"url"`
ImageURL string `json:"image_url"`
RecommendationURL string `json:"recommendation_url"`
Title string `json:"title"`
RecommendationCount int `json:"recommendation_count"`
} `json:"recommendations"`
}
func getNews(url string) (News, error) {
result := News{}
err := ParseResults(url, &result)
return result, err
}
func getPictures(url string) (Pictures, error) {
result := Pictures{}
err := ParseResults(url, &result)
return result, err
}
func getForum(url string) (Forum, error) {
result := Forum{}
err := ParseResults(url, &result)
return result, err
}
func getMoreinfo(url string) (Moreinfo, error) {
result := Moreinfo{}
err := ParseResults(url, &result)
return result, err
}
func getRecommendations(url string) (Recommendations, error) {
result := Recommendations{}
err := ParseResults(url, &result)
return result, err
}
func ParseResults(url string, obj interface{}) error {
response, err := client.Get(url).End()
if err != nil {
return err
}
jsonString := response.String()
err = json.Unmarshal([]byte(jsonString), obj)
return err
}