From c3494cfe19e87e07b0f2a933ba719e4432dfabbe Mon Sep 17 00:00:00 2001 From: Alessio Date: Sun, 1 Jan 2023 21:45:00 -0500 Subject: [PATCH] Add handle of the logged-in user to API --- scraper/api_request_utils.go | 37 ++++++++++++++++++++++++++-------- scraper/authentication_test.go | 16 ++++++++------- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/scraper/api_request_utils.go b/scraper/api_request_utils.go index 430ee0d..70ae32e 100644 --- a/scraper/api_request_utils.go +++ b/scraper/api_request_utils.go @@ -17,11 +17,11 @@ const API_CONVERSATION_BASE_PATH = "https://twitter.com/i/api/2/timeline/convers const API_USER_TIMELINE_BASE_PATH = "https://api.twitter.com/2/timeline/profile/" type API struct { - IsAuthenticated bool - GuestToken string - AuthenticationToken string - Client http.Client - CSRFToken string + UserHandle UserHandle + IsAuthenticated bool + GuestToken string + Client http.Client + CSRFToken string } func (api API) add_authentication_headers(req *http.Request) { @@ -54,9 +54,8 @@ func NewGuestSession() API { panic(err) } return API{ - IsAuthenticated: false, - GuestToken: guestAPIString, - AuthenticationToken: "", + IsAuthenticated: false, + GuestToken: guestAPIString, Client: http.Client{ Timeout: 10 * time.Second, Jar: jar, @@ -108,6 +107,28 @@ func (api *API) LogIn(username string, password string) { } } + type final_result_struct struct { + Subtasks []struct { + OpenAccount struct { + User struct { + Name string + ScreenName string `json:"screen_name"` + } + } `json:"open_account"` + } + } + bytes, err := json.Marshal(result) + if err != nil { + panic(err) + } + var final_result final_result_struct + err = json.Unmarshal(bytes, &final_result) + if err != nil { + panic(err) + } + + api.UserHandle = UserHandle(final_result.Subtasks[0].OpenAccount.User.ScreenName) + dummyURL, err := url.Parse(loginURL) if err != nil { panic(err) diff --git a/scraper/authentication_test.go b/scraper/authentication_test.go index 16ed215..388a9ce 100644 --- a/scraper/authentication_test.go +++ b/scraper/authentication_test.go @@ -1,28 +1,30 @@ package scraper_test import ( - "fmt" - "offline_twitter/scraper" + . "offline_twitter/scraper" "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestAuthentication(t *testing.T) { assert := assert.New(t) + require := require.New(t) username := "offline_twatter" password := "S1pKIW#eRT016iA@OFcK" - api := scraper.NewGuestSession() + api := NewGuestSession() api.LogIn(username, password) assert.True(api.IsAuthenticated) assert.NotEqual(api.CSRFToken, "") + assert.Equal(api.UserHandle, UserHandle("Offline_Twatter")) response, err := api.GetLikesFor(1458284524761075714, "") - if err != nil { - panic(err) - } - fmt.Println(response) + require.NoError(err) + trove, err := response.ToTweetTrove() + require.NoError(err) + assert.True(len(trove.Tweets) > 0) }