added CreateScheduledTweet
This commit is contained in:
parent
c862968cdd
commit
00eb392224
2 changed files with 71 additions and 2 deletions
53
schedule.go
53
schedule.go
|
|
@ -183,3 +183,56 @@ func (s *Scraper) DeleteScheduledTweet(id string) error {
|
||||||
|
|
||||||
return errors.New("scheduled tweet wasn't removed")
|
return errors.New("scheduled tweet wasn't removed")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CreateScheduledTweet schedule new tweet.
|
||||||
|
func (s *Scraper) CreateScheduledTweet(text string, date time.Time) (string, error) {
|
||||||
|
if date.Unix() <= time.Now().Unix() {
|
||||||
|
return "", errors.New("date can't be in past")
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := s.newRequest("POST", "https://twitter.com/i/api/graphql/LCVzRQGxOaGnOnYH01NQXg/CreateScheduledTweet")
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("content-type", "application/json")
|
||||||
|
|
||||||
|
post_tweet_request := map[string]interface{}{
|
||||||
|
"auto_populate_reply_metadata": false,
|
||||||
|
"status": text,
|
||||||
|
"exclude_reply_user_ids": []string{},
|
||||||
|
"media_ids": []string{},
|
||||||
|
}
|
||||||
|
|
||||||
|
variables := map[string]interface{}{
|
||||||
|
"post_tweet_request": post_tweet_request,
|
||||||
|
"execute_at": date.Unix(),
|
||||||
|
}
|
||||||
|
|
||||||
|
body := map[string]interface{}{
|
||||||
|
"variables": variables,
|
||||||
|
"queryId": "LCVzRQGxOaGnOnYH01NQXg",
|
||||||
|
}
|
||||||
|
|
||||||
|
b, _ := json.Marshal(body)
|
||||||
|
req.Body = io.NopCloser(bytes.NewReader(b))
|
||||||
|
|
||||||
|
var response struct {
|
||||||
|
Data struct {
|
||||||
|
Tweet struct {
|
||||||
|
ID string `json:"rest_id"`
|
||||||
|
} `json:"tweet"`
|
||||||
|
} `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
err = s.RequestAPI(req, &response)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if response.Data.Tweet.ID != "" {
|
||||||
|
return response.Data.Tweet.ID, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", errors.New("tweet wasn't scheduled")
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFetchScheduledTweets(t *testing.T) {
|
func TestFetchScheduledTweets(t *testing.T) {
|
||||||
|
|
@ -16,8 +17,23 @@ func TestFetchScheduledTweets(t *testing.T) {
|
||||||
fmt.Println(string(b))
|
fmt.Println(string(b))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDeleteScheduledTweets(t *testing.T) {
|
var id string
|
||||||
if err := testScraper.DeleteScheduledTweet("1760827700724355072"); err != nil {
|
|
||||||
|
func TestCreateScheduledTweets(t *testing.T) {
|
||||||
|
var err error
|
||||||
|
id, err = testScraper.CreateScheduledTweet("new tweet", time.Now().Add(time.Hour*24*31))
|
||||||
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDeleteScheduledTweets(t *testing.T) {
|
||||||
|
if id == "" {
|
||||||
|
t.Skip("run TestCreateScheduledTweets before")
|
||||||
|
}
|
||||||
|
if err := testScraper.DeleteScheduledTweet(id); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else {
|
||||||
|
id = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue