This repository has been archived by the owner on Sep 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
news.go
55 lines (46 loc) · 1.55 KB
/
news.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
package crunchyroll
import (
"encoding/json"
"fmt"
"net/http"
)
// News returns the top and latest news from crunchyroll for the current locale within the given limits.
func (c *Crunchyroll) News(topLimit uint, latestLimit uint) (t []*News, l []*News, err error) {
newsFeedEndpoint := fmt.Sprintf("https://beta-api.crunchyroll.com/content/v1/news_feed?top_news_n=%d&latest_news_n=%d&locale=%s",
topLimit, latestLimit, c.Locale)
resp, err := c.request(newsFeedEndpoint, http.MethodGet)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
var jsonBody map[string]interface{}
if err = json.NewDecoder(resp.Body).Decode(&jsonBody); err != nil {
return nil, nil, fmt.Errorf("failed to parse 'news_feed' response: %w", err)
}
topNews := jsonBody["top_news"].(map[string]interface{})
for _, item := range topNews["items"].([]interface{}) {
topNews := &News{}
if err := decodeMapToStruct(item, topNews); err != nil {
return nil, nil, err
}
t = append(t, topNews)
}
latestNews := jsonBody["latest_news"].(map[string]interface{})
for _, item := range latestNews["items"].([]interface{}) {
latestNews := &News{}
if err := decodeMapToStruct(item, latestNews); err != nil {
return nil, nil, err
}
l = append(l, latestNews)
}
return t, l, nil
}
// News contains all information about news.
type News struct {
Title string `json:"title"`
Link string `json:"link"`
Image string `json:"image"`
Creator string `json:"creator"`
PublishDate string `json:"publish_date"`
Description string `json:"description"`
}