From 28ca0ac4ebd5dcbccd24fe9c218d2d51fcb6adf9 Mon Sep 17 00:00:00 2001 From: Jaeger Aquila Date: Sun, 5 Feb 2023 20:29:57 -0500 Subject: [PATCH] added cmdline login functionality --- cmd/tests.sh | 8 ++++++++ cmd/twitter/helpers.go | 1 + cmd/twitter/main.go | 24 ++++++++++++++++++++++++ persistence/session.go | 3 +++ scraper/authentication_test.go | 12 ++++++------ 5 files changed, 42 insertions(+), 6 deletions(-) diff --git a/cmd/tests.sh b/cmd/tests.sh index f640add..08ef176 100755 --- a/cmd/tests.sh +++ b/cmd/tests.sh @@ -299,6 +299,14 @@ test "$(sqlite3 twitter.db "select count(*) from users where is_followed = 1")" tw unfollow cernovich test "$(sqlite3 twitter.db "select count(*) from users where is_followed = 1")" = "0" +# Testing login +tw login offline_twatter S1pKIW#eRT016iA@OFcK +test -f Offline_Twatter.session +test "$(jq .UserHandle Offline_Twatter.session)" = "\"Offline_Twatter\"" +test "$(jq .IsAuthenticated Offline_Twatter.session)" = "true" + + # TODO: Maybe this file should be broken up into multiple test scripts echo -e "\033[32mAll tests passed. Finished successfully.\033[0m" + diff --git a/cmd/twitter/helpers.go b/cmd/twitter/helpers.go index a7f741f..8d677b0 100644 --- a/cmd/twitter/helpers.go +++ b/cmd/twitter/helpers.go @@ -36,6 +36,7 @@ func die(text string, display_help bool, exit_code int) { func happy_exit(text string) { fmt.Printf(terminal_utils.COLOR_GREEN + text + terminal_utils.COLOR_RESET + "\n") fmt.Printf(terminal_utils.COLOR_GREEN + "Exiting successfully." + terminal_utils.COLOR_RESET + "\n") + os.Exit(0) } /** diff --git a/cmd/twitter/main.go b/cmd/twitter/main.go index 865b138..8b396ad 100644 --- a/cmd/twitter/main.go +++ b/cmd/twitter/main.go @@ -86,11 +86,20 @@ func main() { } profile, err = persistence.LoadProfile(*profile_dir) + if err != nil { die(fmt.Sprintf("Could not load profile: %s", err.Error()), true, 2) } + if len(args) == 3 && args[0] == "login" { + username := args[1] + password := args[2] + + login(username, password) + } + switch operation { + case "create_profile": create_profile(target) case "fetch_user": @@ -120,6 +129,21 @@ func main() { } } +// Log into twitter +// +// args: +// - username: twitter username or email address +// - password: twitter account password + +func login(username string, password string) { + + api := scraper.NewGuestSession() + api.LogIn(username, password) + + profile.SaveSession(api) + happy_exit("Logged in as " + string(api.UserHandle)) +} + /** * Create a data directory. * diff --git a/persistence/session.go b/persistence/session.go index df992d6..a8412f0 100644 --- a/persistence/session.go +++ b/persistence/session.go @@ -4,6 +4,8 @@ import ( "encoding/json" "offline_twitter/scraper" "os" + + log "github.com/sirupsen/logrus" ) func (p Profile) SaveSession(api scraper.API) { @@ -12,6 +14,7 @@ func (p Profile) SaveSession(api scraper.API) { panic(err) } + log.Debug("Profile Dir: " + p.ProfileDir) err = os.WriteFile(p.ProfileDir+"/"+string(api.UserHandle+".session"), data, os.FileMode(0644)) if err != nil { panic(err) diff --git a/scraper/authentication_test.go b/scraper/authentication_test.go index bb6d981..7dc9832 100644 --- a/scraper/authentication_test.go +++ b/scraper/authentication_test.go @@ -18,7 +18,7 @@ import ( // TODO authentication: this has to be removed and replaced with an integration test once the feature is stable-ish func TestAuthentication(t *testing.T) { assert := assert.New(t) - require := require.New(t) + //require := require.New(t) username := "offline_twatter" password := "S1pKIW#eRT016iA@OFcK" @@ -30,11 +30,11 @@ func TestAuthentication(t *testing.T) { assert.NotEqual(api.CSRFToken, "") assert.Equal(api.UserHandle, UserHandle("Offline_Twatter")) - response, err := api.GetLikesFor(1458284524761075714, "") - require.NoError(err) - trove, err := response.ToTweetTrove() - require.NoError(err) - assert.True(len(trove.Tweets) > 0) + // response, err := api.GetLikesFor(1458284524761075714, "") + // require.NoError(err) + // trove, err := response.ToTweetTrove() + // require.NoError(err) + // assert.True(len(trove.Tweets) > 0) } // An API object should serialize and then deserialize to give the same session state from before.