From 0f83e34c848f6eaa1b3560c89da9b6e34690f969 Mon Sep 17 00:00:00 2001 From: gutmet Date: Thu, 15 Oct 2020 11:32:12 +0200 Subject: [PATCH] separate user filters for home and mentions --- credentials.go | 15 +++++------ drivel.go | 71 +++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 65 insertions(+), 21 deletions(-) diff --git a/credentials.go b/credentials.go index 3b7000b..2dcc034 100644 --- a/credentials.go +++ b/credentials.go @@ -27,7 +27,7 @@ type credentials struct { accessTokenSecret string } -func (c *credentials) writeToAppDir(appDir string) { +func (c *credentials) writeCredentials(appDir string) { write := func(file string, s string) { optLogFatal("writeToAppDir", ioutil.WriteFile(path.Join(appDir, file), []byte(s), 0640)) } @@ -37,7 +37,7 @@ func (c *credentials) writeToAppDir(appDir string) { write(fileAccessTokenSecret, c.accessTokenSecret) } -func readFromAppDir(appDir string) *credentials { +func readCredentials(appDir string) *credentials { read := func(f string) string { s, err := goutil.ReadFile(path.Join(appDir, f)) optLogFatal("readFromAppDir", err) @@ -65,11 +65,11 @@ func createAppDir(appDir string) { at := ask("Access Token") ats := ask("Access Token Secret") c := credentials{ck, cs, at, ats} - c.writeToAppDir(appDir) + c.writeCredentials(appDir) } -func loadCredentials() *credentials { - log := func(err error) { optLogFatal("loadCredentials", err) } +func appDir() string { + log := func(err error) { optLogFatal("appDir", err) } currentUser, err := user.Current() log(err) homeDir := currentUser.HomeDir @@ -77,12 +77,11 @@ func loadCredentials() *credentials { if !goutil.PathExists(appDir) { createAppDir(appDir) } - c := readFromAppDir(appDir) - return c + return appDir } func getClient() *http.Client { - c := loadCredentials() + c := readCredentials(appDir()) config := oauth1.NewConfig(c.consumerKey, c.consumerSecret) token := oauth1.NewToken(c.accessToken, c.accessTokenSecret) return config.Client(oauth1.NoContext, token) diff --git a/drivel.go b/drivel.go index 2c68dcd..d9b0d79 100644 --- a/drivel.go +++ b/drivel.go @@ -12,6 +12,7 @@ import ( "net/http" "net/url" "os" + "path" "path/filepath" "strconv" "strings" @@ -588,10 +589,12 @@ func (m Status) String() string { return s } -func PrintTweets(tweets []Status) { +func PrintTweets(tweets []Status, userFilter hashset) { for _, tweet := range tweets { - fmt.Println(tweet) - fmt.Println("---------") + if !userFilter.contains(tweet.User.Screen_name) { + fmt.Println(tweet) + fmt.Println("---------") + } } } @@ -619,29 +622,28 @@ func lookup(args []string) error { os.Exit(-1) } tweets := _lookup(args) - PrintTweets(tweets) + PrintTweets(tweets, nil) return nil } -func timeline(endpoint string, quiet bool) []Status { +func timeline(endpoint string) []Status { log := func(err error) { optLogFatal("timeline", err) } body := get(endpoint) var tweets []Status err := json.Unmarshal(body, &tweets) log(err) - if !quiet { - PrintTweets(tweets) - } return tweets } func mentions(args []string) error { - timeline(MENTIONS_ENDPOINT, false) + tweets := timeline(MENTIONS_ENDPOINT) + PrintTweets(tweets, mentionsFilter) return nil } func home(args []string) error { - timeline(HOME_ENDPOINT, false) + tweets := timeline(HOME_ENDPOINT) + PrintTweets(tweets, homeFilter) return nil } @@ -650,7 +652,8 @@ func UserTimelineParameters(screenName string) string { } func userTimeline(args []string) error { - timeline(TIMELINE_ENDPOINT+UserTimelineParameters(args[0]), false) + tweets := timeline(TIMELINE_ENDPOINT + UserTimelineParameters(args[0])) + PrintTweets(tweets, nil) return nil } @@ -747,7 +750,7 @@ func wipeTimeline(likes bool, keepDays int) { } n := 0 now := time.Now() - tweets := timeline(endpoint, true) + tweets := timeline(endpoint) for { for _, tweet := range tweets { daysSince := now.Sub(tweet.Created_at.Time).Hours() / 24 @@ -764,7 +767,7 @@ func wipeTimeline(likes bool, keepDays int) { } } } - newTweets := timeline(endpoint, true) + newTweets := timeline(endpoint) if !equals(newTweets, tweets) { tweets = newTweets } else { @@ -791,10 +794,52 @@ func wipeCommand() (goutil.CommandFlagsInit, goutil.CommandFunc) { return flagsInit, func([]string) error { return wipe(f) } } +type hashset map[string]interface{} + +func makeHashset() hashset { + return make(map[string]interface{}) +} + +func (s hashset) add(member string) { + if s != nil { + s[member] = nil + } +} + +func (s hashset) contains(member string) bool { + if s == nil { + return false + } + if _, ok := s[member]; ok { + return true + } else { + return false + } +} + +func setFilters(appDir string) { + getHashset := func(s string, err error) hashset { + set := makeHashset() + if err == nil { + for _, id := range strings.Split(s, "\n") { + if strings.TrimSpace(id) != "" { + set.add(id) + } + } + } + return set + } + homeFilter = getHashset(goutil.ReadFile(path.Join(appDir, "FilterHome"))) + mentionsFilter = getHashset(goutil.ReadFile(path.Join(appDir, "FilterMentions"))) +} + var client *http.Client +var homeFilter hashset +var mentionsFilter hashset func main() { client = getClient() + setFilters(appDir()) commands := []goutil.Command{ goutil.NewCommand("status", status, "post a status with message and/or media"), goutil.NewCommand("home", home, "get your home timeline"),