REFACTOR: make Session file storage functions more generic (not hardcoded for scraper.API)

This commit is contained in:
Alessio 2025-02-04 14:46:13 -08:00
parent cb0b478c08
commit b2f697a8a6
6 changed files with 20 additions and 20 deletions

View File

@ -133,7 +133,7 @@ func main() {
// Lop off the ".session" suffix (allows using `--session asdf.session` which lets you tab-autocomplete at command line)
*session_name = (*session_name)[:len(*session_name)-8]
}
api = profile.LoadSession(scraper.UserHandle(*session_name))
profile.LoadSession(scraper.UserHandle(*session_name), &api)
} else {
var err error
api, err = scraper.NewGuestSession()
@ -269,7 +269,7 @@ func login(username string, password string) {
api.LoginVerifyPhone(*challenge, phone_number)
}
profile.SaveSession(api)
profile.SaveSession(api.UserHandle, api.MustMarshalJSON())
happy_exit("Logged in as "+string(api.UserHandle), nil)
}

View File

@ -67,7 +67,7 @@ func (app *Application) Login(w http.ResponseWriter, r *http.Request) {
}
func (app *Application) after_login(w http.ResponseWriter, r *http.Request, api scraper.API) {
app.Profile.SaveSession(api)
app.Profile.SaveSession(api.UserHandle, api.MustMarshalJSON())
// Ensure the user is downloaded
user, err := api.GetUser(api.UserHandle)

View File

@ -76,7 +76,7 @@ func (app *Application) SetActiveUser(handle scraper.UserHandle) error {
if err != nil {
return fmt.Errorf("set active user to %q: %w", handle, err)
}
app.API = app.Profile.LoadSession(handle)
app.Profile.LoadSession(handle, &app.API)
app.ActiveUser = user
app.IsScrapingDisabled = false
}

View File

@ -10,30 +10,22 @@ import (
. "gitlab.com/offline-twitter/twitter_offline_engine/pkg/scraper"
)
func (p Profile) SaveSession(api API) {
data, err := json.Marshal(api)
if err != nil {
panic(err)
}
func (p Profile) SaveSession(userhandle UserHandle, data []byte) {
log.Debug("Profile Dir: " + p.ProfileDir)
err = os.WriteFile(p.ProfileDir+"/"+string(api.UserHandle+".session"), data, os.FileMode(0644))
err := os.WriteFile(p.ProfileDir+"/"+string(userhandle+".session"), data, os.FileMode(0644))
if err != nil {
panic(err)
}
}
func (p Profile) LoadSession(userhandle UserHandle) API {
func (p Profile) LoadSession(userhandle UserHandle, result json.Unmarshaler) {
data, err := os.ReadFile(p.ProfileDir + "/" + string(userhandle+".session"))
if err != nil {
panic(err)
}
var result API
err = json.Unmarshal(data, &result)
err = json.Unmarshal(data, result)
if err != nil {
panic(err)
}
return result
}

View File

@ -11,7 +11,7 @@ import (
"github.com/go-test/deep"
. "gitlab.com/offline-twitter/twitter_offline_engine/pkg/scraper"
"gitlab.com/offline-twitter/twitter_offline_engine/pkg/scraper"
)
// Save and load an API session; it should come back the same
@ -24,7 +24,7 @@ func TestSaveAndLoadAuthenticatedSession(t *testing.T) {
panic(err)
}
api := API{
api := scraper.API{
UserHandle: "testUser",
IsAuthenticated: true,
Client: http.Client{
@ -35,8 +35,9 @@ func TestSaveAndLoadAuthenticatedSession(t *testing.T) {
}
// Save and load the session; it should come back the same
profile.SaveSession(api)
new_api := profile.LoadSession(api.UserHandle)
profile.SaveSession(api.UserHandle, api.MustMarshalJSON())
var new_api scraper.API
profile.LoadSession(api.UserHandle, &new_api)
if diff := deep.Equal(api, new_api); diff != nil {
t.Error(diff)

View File

@ -51,6 +51,13 @@ func (api API) MarshalJSON() ([]byte, error) {
}
return result, nil
}
func (api API) MustMarshalJSON() []byte {
data, err := json.Marshal(api)
if err != nil {
panic(err)
}
return data
}
func (api *API) UnmarshalJSON(data []byte) error {
var in_struct api_outstruct