full rewrite
This commit is contained in:
parent
1586c5383e
commit
34ae7a1777
182
main.go
182
main.go
|
@ -1,55 +1,183 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"net/http/httputil"
|
||||
"os"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TouteMonAnneeBlog(schoolname string) {
|
||||
type username struct {
|
||||
Email string `json:"username"`
|
||||
}
|
||||
|
||||
blog := "https://" + schoolname + ".toutemonecole.fr/"
|
||||
// Request the HTML page.
|
||||
res, err := http.Get(blog)
|
||||
type signin struct {
|
||||
Email string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Student int `json:"student"`
|
||||
}
|
||||
|
||||
var client http.Client
|
||||
|
||||
func init() {
|
||||
jar, err := cookiejar.New(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()
|
||||
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)
|
||||
|
||||
// Load the HTML document
|
||||
doc, err := goquery.NewDocumentFromReader(res.Body)
|
||||
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)
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
dump, err := httputil.DumpRequestOut(req, true)
|
||||
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("%q", dump)
|
||||
|
||||
fmt.Printf("Content %d: %s\n", i, content)
|
||||
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() {
|
||||
|
||||
school, ok := os.LookupEnv("TOUTMONENFANT_ECOLE")
|
||||
schoolid, ok := os.LookupEnv("TOUTMONENFANTID")
|
||||
email := os.LookupEnv("TOUTMONENFANTEMAIL")
|
||||
password := os.LookupEnv("TOUTMONENFANTPASSWORD")
|
||||
if ok {
|
||||
TouteMonAnneeBlog(school)
|
||||
toutmonlogin(schoolid, email, password)
|
||||
} else {
|
||||
fmt.Printf("TOUTMONENFANT_ECOLE var is undefined")
|
||||
fmt.Printf("TOUTMONENFANTID var is undefined")
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue