Add handle of the logged-in user to API

This commit is contained in:
Alessio 2023-01-01 21:45:00 -05:00
parent 6302b3d5ab
commit c3494cfe19
2 changed files with 38 additions and 15 deletions

View File

@ -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/" const API_USER_TIMELINE_BASE_PATH = "https://api.twitter.com/2/timeline/profile/"
type API struct { type API struct {
IsAuthenticated bool UserHandle UserHandle
GuestToken string IsAuthenticated bool
AuthenticationToken string GuestToken string
Client http.Client Client http.Client
CSRFToken string CSRFToken string
} }
func (api API) add_authentication_headers(req *http.Request) { func (api API) add_authentication_headers(req *http.Request) {
@ -54,9 +54,8 @@ func NewGuestSession() API {
panic(err) panic(err)
} }
return API{ return API{
IsAuthenticated: false, IsAuthenticated: false,
GuestToken: guestAPIString, GuestToken: guestAPIString,
AuthenticationToken: "",
Client: http.Client{ Client: http.Client{
Timeout: 10 * time.Second, Timeout: 10 * time.Second,
Jar: jar, 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) dummyURL, err := url.Parse(loginURL)
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -1,28 +1,30 @@
package scraper_test package scraper_test
import ( import (
"fmt" . "offline_twitter/scraper"
"offline_twitter/scraper"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestAuthentication(t *testing.T) { func TestAuthentication(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
require := require.New(t)
username := "offline_twatter" username := "offline_twatter"
password := "S1pKIW#eRT016iA@OFcK" password := "S1pKIW#eRT016iA@OFcK"
api := scraper.NewGuestSession() api := NewGuestSession()
api.LogIn(username, password) api.LogIn(username, password)
assert.True(api.IsAuthenticated) assert.True(api.IsAuthenticated)
assert.NotEqual(api.CSRFToken, "") assert.NotEqual(api.CSRFToken, "")
assert.Equal(api.UserHandle, UserHandle("Offline_Twatter"))
response, err := api.GetLikesFor(1458284524761075714, "") response, err := api.GetLikesFor(1458284524761075714, "")
if err != nil { require.NoError(err)
panic(err) trove, err := response.ToTweetTrove()
} require.NoError(err)
fmt.Println(response) assert.True(len(trove.Tweets) > 0)
} }