full rewrite

This commit is contained in:
Yann Verry 2022-01-12 00:26:04 +01:00
parent 1586c5383e
commit 34ae7a1777
Signed by: yann
GPG key ID: EB9E679A66B8C7A1

182
main.go
View file

@ -1,55 +1,183 @@
package main package main
import ( import (
"bytes"
"encoding/json"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"net/http/cookiejar"
"net/http/httputil"
"os" "os"
"time"
"github.com/PuerkitoBio/goquery"
) )
func TouteMonAnneeBlog(schoolname string) { type username struct {
Email string `json:"username"`
}
blog := "https://" + schoolname + ".toutemonecole.fr/" type signin struct {
// Request the HTML page. Email string `json:"username"`
res, err := http.Get(blog) Password string `json:"password"`
Student int `json:"student"`
}
var client http.Client
func init() {
jar, err := cookiejar.New(nil)
if err != nil { if err != nil {
log.Fatal(err) log.Fatalf("Got error while creating cookie jar %s", err.Error())
}
client = http.Client{
Jar: jar,
Timeout: time.Second * 10,
}
}
func callGet(url string) error {
method := "GET"
req, err := http.NewRequest(method, url, nil)
if err != nil {
return fmt.Errorf("Got error %s", err.Error())
}
req.Header.Set("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:96.0) Gecko/20100101 Firefox/96.0")
res, err := client.Do(req)
if err != nil {
return fmt.Errorf("Got error %s", err.Error())
} }
defer res.Body.Close() defer res.Body.Close()
if res.StatusCode != 200 { /*
log.Fatalf("status code error: %d %s", res.StatusCode, res.Status) fmt.Printf("=====\n\nStatuscode: %s %d\n========\n\n", url, res.StatusCode)
}
body, err := ioutil.ReadAll(res.Body)
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
// Find the review items fmt.Println(string(body))
doc.Find(".actu_titre").Each(func(i int, s *goquery.Selection) { fmt.Printf("=====\n\n")
// For each item found, get the title for name, values := range res.Header {
title := s.Find("span .translatable-title").Text() // Loop over all values for the name.
fmt.Printf("Title %d: %s\n", i, title) for _, value := range values {
}) fmt.Println(name, value)
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) dump, err := httputil.DumpRequestOut(req, true)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%q", dump)
return nil
}
func callPost(url string, payload []byte) error {
method := "POST"
req, err := http.NewRequest(method, url, bytes.NewBuffer(payload))
if err != nil {
return fmt.Errorf("Got error %s", err.Error())
}
// assume all POST is JSON
req.Header.Set("content-type", "application/json; charset=UTF-8")
req.Header.Set("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:96.0) Gecko/20100101 Firefox/96.0")
dump, err := httputil.DumpRequestOut(req, true)
res, err := client.Do(req)
if err != nil {
return fmt.Errorf("Got error %s", err.Error())
}
defer res.Body.Close()
/*
fmt.Printf("=====\n\nStatuscode: %s %d\n========\n\n", url, res.StatusCode)
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(body))
fmt.Printf("=====\n\n")
for name, values := range res.Header {
// Loop over all values for the name.
for _, value := range values {
fmt.Println(name, value)
}
}
*/
fmt.Printf("=DumpRequestOut====\n\n")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", dump)
return nil
}
func toutmonlogin(schoolid string, email string, password string) {
// get all cookie first
loginUrl := "https://www.toutemonannee.com/connect"
callGet(loginUrl)
// email-username
emailUrl := "https://www.toutemonannee.com/connect/check-email-username"
emailPayload := username{
Email: email,
}
jsonEmailPayload, err := json.Marshal(emailPayload)
if err != nil {
log.Fatal(err)
}
callPost(emailUrl, jsonEmailPayload)
// signin
signinUrl := "https://www.toutemonannee.com/connect/signin"
signinPayload := signin{
Email: email,
Password: password,
Student: 0,
}
jsonSigninPayload, err := json.Marshal(signinPayload)
if err != nil {
log.Fatal(err)
}
callPost(signinUrl, jsonSigninPayload)
// get json
blogposturl := "https://www.toutemonannee.com/journal/" + schoolid + "/posts/list?page=1&publishedDateOrder=1"
callGet(blogposturl)
fmt.Printf("URL: %s\n", url)
})
} }
func main() { func main() {
school, ok := os.LookupEnv("TOUTMONENFANT_ECOLE") schoolid, ok := os.LookupEnv("TOUTMONENFANTID")
email := os.LookupEnv("TOUTMONENFANTEMAIL")
password := os.LookupEnv("TOUTMONENFANTPASSWORD")
if ok { if ok {
TouteMonAnneeBlog(school) toutmonlogin(schoolid, email, password)
} else { } else {
fmt.Printf("TOUTMONENFANT_ECOLE var is undefined") fmt.Printf("TOUTMONENFANTID var is undefined")
} }
} }