/
info.go
210 lines (164 loc) · 4.91 KB
/
info.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package requests
import (
"bufio"
"fmt"
"log"
"net/http"
"os"
"github.com/tempor1s/gonyx/message"
"github.com/bwmarrin/discordgo"
"github.com/gocolly/colly"
)
// GetXurInfo gets xur information and sends to to the given channel
func GetXurInfo(session *discordgo.Session, channelID string, task bool) {
if task == false {
tempMessage, _ := session.ChannelMessageSend(channelID, "Fetching latest info...")
defer message.DeleteMessage(session, channelID, tempMessage.ID)
}
c := colly.NewCollector()
var xurURL []string
c.OnHTML("img", func(e *colly.HTMLElement) {
xurURL = append(xurURL, e.Attr("data-src"))
})
if err := c.Visit("https://www.niris.tv/blog/xurs-wares"); err != nil {
log.Println("Error occurred when scraping Xur's Wares.")
}
var sameUrl bool
if task {
sameUrl = CompareUrls("xur.txt", xurURL)
} else {
sameUrl = false
}
if sameUrl == false && task {
message.DeleteMessagesWithAttachment(session, channelID, "xur.png")
}
// Get the response from the URL
resp, err := http.Get(xurURL[0])
// Make sure that we get a response so we dont have nil errors.
if err != nil {
message.SendMessage(session, channelID, "Error fetching Xur info. Please try again :)")
return
}
if sameUrl == false {
message.SendFile(session, channelID, "xur.png", resp.Body)
}
}
// GetWeeklyInfo gets weekly information and sends to to the given channel
func GetWeeklyInfo(session *discordgo.Session, channelID string, task bool) {
if task == false {
tempMessage, _ := session.ChannelMessageSend(channelID, "Fetching latest info...")
defer message.DeleteMessage(session, channelID, tempMessage.ID)
}
// TODO: Clean this up
// TODO: Set up caching or logging in database to speed this up most of the time.
// Setup web scraper collector and base struct
c := colly.NewCollector()
var weeklyURL []string
c.OnHTML("img", func(e *colly.HTMLElement) {
weeklyURL = append(weeklyURL, e.Attr("data-src"))
})
// Start the web scraper
if err := c.Visit("https://www.niris.tv/blog/weekly-reset"); err != nil {
log.Println("Error occurred when scraping Weekly Reset.")
}
var sameUrls bool
if task {
sameUrls = CompareUrls("weekly.txt", weeklyURL)
} else {
sameUrls = false
}
if sameUrls == false && task {
message.DeleteMessagesWithAttachment(session, channelID, "weekly.png")
}
// Send all the URLS :)
for _, url := range weeklyURL {
resp, err := http.Get(url)
// Make sure that we get a response so we dont have nil errors.
if err != nil {
msg := fmt.Sprintf("Error fetching weekly info from `%s` Please try again :)", url)
message.SendMessage(session, channelID, msg)
return
}
if sameUrls == false {
message.SendFile(session, channelID, "weekly.png", resp.Body)
}
}
}
func GetTrialsInfo(session *discordgo.Session, channelID string, task bool) {
if task == false {
tempMessage, _ := session.ChannelMessageSend(channelID, "Fetching latest info...")
defer message.DeleteMessage(session, channelID, tempMessage.ID)
}
c := colly.NewCollector()
var trialsURL []string
c.OnHTML("img", func(e *colly.HTMLElement) {
trialsURL = append(trialsURL, e.Attr("data-src"))
})
if err := c.Visit("https://www.niris.tv/blog/trials"); err != nil {
log.Println("Error occurred when scraping trials info")
}
var sameUrl bool
if task {
sameUrl = CompareUrls("trials.txt", trialsURL)
} else {
sameUrl = false
}
if sameUrl == false && task {
message.DeleteMessagesWithAttachment(session, channelID, "trials.png")
}
// Get the response from the URL
resp, err := http.Get(trialsURL[0])
// Make sure that we get a response so we dont have nil errors.
if err != nil {
message.SendMessage(session, channelID, "Error fetching Xur info. Please try again :)")
return
}
if sameUrl == false {
message.SendFile(session, channelID, "trials.png", resp.Body)
}
}
func CompareUrls(fileName string, urls []string) bool {
if fileExists(fileName) == false {
writeLinks(fileName, urls)
return false
}
file, err := os.Open(fileName)
if err != nil {
log.Fatal(err)
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
for i, line := range lines {
if urls[i] != line {
os.Remove(fileName)
writeLinks(fileName, urls)
return false
}
}
fmt.Printf("URLS in file '%s' are the same. Skipping...\n", fileName)
return true
}
func writeLinks(fileName string, urls []string) {
file, err := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatalf("failed creating or reading file: %s", err)
}
defer file.Close()
dataWriter := bufio.NewWriter(file)
defer dataWriter.Flush()
for _, url := range urls {
_, _ = dataWriter.WriteString(url + "\n")
}
log.Printf("Successfully wrote URLS to file %q\n", fileName)
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}