Add list_followed subcommand

This commit is contained in:
Alessio 2022-02-28 16:06:58 -08:00
parent 3c2aee5016
commit 508670248e
7 changed files with 53 additions and 4 deletions

View File

@ -235,7 +235,16 @@ test $(sqlite3 twitter.db "select is_content_downloaded from users where handle=
test "$(sqlite3 twitter.db "select count(*) from users where is_followed = 1")" = "0"
tw follow michaelmalice
test "$(sqlite3 twitter.db "select handle from users where is_followed = 1")" = "michaelmalice"
tw follow cernovich
test "$(tw list_followed | wc -l)" = 2
test "$(tw list_followed | grep -iq cernovich && echo YES)" = "YES"
test "$(tw list_followed | grep -iq michaelmalice && echo YES)" = "YES"
test "$(tw list_followed | grep -iq blahblahgibberish && echo YES)" = ""
tw unfollow michaelmalice
test "$(sqlite3 twitter.db "select count(*) from users where is_followed = 1")" = "1"
tw unfollow cernovich
test "$(sqlite3 twitter.db "select count(*) from users where is_followed = 1")" = "0"
# TODO: Maybe this file should be broken up into multiple test scripts

View File

@ -51,6 +51,10 @@ This application downloads tweets from twitter and saves them in a SQLite databa
unfollow
<TARGET> is the user handle
list_followed
No <TARGET> is needed; will be ignored if given.
Lists all the users (by their @handle) that are followed.
search
<TARGET> is the search query. Should be wrapped in quotes if it has spaces.

View File

@ -69,7 +69,12 @@ func main() {
log.SetLevel(logging_level)
if len(args) < 2 {
die("", true, 1)
if len(args) == 1 && args[0] == "list_followed" {
// "list_followed" doesn't need a target, so create a fake second arg
args = append(args, "")
} else {
die("", true, 1)
}
}
operation := args[0]
@ -108,6 +113,8 @@ func main() {
follow_user(target, true)
case "unfollow":
follow_user(target, false)
case "list_followed":
list_followed()
default:
die("Invalid operation: " + operation, true, 3)
}
@ -266,3 +273,9 @@ func follow_user(handle string, is_followed bool) {
happy_exit("Unfollowed user: " + handle)
}
}
func list_followed() {
for _, handle := range profile.GetAllFollowedUsers() {
fmt.Println(handle)
}
}

View File

@ -233,3 +233,24 @@ func (p Profile) NextFakeUserID() scraper.UserID {
}
return ret
}
func (p Profile) GetAllFollowedUsers() []scraper.UserHandle {
rows, err := p.DB.Query("select handle from users where is_followed = 1")
if err != nil {
panic(err)
}
ret := []scraper.UserHandle{}
var tmp scraper.UserHandle
for rows.Next() {
err = rows.Scan(&tmp)
if err != nil {
panic(err)
}
ret = append(ret, tmp)
}
return ret
}

View File

@ -300,7 +300,6 @@ func TestCreateUnknownUserWithHandleThatAlreadyExists(t *testing.T) {
user := create_stable_user()
unknown_user := scraper.GetUnknownUserWithHandle(user.Handle)
assert.Equal(scraper.UserID(0), unknown_user.ID)

View File

@ -332,7 +332,6 @@ func ApiRequestAddAllParams(req *http.Request) {
query.Add("tweet_mode", "extended")
query.Add("include_entities", "true")
query.Add("include_user_entities", "true")
query.Add("include_ext_media_color", "true")
query.Add("include_ext_media_availability", "true")
query.Add("send_error_codes", "true")
query.Add("simple_quoted_tweet", "true")

View File

@ -102,6 +102,10 @@ func ParseHandleFromTweetUrl(tweet_url string) (UserHandle, error) {
return UserHandle(matches[1]), nil
}
/**
* Unknown Users with handles are only created by direct GetUser calls (either `twitter fetch_user`
* subcommand or as part of tombstone user fetching.)
*/
func GetUnknownUserWithHandle(handle UserHandle) User {
return User{
ID: UserID(0), // 2^62 + 1...
@ -114,7 +118,7 @@ func GetUnknownUserWithHandle(handle UserHandle) User {
Website:"<blank>",
JoinDate: time.Unix(0, 0),
IsVerified: false,
IsPrivate: true,
IsPrivate: false,
IsNeedingFakeID: true,
IsIdFake: true,
}