forked from imperatrona/twitter-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Used guest frontend API BREAKING CHANGE: remove tweet.HTML property Loading more information Minor fixes and changes
- Loading branch information
Alexander Sheiko
committed
Dec 11, 2020
1 parent
1c582e1
commit edad8f6
Showing
15 changed files
with
618 additions
and
487 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package twitterscraper | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"sync" | ||
) | ||
|
||
const bearerToken string = "AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA" | ||
|
||
type user struct { | ||
Data struct { | ||
User struct { | ||
RestID string `json:"rest_id"` | ||
} `json:"user"` | ||
} `json:"data"` | ||
} | ||
|
||
var ( | ||
guestToken string | ||
cacheIDs sync.Map | ||
) | ||
|
||
func requestAPI(req *http.Request, target interface{}) error { | ||
if guestToken == "" { | ||
err := GetGuestToken() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
req.Header.Set("Authorization", "Bearer "+bearerToken) | ||
req.Header.Set("X-Guest-Token", guestToken) | ||
|
||
resp, err := newHTTPClient().Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
return json.NewDecoder(resp.Body).Decode(target) | ||
} | ||
|
||
// GetGuestToken from API | ||
func GetGuestToken() error { | ||
req, err := http.NewRequest("POST", "https://api.twitter.com/1.1/guest/activate.json", nil) | ||
if err != nil { | ||
return err | ||
} | ||
req.Header.Set("Authorization", "Bearer "+bearerToken) | ||
|
||
resp, err := newHTTPClient().Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("response status %s", resp.Status) | ||
} | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var jsn map[string]interface{} | ||
if err := json.Unmarshal(body, &jsn); err != nil { | ||
return err | ||
} | ||
var ok bool | ||
if guestToken, ok = jsn["guest_token"].(string); !ok { | ||
return fmt.Errorf("guest_token not found") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// GetUserIDByScreenName from API | ||
func GetUserIDByScreenName(screenName string) (string, error) { | ||
id, ok := cacheIDs.Load(screenName) | ||
if ok { | ||
return id.(string), nil | ||
} | ||
|
||
var jsn user | ||
req, err := http.NewRequest("GET", "https://api.twitter.com/graphql/4S2ihIKfF3xhp-ENxvUAfQ/UserByScreenName?variables=%7B%22screen_name%22%3A%22"+screenName+"%22%2C%22withHighlightedLabel%22%3Atrue%7D", nil) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
err = requestAPI(req, &jsn) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if jsn.Data.User.RestID == "" { | ||
return "", fmt.Errorf("rest_id not found") | ||
} | ||
|
||
cacheIDs.Store(screenName, jsn.Data.User.RestID) | ||
|
||
return jsn.Data.User.RestID, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package twitterscraper | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestGetGuestToken(t *testing.T) { | ||
if err := GetGuestToken(); err != nil { | ||
t.Errorf("getGuestToken() error = %v", err) | ||
} | ||
if guestToken == "" { | ||
t.Error("Expected non-empty guestToken") | ||
} | ||
} | ||
|
||
func TestGetUserIDByScreenName(t *testing.T) { | ||
userID, err := GetUserIDByScreenName("Twitter") | ||
if err != nil { | ||
t.Errorf("getUserByScreenName() error = %v", err) | ||
} | ||
if userID == "" { | ||
t.Error("Expected non-empty user ID") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,4 @@ | ||
github.com/PuerkitoBio/goquery v1.5.1 h1:PSPBGne8NIUWw+/7vFBV+kG2J/5MOjbzc7154OaKCSE= | ||
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= | ||
github.com/andybalholm/cascadia v1.1.0 h1:BuuO6sSfQNFRu1LppgbD25Hr2vLYW25JvxHs5zzsLTo= | ||
github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= | ||
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= | ||
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= | ||
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI= | ||
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M= | ||
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.