toutmonenfant/main.go
2021-09-03 22:31:55 +02:00

56 lines
1.2 KiB
Go

package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/PuerkitoBio/goquery"
)
func TouteMonAnneeBlog(schoolname string) {
blog := "https://" + schoolname + ".toutemonecole.fr/"
// Request the HTML page.
res, err := http.Get(blog)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
}
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Fatal(err)
}
// Find the review items
doc.Find(".actu_titre").Each(func(i int, s *goquery.Selection) {
// For each item found, get the title
title := s.Find("span .translatable-title").Text()
fmt.Printf("Title %d: %s\n", i, title)
})
doc.Find(".actu_description").Each(func(i int, s *goquery.Selection) {
url, _ := s.Find(".actu_extrait div a").Attr("href")
content := s.Find("span").Text()
fmt.Printf("Content %d: %s\n", i, content)
fmt.Printf("URL: %s\n", url)
})
}
func main() {
school, ok := os.LookupEnv("TOUTMONENFANT_SCHOOLNAME")
if ok {
TouteMonAnneeBlog(school)
} else {
fmt.Printf("TOUTMONENFANT_SCHOOLNAME var is undefined")
}
}