Skip to content

Commit

Permalink
added scheduling & media upload
Browse files Browse the repository at this point in the history
  • Loading branch information
imperatrona committed Mar 9, 2024
1 parent f5c1694 commit 3c23a97
Show file tree
Hide file tree
Showing 7 changed files with 431 additions and 7 deletions.
58 changes: 56 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ You can use this library to get tweets, profiles, and trends trivially.
- [Get trends](#get-trends)
- [Get following](#get-following)
- [Get followers](#get-followers)
- [Get scheduled tweets](#get-scheduled-tweets)
- [Create scheduled tweet](#create-scheduled-tweet)
- [Delete scheduled tweet](#delete-scheduled-tweet)
- [Upload media](#upload-media)
- [Connection](#connection)
- [Proxy](#proxy)
- [HTTP(s)](#https)
Expand Down Expand Up @@ -345,7 +349,7 @@ trends, err := scraper.GetTrends()

```golang
var cursor string
users, cursor, err := testScraper.FetchFollowing("Support", 20, cursor)
users, cursor, err := scraper.FetchFollowing("Support", 20, cursor)
```

### Get followers
Expand All @@ -357,7 +361,57 @@ users, cursor, err := testScraper.FetchFollowing("Support", 20, cursor)

```golang
var cursor string
users, cursor, err := testScraper.FetchFollowers("Support", 20, cursor)
users, cursor, err := scraper.FetchFollowers("Support", 20, cursor)
```

### Get scheduled tweets

> [!IMPORTANT]
> Requires authentication!
500 requests / 15 minutes

```golang
tweets, err := scraper.FetchScheduledTweets()
```

### Create scheduled tweet

> [!IMPORTANT]
> Requires authentication!
500 requests / 15 minutes

```golang
tweets, err := scraper.CreateScheduledTweet(twitterscraper.TweetSchedule{
Text: "New scheduled tweet text",
Date: time.Now().Add(time.Hour * 24 * 31),
Medias: nil,
})
```

### Delete scheduled tweet

> [!IMPORTANT]
> Requires authentication!
500 requests / 15 minutes

```golang
err := scraper.DeleteScheduledTweet("123")
```

### Upload media

> [!IMPORTANT]
> Requires authentication!
50 requests / 15 minutes

Uploads photo, video or gif for further posting or scheduling. Expires in 24 hours if not used.

```golang
media, err := scraper.UploadMedia("./files/movie.mp4")
```

## Connection
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/imperatrona/twitter-scraper
go 1.16

require (
github.com/AlexEidt/Vidio v1.5.1 // indirect
github.com/google/go-cmp v0.5.9
golang.org/x/net v0.17.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/AlexEidt/Vidio v1.5.1 h1:tovwvtgQagUz1vifiL9OeWkg1fP/XUzFazFKh7tFtaE=
github.com/AlexEidt/Vidio v1.5.1/go.mod h1:djhIMnWMqPrC3X6nB6ymGX6uWWlgw+VayYGKE1bNwmI=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Expand Down
25 changes: 21 additions & 4 deletions schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"io"
"net/url"
"strconv"
"strings"
"time"
)
Expand Down Expand Up @@ -109,6 +110,12 @@ type scheduleTweets struct {
} `json:"data"`
}

type TweetSchedule struct {
Text string
Date time.Time
Medias []*Media
}

func (timeline *scheduleTweets) parseTweets() []*ScheduledTweet {
var tweets []*ScheduledTweet

Expand Down Expand Up @@ -185,8 +192,8 @@ func (s *Scraper) DeleteScheduledTweet(id string) error {
}

// CreateScheduledTweet schedule new tweet.
func (s *Scraper) CreateScheduledTweet(text string, date time.Time) (string, error) {
if date.Unix() <= time.Now().Unix() {
func (s *Scraper) CreateScheduledTweet(schedule TweetSchedule) (string, error) {
if schedule.Date.Unix() <= time.Now().Unix() {
return "", errors.New("date can't be in past")
}

Expand All @@ -199,14 +206,24 @@ func (s *Scraper) CreateScheduledTweet(text string, date time.Time) (string, err

post_tweet_request := map[string]interface{}{
"auto_populate_reply_metadata": false,
"status": text,
"status": schedule.Text,
"exclude_reply_user_ids": []string{},
"media_ids": []string{},
}

if len(schedule.Medias) > 0 {
var media_ids []string

for _, media := range schedule.Medias {
media_ids = append(media_ids, strconv.Itoa(media.ID))
}

post_tweet_request["media_ids"] = media_ids
}

variables := map[string]interface{}{
"post_tweet_request": post_tweet_request,
"execute_at": date.Unix(),
"execute_at": schedule.Date.Unix(),
}

body := map[string]interface{}{
Expand Down
9 changes: 8 additions & 1 deletion schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"testing"
"time"

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

func TestFetchScheduledTweets(t *testing.T) {
Expand All @@ -27,7 +29,12 @@ func TestCreateScheduledTweets(t *testing.T) {
t.Skip("Skipping test due to environment variable")
}
var err error
id, err = testScraper.CreateScheduledTweet("new tweet", time.Now().Add(time.Hour*24*31))

id, err = testScraper.CreateScheduledTweet(twitterscraper.TweetSchedule{
Text: "new tweet",
Date: time.Now().Add(time.Hour * 24 * 31),
Medias: nil,
})
if err != nil {
t.Error(err)
}
Expand Down
Loading

0 comments on commit 3c23a97

Please sign in to comment.