mirror of
https://github.com/Xevion/scla-unsubscribe.git
synced 2025-12-06 01:16:15 -06:00
Finish cookie save/load
This commit is contained in:
61
main.go
61
main.go
@@ -1,8 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/cookiejar"
|
"net/http/cookiejar"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
|
||||||
@@ -10,6 +12,7 @@ import (
|
|||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
"github.com/samber/lo"
|
||||||
)
|
)
|
||||||
|
|
||||||
var client *http.Client
|
var client *http.Client
|
||||||
@@ -25,7 +28,6 @@ func init() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg("Failed to open database")
|
log.Fatal().Err(err).Msg("Failed to open database")
|
||||||
}
|
}
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
// Setup http client + cookie jar
|
// Setup http client + cookie jar
|
||||||
jar, _ := cookiejar.New(nil)
|
jar, _ := cookiejar.New(nil)
|
||||||
@@ -37,20 +39,58 @@ func init() {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
defer SaveCookies()
|
// Load cookies from db
|
||||||
|
LoadCookies()
|
||||||
}
|
}
|
||||||
|
|
||||||
func SaveCookies() {
|
func SaveCookies() {
|
||||||
jar := client.Jar.(*cookiejar.Jar)
|
// Get cookies for UTSA.EDU
|
||||||
for _, cookie := range cookies {
|
utsaUrl, _ := url.Parse("https://www.utsa.edu")
|
||||||
err := db.Update(func(txn *badger.Txn) error {
|
utsaCookies := lo.Map(client.Jar.Cookies(utsaUrl), func(cookiePointer *http.Cookie, _ int) http.Cookie {
|
||||||
err := txn.Set([]byte(cookie.Name), []byte(cookie.Value))
|
return *cookiePointer
|
||||||
|
})
|
||||||
|
|
||||||
|
log.Debug().Int("count", len(utsaCookies)).Msg("Saving Cookies")
|
||||||
|
|
||||||
|
// Marshal cookies, create transaction
|
||||||
|
marshalledCookies, _ := json.Marshal(utsaCookies)
|
||||||
|
err := db.Update(func(txn *badger.Txn) error {
|
||||||
|
log.Printf(string(marshalledCookies))
|
||||||
|
err := txn.Set([]byte("utsa_cookies"), []byte(marshalledCookies))
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Err(err).Msg("Failed to save marshalled cookies")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadCookies() {
|
||||||
|
// Load cookies from DB
|
||||||
|
var cookies []http.Cookie
|
||||||
|
err := db.View(func(txn *badger.Txn) error {
|
||||||
|
// Get cookies
|
||||||
|
item, err := txn.Get([]byte("utsa_cookies"))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the value, unmarshal
|
||||||
|
err = item.Value(func(val []byte) error {
|
||||||
|
err := json.Unmarshal(val, &cookies)
|
||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
if err != nil {
|
|
||||||
log.Error().Err(err).Msg("Failed to save cookie")
|
return err
|
||||||
}
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Err(err).Msg("Failed to load marshalled cookies")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
utsaUrl, _ := url.Parse("https://www.utsa.edu")
|
||||||
|
client.Jar.SetCookies(utsaUrl, lo.Map(cookies, func(cookie http.Cookie, _ int) *http.Cookie {
|
||||||
|
return &cookie
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -68,6 +108,9 @@ func main() {
|
|||||||
log.Fatal().Err(err).Msg("Failed to login")
|
log.Fatal().Err(err).Msg("Failed to login")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defer db.Close()
|
||||||
|
defer SaveCookies()
|
||||||
|
|
||||||
// email := strings.ToLower(fmt.Sprintf("%s.%s@my.utsa.edu", fake.FirstName(), fake.LastName()))
|
// email := strings.ToLower(fmt.Sprintf("%s.%s@my.utsa.edu", fake.FirstName(), fake.LastName()))
|
||||||
|
|
||||||
// log.Debug().Str("email", email).Msg("Unsubscribing")
|
// log.Debug().Str("email", email).Msg("Unsubscribing")
|
||||||
|
|||||||
Reference in New Issue
Block a user