From 50c15d9addf26b84958987017135727224f3ebb8 Mon Sep 17 00:00:00 2001 From: WheresAlice Date: Sat, 30 Jul 2022 12:18:01 +0100 Subject: [PATCH] breaking: create an mbasic client type to handle cookies, and accept them --- faceloader.go | 8 ++++-- parser/lib.go | 67 ++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 59 insertions(+), 16 deletions(-) diff --git a/faceloader.go b/faceloader.go index dc51b56..f803634 100644 --- a/faceloader.go +++ b/faceloader.go @@ -23,6 +23,8 @@ import ( var cal ics.Calendar +var m *faceloader.MBasic + func update(a fyne.App) ics.Calendar { rawPages := a.Preferences().String("FacebookPages") log.Println(fmt.Sprintf("Loading events from %q", rawPages)) @@ -37,13 +39,13 @@ func update(a fyne.App) ics.Calendar { pageCal := ics.NewCalendar() pageCal.SetMethod(ics.MethodRequest) - eventLinks, err := faceloader.GetFacebookEventLinks(page) + eventLinks, err := m.GetFacebookEventLinks(page) if err != nil { log.Println(err) } for _, eventLink := range eventLinks { log.Println(eventLink) - i, err := faceloader.InterfaceFromMbasic(eventLink) + i, err := m.InterfaceFromMbasic(eventLink) if err != nil { log.Println(err) } @@ -103,6 +105,8 @@ func main() { s.StartAsync() + m = faceloader.NewMBasicClient() + txtFacebookPages := widget.NewMultiLineEntry() txtFacebookPages.SetText(a.Preferences().String("FacebookPages")) diff --git a/parser/lib.go b/parser/lib.go index 6b75c67..82b4825 100644 --- a/parser/lib.go +++ b/parser/lib.go @@ -1,6 +1,7 @@ package faceloader import ( + "bytes" _ "embed" "encoding/json" "errors" @@ -8,13 +9,20 @@ import ( "github.com/PuerkitoBio/goquery" "github.com/araddon/dateparse" ics "github.com/arran4/golang-ical" + "io/ioutil" "net/http" + "net/http/cookiejar" "net/url" "regexp" "strings" "time" ) +type MBasic struct { + httpClient *http.Client + approvedCookies bool +} + // EventScheme The key parts that a Facebook json+ld event includes type EventScheme struct { Context string `json:"@context"` @@ -40,17 +48,52 @@ type EventScheme struct { Url string `json:"url"` } -func InterfaceFromMbasic(eventUrl string) (map[string]interface{}, error) { +func NewMBasicClient() *MBasic { + jar, _ := cookiejar.New(nil) + return &MBasic{ + httpClient: &http.Client{ + Jar: jar, + }, + approvedCookies: false, + } +} + +func (m *MBasic) Get(link string) ([]byte, error) { + u, err := url.Parse(link) + if err != nil { + return nil, err + } + + if !m.approvedCookies { + u, _ := url.Parse("https://mbasic.facebook.com/cookie/consent/?next_uri=https%3A%2F%2Fmbasic.facebook.com%2F") + resp, _ := m.httpClient.Post(u.String(), "application/x-www-form-urlencoded", bytes.NewBuffer([]byte("accept_only_essential=1"))) + m.httpClient.Jar.SetCookies(u, resp.Cookies()) + m.approvedCookies = true + } + resp, err := m.httpClient.Get(link) + if err != nil { + return nil, err + } + m.httpClient.Jar.SetCookies(u, resp.Cookies()) + + if err != nil { + return nil, err + } + defer resp.Body.Close() + if resp.StatusCode != 200 { + return nil, errors.New(fmt.Sprintf("status code error: %d %s", resp.StatusCode, resp.Status)) + } + respBody, err := ioutil.ReadAll(resp.Body) + return respBody, err +} + +func (m *MBasic) InterfaceFromMbasic(eventUrl string) (map[string]interface{}, error) { var result []map[string]interface{} - res, err := http.Get(eventUrl) + res, err := m.Get(eventUrl) if err != nil { return nil, nil } - defer res.Body.Close() - if res.StatusCode != 200 { - return nil, errors.New(fmt.Sprintf("status code error: %d %s", res.StatusCode, res.Status)) - } - doc, err := goquery.NewDocumentFromReader(res.Body) + doc, err := goquery.NewDocumentFromReader(bytes.NewBuffer(res)) if err != nil { return nil, err } @@ -130,17 +173,13 @@ func RemoveDuplicateStr(strSlice []string) []string { } // GetFacebookEventLinks find links to Facebook events from a Facebook page name -func GetFacebookEventLinks(pageName string) ([]string, error) { +func (m *MBasic) GetFacebookEventLinks(pageName string) ([]string, error) { var links []string - res, err := http.Get(fmt.Sprintf("https://mbasic.facebook.com/%v?v=events", pageName)) + res, err := m.Get(fmt.Sprintf("https://mbasic.facebook.com/%v?v=events", pageName)) if err != nil { return nil, err } - defer res.Body.Close() - if res.StatusCode != 200 { - return nil, errors.New(fmt.Sprintf("status code error: %d %s", res.StatusCode, res.Status)) - } - doc, err := goquery.NewDocumentFromReader(res.Body) + doc, err := goquery.NewDocumentFromReader(bytes.NewBuffer(res)) if err != nil { return nil, err }