scraping desired tags

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

98
main.go
View File

@ -114,38 +114,84 @@ 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) {
// Parse the HTML content // Extract tags from the HTML content
doc, err := goquery.NewDocumentFromReader(strings.NewReader(html)) tags := extractTags(html)
if err != nil {
fmt.Println("Error parsing HTML:", err)
return
}
// Find and extract the date from the webpage // Define the desired tags to filter for
var foundDate bool desiredTags := []string{"Patch Notes", "Atomic Shop", "News"}
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 // Check if any of the desired tags are present in the extracted tags
if foundDate { for _, tag := range tags {
_, err := session.ChannelMessageSend(channelID, "Today's date was found on the Fallout news page!") for _, desiredTag := range desiredTags {
if err != nil { if tag == desiredTag {
fmt.Println("Error sending message to Discord:", err) // If the desired tag is found, send a message to the Discord channel
return _, err := session.ChannelMessageSend(channelID, fmt.Sprintf("Found tag: %s", tag))
} if err != nil {
fmt.Println("Message sent to Discord channel") fmt.Println("Error sending message to Discord:", err)
} else { return
fmt.Println("Today's date not found on the Fallout news page") }
} 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 // 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