scraping desired tags

This commit is contained in:
Chris kerr 2024-04-26 21:59:05 -04:00
parent afada3e970
commit 96a33f61d7

46
main.go
View File

@ -114,8 +114,53 @@ func scrapeNews() string {
return html 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 // Function to scrape the webpage and send a message if the date matches
func sendNotifications(session *discordgo.Session, channelID string, html string) { func sendNotifications(session *discordgo.Session, channelID string, html string) {
// Extract tags from the HTML content
tags := extractTags(html)
// Define the desired tags to filter for
desiredTags := []string{"Patch Notes", "Atomic Shop", "News"}
// 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 // Parse the HTML content
doc, err := goquery.NewDocumentFromReader(strings.NewReader(html)) doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
if err != nil { if err != nil {
@ -146,6 +191,7 @@ func sendNotifications(session *discordgo.Session, channelID string, html string
} }
} }
// This function will be called whenever a new message is created // This function will be called whenever a new message is created
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore messages created by the bot itself // Ignore messages created by the bot itself