added cmdline login functionality

This commit is contained in:
Jaeger Aquila 2023-02-05 20:29:57 -05:00
parent 68867718af
commit 28ca0ac4eb
5 changed files with 42 additions and 6 deletions

View File

@ -299,6 +299,14 @@ test "$(sqlite3 twitter.db "select count(*) from users where is_followed = 1")"
tw unfollow cernovich tw unfollow cernovich
test "$(sqlite3 twitter.db "select count(*) from users where is_followed = 1")" = "0" 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 # TODO: Maybe this file should be broken up into multiple test scripts
echo -e "\033[32mAll tests passed. Finished successfully.\033[0m" echo -e "\033[32mAll tests passed. Finished successfully.\033[0m"

View File

@ -36,6 +36,7 @@ func die(text string, display_help bool, exit_code int) {
func happy_exit(text string) { func happy_exit(text string) {
fmt.Printf(terminal_utils.COLOR_GREEN + text + terminal_utils.COLOR_RESET + "\n") 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") fmt.Printf(terminal_utils.COLOR_GREEN + "Exiting successfully." + terminal_utils.COLOR_RESET + "\n")
os.Exit(0)
} }
/** /**

View File

@ -86,11 +86,20 @@ func main() {
} }
profile, err = persistence.LoadProfile(*profile_dir) profile, err = persistence.LoadProfile(*profile_dir)
if err != nil { if err != nil {
die(fmt.Sprintf("Could not load profile: %s", err.Error()), true, 2) 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 { switch operation {
case "create_profile": case "create_profile":
create_profile(target) create_profile(target)
case "fetch_user": 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. * Create a data directory.
* *

View File

@ -4,6 +4,8 @@ import (
"encoding/json" "encoding/json"
"offline_twitter/scraper" "offline_twitter/scraper"
"os" "os"
log "github.com/sirupsen/logrus"
) )
func (p Profile) SaveSession(api scraper.API) { func (p Profile) SaveSession(api scraper.API) {
@ -12,6 +14,7 @@ func (p Profile) SaveSession(api scraper.API) {
panic(err) panic(err)
} }
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(api.UserHandle+".session"), data, os.FileMode(0644))
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -18,7 +18,7 @@ import (
// TODO authentication: this has to be removed and replaced with an integration test once the feature is stable-ish // TODO authentication: this has to be removed and replaced with an integration test once the feature is stable-ish
func TestAuthentication(t *testing.T) { func TestAuthentication(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
require := require.New(t) //require := require.New(t)
username := "offline_twatter" username := "offline_twatter"
password := "S1pKIW#eRT016iA@OFcK" password := "S1pKIW#eRT016iA@OFcK"
@ -30,11 +30,11 @@ func TestAuthentication(t *testing.T) {
assert.NotEqual(api.CSRFToken, "") assert.NotEqual(api.CSRFToken, "")
assert.Equal(api.UserHandle, UserHandle("Offline_Twatter")) assert.Equal(api.UserHandle, UserHandle("Offline_Twatter"))
response, err := api.GetLikesFor(1458284524761075714, "") // response, err := api.GetLikesFor(1458284524761075714, "")
require.NoError(err) // require.NoError(err)
trove, err := response.ToTweetTrove() // trove, err := response.ToTweetTrove()
require.NoError(err) // require.NoError(err)
assert.True(len(trove.Tweets) > 0) // assert.True(len(trove.Tweets) > 0)
} }
// An API object should serialize and then deserialize to give the same session state from before. // An API object should serialize and then deserialize to give the same session state from before.