Skip to content

Instantly share code, notes, and snippets.

@vquaiato
Last active July 2, 2020 00:25
Show Gist options
  • Save vquaiato/fde1d79152d6e9208236b555ecd8a90c to your computer and use it in GitHub Desktop.
Save vquaiato/fde1d79152d6e9208236b555ecd8a90c to your computer and use it in GitHub Desktop.
Scrapper/monitor a PC build price on https://meupc.net
package main
import (
"fmt"
"log"
"os/exec"
"regexp"
"github.com/gocolly/colly/v2"
)
func main() {
url := "https://meupc.net/build/tH5P3N"
c := colly.NewCollector(colly.AllowedDomains("meupc.net"))
var values []string
reg := regexp.MustCompile("[0-9\\.\\,]+")
c.OnHTML("tbody.table-responsive-totals", func(e *colly.HTMLElement) {
e.ForEach("tr td:nth-of-type(2)", func(i int, c *colly.HTMLElement) {
values = append(values, reg.FindString(c.Text))
})
display(values, url)
})
c.OnRequest(func(r *colly.Request) {
fmt.Printf("Visiting: %s \n", r.URL)
})
c.OnError(func(_resp *colly.Response, e error) {
log.Fatal(e.Error())
})
c.Visit(url)
}
func display(values []string, url string) {
notif := fmt.Sprintf("💵 R$ %s | 💳 R$ %s", values[2], values[0])
icon := "https://www.cyberpowerpc.com/images/cs/eluna242v/CS-450-151_220.png"
err := exec.Command("terminal-notifier", "-title", "PC Scrapper", "-message", notif, "-open", url, "-sound", "default", "-appIcon", icon).Run()
if err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment