Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

Commit

Permalink
Improve logout
Browse files Browse the repository at this point in the history
drop session in Twitter
  • Loading branch information
Alexander Sheiko committed May 10, 2023
1 parent e7f1c80 commit 13f56d8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
19 changes: 10 additions & 9 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package twitterscraper
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"time"
)
Expand Down Expand Up @@ -48,23 +48,24 @@ func (s *Scraper) RequestAPI(req *http.Request, target interface{}) error {
}
defer resp.Body.Close()

content, err := io.ReadAll(resp.Body)
if err != nil {
return err
}

// private profiles return forbidden, but also data
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusForbidden {
content, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("response status %s: %s", resp.Status, content)
}

if resp.Header.Get("X-Rate-Limit-Remaining") == "0" {
s.guestToken = ""
}

b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
if target == nil {
return nil
}
// fmt.Println(string(b))

return json.Unmarshal(b, target)
return json.Unmarshal(content, target)
}

// GetGuestToken from Twitter API
Expand All @@ -81,7 +82,7 @@ func (s *Scraper) GetGuestToken() error {
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
Expand Down
13 changes: 12 additions & 1 deletion auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

const (
loginURL = "https://api.twitter.com/1.1/onboarding/task.json"
logoutURL = "https://api.twitter.com/1.1/account/logout.json"
bearerToken2 = "AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA"
)

Expand Down Expand Up @@ -230,11 +231,21 @@ func (s *Scraper) Login(credentials ...string) error {
}

// Logout is reset session
func (s *Scraper) Logout() {
func (s *Scraper) Logout() error {
req, err := http.NewRequest("POST", logoutURL, nil)
if err != nil {
return err
}
err = s.RequestAPI(req, nil)
if err != nil {
return err
}

s.isLogged = false
s.guestToken = ""
s.client.Jar, _ = cookiejar.New(nil)
s.setBearerToken(bearerToken)
return nil
}

func (s *Scraper) GetCookies() []*http.Cookie {
Expand Down
10 changes: 6 additions & 4 deletions auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ func TestAuth(t *testing.T) {
t.Fatalf("Expected IsLoggedIn() = true")
}
cookies := scraper.GetCookies()
scraper.Logout()
if scraper.IsLoggedIn() {
t.Error("Expected IsLoggedIn() = false")
}
scraper2 := twitterscraper.New()
scraper2.SetCookies(cookies)
if !scraper2.IsLoggedIn() {
t.Error("Expected restored IsLoggedIn() = true")
}
if err := scraper.Logout(); err != nil {
t.Errorf("Logout() error = %v", err)
}
if scraper.IsLoggedIn() {
t.Error("Expected IsLoggedIn() = false")
}
}

0 comments on commit 13f56d8

Please sign in to comment.