twitterscraper

package module
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2020 License: MIT Imports: 10 Imported by: 0

README

Twitter Scraper

Golang implementation of python library https://github.com/bisguzar/twitter-scraper

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/ihsanturk/twitter-scraper

Usage

Get user tweets
package main

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

func main() {
    for tweet := range twitterscraper.GetTweets(context.Background(), "Twitter", 50) {
        if tweet.Error != nil {
            panic(tweet.Error)
        }
        fmt.Println(tweet.HTML)
    }
}

It appears you can ask for up to 50 tweets.

Search tweets by query standard operators

Tweets containing “twitter” and “scraper” and “data“, filtering out retweets:

package main

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

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

The search ends if we have 50 tweets.

See Rules and filtering for build standard queries.

Get profile
package main

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

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

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetTrends

func GetTrends() ([]string, error)

GetTrends return list of trends.

func GetTweets

func GetTweets(ctx context.Context, user string, maxTweetsNbr int) <-chan *Result

GetTweets returns channel with tweets for a given user.

func SearchTweets

func SearchTweets(ctx context.Context, query string, maxTweetsNbr int) <-chan *Result

SearchTweets returns channel with tweets for a given search query

Types

type Profile

type Profile struct {
	Avatar         string
	Banner         string
	Biography      string
	Birthday       string
	FollowersCount int
	FollowingCount int
	IsPrivate      bool
	IsVerified     bool
	Joined         *time.Time
	LikesCount     int
	Location       string
	Name           string
	TweetsCount    int
	URL            string
	UserID         string
	Username       string
	Website        string
}

Profile of twitter user.

func GetProfile

func GetProfile(username string) (Profile, error)

GetProfile return parsed user profile.

type Result

type Result struct {
	Tweet
	Error error
}

Result of scrapping.

type Tweet

type Tweet struct {
	Hashtags     []string  `json:"hashtags"`
	HTML         string    `json:"html"`
	ID           string    `json:"_id"`
	IsPin        bool      `json:"isPinned"`
	IsRetweet    bool      `json:"isRetweet"`
	Likes        int       `json:"likes"`
	PermanentURL string    `json:"tweetUrl"`
	Photos       []string  `json:"photos"`
	Replies      int       `json:"replies"`
	Retweets     int       `json:"retweets"`
	Text         string    `json:"text"`
	TimeParsed   time.Time `json:"time"`
	TimeCaptured time.Time `json:"capture_time"`
	CaptureDelay float64   `json:"capture_delay_sec"`
	Timestamp    int64     `json:"timestamp"`
	URLs         []string  `json:"urls"`
	UserID       string    `json:"userId"`
	Username     string    `json:"username"`
	Videos       []Video   `json:"videos"`
}

Tweet type.

func FetchSearchTweets

func FetchSearchTweets(query, nextCursor string) ([]*Tweet, string, error)

FetchSearchTweets gets tweets for a given search query, via the Twitter frontend API

func FetchTweets

func FetchTweets(user string, last string) ([]*Tweet, error)

FetchTweets gets tweets for a given user, via the Twitter frontend API.

func (*Tweet) MarshalJSON added in v0.0.3

func (t *Tweet) MarshalJSON() ([]byte, error)

func (*Tweet) UnmarshalJSON added in v0.0.3

func (t *Tweet) UnmarshalJSON(data []byte) error

type Video

type Video struct {
	ID      string
	Preview string
}

Video type.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL