When POSTing a 'like' to the API, save it as a Like in the db as well
This commit is contained in:
parent
a27f12a915
commit
ffaec2799d
@ -351,13 +351,18 @@ test $(sqlite3 twitter.db "select count(*) from tweets where user_id = 44067298
|
||||
# Test fetching user Likes
|
||||
tw fetch_user Offline_Twatter # TODO: why doesn't this work when authenticated?
|
||||
tw --session Offline_Twatter get_user_likes Offline_Twatter
|
||||
test $(sqlite3 twitter.db "select count(*) from likes") = "2"
|
||||
test $(sqlite3 twitter.db "select count(*) from likes") -ge "2"
|
||||
test $(sqlite3 twitter.db "select count(*) from likes where tweet_id = 1671902735250124802") = "1"
|
||||
|
||||
|
||||
# Test liking and unliking
|
||||
tw fetch_tweet_only 1589023388676554753
|
||||
test $(sqlite3 twitter.db "select count(*) from likes where tweet_id = 1589023388676554753 and user_id = (select id from users where handle like 'offline_twatter')") = "0"
|
||||
tw --session Offline_Twatter like_tweet https://twitter.com/elonmusk/status/1589023388676554753
|
||||
test $(sqlite3 twitter.db "select count(*) from likes where tweet_id = 1589023388676554753 and user_id = (select id from users where handle like 'offline_twatter')") = "1"
|
||||
tw --session Offline_Twatter unlike_tweet https://twitter.com/elonmusk/status/1589023388676554753
|
||||
# TODO: implement deleting a Like
|
||||
# test $(sqlite3 twitter.db "select count(*) from likes where tweet_id = 1589023388676554753 and user_id = (select id from users where handle like 'offline_twatter')") = "0"
|
||||
|
||||
|
||||
# TODO: Maybe this file should be broken up into multiple test scripts
|
||||
|
@ -356,10 +356,14 @@ func like_tweet(tweet_identifier string) {
|
||||
if err != nil {
|
||||
die(err.Error(), false, -1)
|
||||
}
|
||||
err = scraper.LikeTweet(tweet_id)
|
||||
like, err := scraper.LikeTweet(tweet_id)
|
||||
if err != nil {
|
||||
die(err.Error(), false, -10)
|
||||
}
|
||||
err = profile.SaveLike(like)
|
||||
if err != nil {
|
||||
die(err.Error(), false, -1)
|
||||
}
|
||||
happy_exit("Liked the tweet.")
|
||||
}
|
||||
|
||||
|
@ -174,7 +174,7 @@ func (api *API) LogIn(username string, password string) {
|
||||
Subtasks []struct {
|
||||
OpenAccount struct {
|
||||
User struct {
|
||||
ID int
|
||||
ID int `json:"id_str,string"`
|
||||
Name string
|
||||
ScreenName string `json:"screen_name"`
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ import (
|
||||
var AlreadyLikedThisTweet error = errors.New("already liked this tweet")
|
||||
var HaventLikedThisTweet error = errors.New("Haven't liked this tweet")
|
||||
|
||||
func (api API) LikeTweet(id TweetID) error {
|
||||
func (api API) LikeTweet(id TweetID) (Like, error) {
|
||||
type LikeResponse struct {
|
||||
Data struct {
|
||||
FavoriteTweet string `json:"favorite_tweet"`
|
||||
@ -30,17 +30,21 @@ func (api API) LikeTweet(id TweetID) error {
|
||||
&result,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error executing the HTTP POST request:\n %w", err)
|
||||
return Like{}, fmt.Errorf("Error executing the HTTP POST request:\n %w", err)
|
||||
}
|
||||
if len(result.Errors) > 0 {
|
||||
if strings.Contains(result.Errors[0].Message, "has already favorited tweet") {
|
||||
return AlreadyLikedThisTweet
|
||||
return Like{}, AlreadyLikedThisTweet
|
||||
}
|
||||
}
|
||||
if result.Data.FavoriteTweet != "Done" {
|
||||
panic(fmt.Sprintf("Dunno why but it failed with value %q", result.Data.FavoriteTweet))
|
||||
}
|
||||
return nil
|
||||
return Like{
|
||||
UserID: api.UserID,
|
||||
TweetID: id,
|
||||
SortID: -1,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (api API) UnlikeTweet(id TweetID) error {
|
||||
@ -75,7 +79,7 @@ func (api API) UnlikeTweet(id TweetID) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func LikeTweet(id TweetID) error {
|
||||
func LikeTweet(id TweetID) (Like, error) {
|
||||
if !the_api.IsAuthenticated {
|
||||
log.Fatalf("Must be authenticated!")
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user