Authentication is required!

(quick dirty fix)
Close #115
This commit is contained in:
Alexander Sheiko 2023-07-02 01:41:48 +03:00
parent 5b0c8c3f9f
commit 07ad3789ef
9 changed files with 274 additions and 147 deletions

149
README.md
View file

@ -16,6 +16,75 @@ go get -u github.com/n0madic/twitter-scraper
## Usage
### Authentication
Authentication is required for all methods now!
#### Login
```golang
err := scraper.Login("username", "password")
```
Use username to login, not email!
But if you have email confirmation, use email address in addition:
```golang
err := scraper.Login("username", "password", "email")
```
If you have two-factor authentication, use code:
```golang
err := scraper.Login("username", "password", "code")
```
Status of login can be checked with:
```golang
scraper.IsLoggedIn()
```
Logout (clear session):
```golang
scraper.Logout()
```
If you want save session between restarts, you can save cookies with `scraper.GetCookies()` and restore with `scraper.SetCookies()`.
For example, save cookies:
```golang
cookies := scraper.GetCookies()
// serialize to JSON
js, _ := json.Marshal(cookies)
// save to file
f, _ = os.Create("cookies.json")
f.Write(js)
```
and load cookies:
```golang
f, _ := os.Open("cookies.json")
// deserialize from JSON
var cookies []*http.Cookie
json.NewDecoder(f).Decode(&cookies)
// load cookies
scraper.SetCookies(cookies)
// check login status
scraper.IsLoggedIn()
```
#### Open account
If you don't want to use your account, you can try login as a Twitter app:
```golang
err := scraper.LoginOpenAccount()
```
### Get user tweets
```golang
@ -29,7 +98,10 @@ import (
func main() {
scraper := twitterscraper.New()
err := scraper.LoginOpenAccount()
if err !== nil {
panic(err)
}
for tweet := range scraper.GetTweets(context.Background(), "Twitter", 50) {
if tweet.Error != nil {
panic(tweet.Error)
@ -54,6 +126,10 @@ import (
func main() {
scraper := twitterscraper.New()
err := scraper.LoginOpenAccount()
if err !== nil {
panic(err)
}
tweet, err := scraper.GetTweet("1328684389388185600")
if err != nil {
panic(err)
@ -124,6 +200,7 @@ import (
func main() {
scraper := twitterscraper.New()
scraper.LoginOpenAccount()
profile, err := scraper.GetProfile("Twitter")
if err != nil {
panic(err)
@ -178,76 +255,6 @@ func main() {
}
```
### Use authentication
Some specified user tweets are protected that you must login and follow.
It is also required to search.
#### Login
```golang
err := scraper.Login("username", "password")
```
Use username to login, not email!
But if you have email confirmation, use email address in addition:
```golang
err := scraper.Login("username", "password", "email")
```
If you have two-factor authentication, use code:
```golang
err := scraper.Login("username", "password", "code")
```
Status of login can be checked with:
```golang
scraper.IsLoggedIn()
```
Logout (clear session):
```golang
scraper.Logout()
```
If you want save session between restarts, you can save cookies with `scraper.GetCookies()` and restore with `scraper.SetCookies()`.
For example, save cookies:
```golang
cookies := scraper.GetCookies()
// serialize to JSON
js, _ := json.Marshal(cookies)
// save to file
f, _ = os.Create("cookies.json")
f.Write(js)
```
and load cookies:
```golang
f, _ := os.Open("cookies.json")
// deserialize from JSON
var cookies []*http.Cookie
json.NewDecoder(f).Decode(&cookies)
// load cookies
scraper.SetCookies(cookies)
// check login status
scraper.IsLoggedIn()
```
#### Open account
If you don't want to use your account, you can login as a Twitter app:
```golang
err := scraper.LoginOpenAccount()
```
### Use Proxy
Support HTTP(s) and SOCKS5 proxy