• This repository has been archived on 30/Nov/2023
  • Stars
    star
    881
  • Rank 51,820 (Top 2 %)
  • Language
    Go
  • License
    MIT License
  • Created about 6 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Scrape the Twitter frontend API without authentication with Golang.

Twitter Scraper

Go Reference

Twitter's API is annoying to work with, and has lots of limitations โ€” luckily their frontend (JavaScript) has it's own API, which I reverse-engineered. No API rate limits. No tokens needed. No restrictions. Extremely fast.

You can use this library to get the text of any user's Tweets trivially.

Installation

go get -u github.com/n0madic/twitter-scraper

Usage

Authentication

Now all methods require authentication!

Login

err := scraper.Login("username", "password")

Use username to login, not email! But if you have email confirmation, use email address in addition:

err := scraper.Login("username", "password", "email")

If you have two-factor authentication, use code:

err := scraper.Login("username", "password", "code")

Status of login can be checked with:

scraper.IsLoggedIn()

Logout (clear session):

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:

cookies := scraper.GetCookies()
// serialize to JSON
js, _ := json.Marshal(cookies)
// save to file
f, _ = os.Create("cookies.json")
f.Write(js)

and load cookies:

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:

err := scraper.LoginOpenAccount()

Get user tweets

package main

import (
    "context"
    "fmt"
    twitterscraper "github.com/n0madic/twitter-scraper"
)

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)
        }
        fmt.Println(tweet.Text)
    }
}

It appears you can ask for up to 50 tweets.

Get single tweet

package main

import (
    "fmt"

    twitterscraper "github.com/n0madic/twitter-scraper"
)

func main() {
    scraper := twitterscraper.New()
    err := scraper.Login(username, password)
    if err != nil {
        panic(err)
    }
    tweet, err := scraper.GetTweet("1328684389388185600")
    if err != nil {
        panic(err)
    }
    fmt.Println(tweet.Text)
}

Search tweets by query standard operators

Now the search only works for authenticated users!

Tweets containing โ€œtwitterโ€ and โ€œscraperโ€ and โ€œdataโ€œ, filtering out retweets:

package main

import (
    "context"
    "fmt"
    twitterscraper "github.com/n0madic/twitter-scraper"
)

func main() {
    scraper := twitterscraper.New()
    err := scraper.Login(username, password)
    if err != nil {
        panic(err)
    }
    for tweet := range scraper.SearchTweets(context.Background(),
        "twitter scraper data -filter:retweets", 50) {
        if tweet.Error != nil {
            panic(tweet.Error)
        }
        fmt.Println(tweet.Text)
    }
}

The search ends if we have 50 tweets.

See Rules and filtering for build standard queries.

Set search mode

scraper.SetSearchMode(twitterscraper.SearchLatest)

Options:

  • twitterscraper.SearchTop - default mode
  • twitterscraper.SearchLatest - live mode
  • twitterscraper.SearchPhotos - image mode
  • twitterscraper.SearchVideos - video mode
  • twitterscraper.SearchUsers - user mode

Get profile

package main

import (
    "fmt"
    twitterscraper "github.com/n0madic/twitter-scraper"
)

func main() {
    scraper := twitterscraper.New()
    scraper.LoginOpenAccount()
    profile, err := scraper.GetProfile("Twitter")
    if err != nil {
        panic(err)
    }
    fmt.Printf("%+v\n", profile)
}

Search profiles by query

package main

import (
    "context"
    "fmt"
    twitterscraper "github.com/n0madic/twitter-scraper"
)

func main() {
    scraper := twitterscraper.New().SetSearchMode(twitterscraper.SearchUsers)
    err := scraper.Login(username, password)
    if err != nil {
        panic(err)
    }
    for profile := range scraper.SearchProfiles(context.Background(), "Twitter", 50) {
        if profile.Error != nil {
            panic(profile.Error)
        }
        fmt.Println(profile.Name)
    }
}

Get trends

package main

import (
    "fmt"
    twitterscraper "github.com/n0madic/twitter-scraper"
)

func main() {
    scraper := twitterscraper.New()
    trends, err := scraper.GetTrends()
    if err != nil {
        panic(err)
    }
    fmt.Println(trends)
}

Use Proxy

Support HTTP(s) and SOCKS5 proxy

with HTTP

err := scraper.SetProxy("http://localhost:3128")
if err != nil {
    panic(err)
}

with SOCKS5

err := scraper.SetProxy("socks5://localhost:1080")
if err != nil {
    panic(err)
}

Delay requests

Add delay between API requests (in seconds)

scraper.WithDelay(5)

Load timeline with tweet replies

scraper.WithReplies(true)

More Repositories

1

google-play-scraper

Golang scraper to get data from Google Play Store
Go
79
star
2

gitlab-registry-images-cleaner

Utility to remove old Docker images from the Gitlab registry
Python
55
star
3

twitter2rss

Twitter to RSS proxy
Go
50
star
4

alpine-gcc

The Alpine Docker image with latest GCC compiler (C/C++)
Dockerfile
20
star
5

ai-art-bot

App generates random images using AI Stable Diffusion and posts them in Telegram
Python
19
star
6

sendmail

Standalone drop-in replacement for sendmail with direct send
Go
16
star
7

go-hdrezka

Scraper package for HDrezka site
Go
9
star
8

fullrss

Full text RSS feeds proxy
Go
6
star
9

sprig-repl

REPL go-template with Sprig functions
Go
5
star
10

tib-shellex

Acronis TIB Shell Extension Installer
5
star
11

macvendor

Go library with embedded DB for getting MAC address vendor information
Go
5
star
12

qemu-boot-tester

QEMU GUI for testing boot ISO
AutoIt
5
star
13

nmap-vulners-vulscan

Docker image for advanced vulnerability scanning with Nmap NSE scripts
Dockerfile
4
star
14

app2kube

The easiest way to create and apply kubernetes manifests for an application
Go
4
star
15

site2rss

Go library for scraping the site and creating RSS feeds.
Go
4
star
16

get-github-release

Utility downloads and unpacks the latest binary release from GitHub
Go
3
star
17

geo-traceroute

Traceroute with GeoIP lookup
Go
3
star
18

site2rss-proxy

RSS proxy for sites updates
Go
3
star
19

fullrss-proxy

Full text RSS feeds proxy
PHP
2
star
20

github-release-feed

GitHub releases RSS feed
Go
2
star
21

vagrant-kubeadm-cluster

Vagrantfile with kubeadm cluster
Shell
2
star
22

fullfeed

Convert partial feed to full-text feed with golang
Go
2
star
23

TabNote

Simple notepad with tabs and autosave
Pascal
1
star
24

img2pdf

img2pdf is a command line tool to convert raster images to PDF
Go
1
star
25

cloudping

Check the network latency between regions of cloud providers and your location
Go
1
star
26

postlog

Postfix log viewer
PHP
1
star
27

git-clone

Git clone standalone
Go
1
star
28

cf-kv-db

Simple K/V serverless database with Cloudflare workers
JavaScript
1
star
29

smarttv-install

SmartTV Widget Installer
AutoIt
1
star
30

page2rss

Site to RSS proxy
PHP
1
star
31

go-instauuid

Go package that provides a simple and efficient way to generate Instagram-like unique identifiers (UUIDs)
Go
1
star