scraping desired tags
This commit is contained in:
parent
afada3e970
commit
96a33f61d7
98
main.go
98
main.go
@ -114,38 +114,84 @@ func scrapeNews() string {
|
||||
return html
|
||||
}
|
||||
|
||||
// Add a function to extract relevant tags from the HTML content
|
||||
func extractTags(html string) []string {
|
||||
var tags []string
|
||||
doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
|
||||
if err != nil {
|
||||
fmt.Println("Error parsing HTML:", err)
|
||||
return tags
|
||||
}
|
||||
// Find and extract tags with class name "news-module-feed-item-details-tag"
|
||||
doc.Find("span.news-module-feed-item-details-tag").Each(func(i int, s *goquery.Selection) {
|
||||
tag := strings.TrimSpace(s.Text())
|
||||
tags = append(tags, tag)
|
||||
})
|
||||
return tags
|
||||
}
|
||||
|
||||
// Define a map to associate each desired tag with its corresponding Discord channel ID
|
||||
var tagChannelMap = map[string]string{
|
||||
"Patch Notes": "patch-notes-channel-id",
|
||||
"Atomic Shop": "atomic-shop-channel-id",
|
||||
"News": "news-channel-id",
|
||||
// Add more tags and corresponding channel IDs as needed
|
||||
}
|
||||
|
||||
// Function to scrape the webpage and send a message if the date matches
|
||||
func sendNotifications(session *discordgo.Session, channelID string, html string) {
|
||||
// Parse the HTML content
|
||||
doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
|
||||
if err != nil {
|
||||
fmt.Println("Error parsing HTML:", err)
|
||||
return
|
||||
}
|
||||
// Extract tags from the HTML content
|
||||
tags := extractTags(html)
|
||||
|
||||
// Find and extract the date from the webpage
|
||||
var foundDate bool
|
||||
doc.Find("span.news-module-feed-item-details-date").Each(func(i int, s *goquery.Selection) {
|
||||
dateString := strings.TrimSpace(s.Text())
|
||||
fmt.Println("Found date:", dateString)
|
||||
if dateString == time.Now().Format("January 2, 2006") {
|
||||
foundDate = true
|
||||
}
|
||||
})
|
||||
// Define the desired tags to filter for
|
||||
desiredTags := []string{"Patch Notes", "Atomic Shop", "News"}
|
||||
|
||||
// If today's date was found on the webpage, send a message to the Discord channel
|
||||
if foundDate {
|
||||
_, err := session.ChannelMessageSend(channelID, "Today's date was found on the Fallout news page!")
|
||||
if err != nil {
|
||||
fmt.Println("Error sending message to Discord:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("Message sent to Discord channel")
|
||||
} else {
|
||||
fmt.Println("Today's date not found on the Fallout news page")
|
||||
}
|
||||
// Check if any of the desired tags are present in the extracted tags
|
||||
for _, tag := range tags {
|
||||
for _, desiredTag := range desiredTags {
|
||||
if tag == desiredTag {
|
||||
// If the desired tag is found, send a message to the Discord channel
|
||||
_, err := session.ChannelMessageSend(channelID, fmt.Sprintf("Found tag: %s", tag))
|
||||
if err != nil {
|
||||
fmt.Println("Error sending message to Discord:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("Message sent to Discord channel")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Parse the HTML content
|
||||
doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
|
||||
if err != nil {
|
||||
fmt.Println("Error parsing HTML:", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Find and extract the date from the webpage
|
||||
var foundDate bool
|
||||
doc.Find("span.news-module-feed-item-details-date").Each(func(i int, s *goquery.Selection) {
|
||||
dateString := strings.TrimSpace(s.Text())
|
||||
fmt.Println("Found date:", dateString)
|
||||
if dateString == time.Now().Format("January 2, 2006") {
|
||||
foundDate = true
|
||||
}
|
||||
})
|
||||
|
||||
// If today's date was found on the webpage, send a message to the Discord channel
|
||||
if foundDate {
|
||||
_, err := session.ChannelMessageSend(channelID, "Today's date was found on the Fallout news page!")
|
||||
if err != nil {
|
||||
fmt.Println("Error sending message to Discord:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("Message sent to Discord channel")
|
||||
} else {
|
||||
fmt.Println("Today's date not found on the Fallout news page")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// This function will be called whenever a new message is created
|
||||
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
// Ignore messages created by the bot itself
|
||||
|
Loading…
Reference in New Issue
Block a user