-
Notifications
You must be signed in to change notification settings - Fork 0
/
news.go
143 lines (123 loc) · 3.75 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
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
package main
import (
"encoding/json"
"log"
"os"
"strconv"
"time"
"github.com/bwmarrin/discordgo"
)
var (
HACKER_NEWS_TOP_STORIES_API string = "https://hacker-news.firebaseio.com/v0/topstories.json"
HACKER_NEWS_DETAIL_STORY_API string = "https://hacker-news.firebaseio.com/v0/item/"
HACKER_NEWS_API_EXT string = ".json"
)
var (
NEWS_API_ROOT_URL string = "https://newsapi.org/v2/"
NEWS_API_TYPE_EVERYTHING string = "everything"
NEWS_API_TYPE_TOP_HEADLINES string = "top-headlines"
)
type DetailArticle struct {
By string `json:"by"`
Descendants int `json:"descendants"`
ID int `json:"id"`
Kids []int `json:"kids"`
Score int `json:"score"`
Time int `json:"time"`
Title string `json:"title"`
Type string `json:"type"`
URL string `json:"url"`
}
func GetTopStories() *DetailArticle {
var stories []int
s := ClientRequest(HACKER_NEWS_TOP_STORIES_API, nil, nil) // []int
err := json.Unmarshal(s, &stories)
if err != nil {
log.Println("top stories error: ", err)
}
num := RandomInt(len(stories), 0)
detailArticleURL := HACKER_NEWS_DETAIL_STORY_API + strconv.Itoa(stories[num]) + HACKER_NEWS_API_EXT
article := ClientRequest(detailArticleURL, nil, nil)
detailArticle := &DetailArticle{}
err = json.Unmarshal(article, detailArticle)
if err != nil {
log.Println("top stories error: ", err)
}
return detailArticle
}
func HackerNews() func(*discordgo.Session, *discordgo.InteractionCreate) {
return func(s *discordgo.Session, i *discordgo.InteractionCreate) {
article := GetTopStories()
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Title: "Hacker News",
Content: "Hacker News by " + article.By + "\n" + article.URL,
},
})
if err != nil {
log.Printf("get news error: " + err.Error())
return
}
}
}
type NewsAPIResponse struct {
Status string `json:"status"`
TotalResults int `json:"totalResults"`
Articles []struct {
Source struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"source"`
Author string `json:"author"`
Title string `json:"title"`
Description string `json:"description"`
URL string `json:"url"`
URLToImage string `json:"urlToImage"`
PublishedAt time.Time `json:"publishedAt"`
Content string `json:"content"`
} `json:"articles"`
}
var (
newsAPIParams = map[string]string{
"page": "1",
"pageSize": "1",
"q": "football",
"sortBy": "publishedAt",
}
)
func GetNewsTypeEverything(apiKey string) *NewsAPIResponse {
url := NEWS_API_ROOT_URL + NEWS_API_TYPE_EVERYTHING
newsAPIResponse := &NewsAPIResponse{}
newAPIHeaders := map[string][]string{
"x-api-key": {apiKey},
}
//Get yesterday's date
yesterday := time.Now().Add(-24 * time.Hour).Format(time.DateOnly)
if _, ok := newsAPIParams["from"]; !ok {
newsAPIParams["from"] = yesterday
}
s := ClientRequest(url, newAPIHeaders, newsAPIParams) //
err := json.Unmarshal(s, newsAPIResponse)
if err != nil {
log.Println("news api error: ", err)
}
return newsAPIResponse
}
func CronHackerNews() {
article := GetTopStories()
s.ChannelMessageSend(*channelID, "===News===\nAuthor: "+article.By+"\nTitle: "+article.Title+"\nLink: "+article.URL)
}
func CronGameNews() {
apiKey := os.Getenv("NEWS_API_KEY")
if apiKey == "" {
log.Println("cannot get news api key")
return
}
news := GetNewsTypeEverything(apiKey)
if len(news.Articles) == 0 {
log.Println("article not found")
return
}
s.ChannelMessageSend(*channelID, "===News===\nAuthor: "+news.Articles[0].Author+"\nTitle: "+news.Articles[0].Title+"\nLink: "+news.Articles[0].URL)
}