separate user filters for home and mentions

This commit is contained in:
gutmet 2020-10-15 11:32:12 +02:00
parent b21bc5825d
commit 0f83e34c84
2 changed files with 65 additions and 21 deletions

View File

@ -27,7 +27,7 @@ type credentials struct {
accessTokenSecret string accessTokenSecret string
} }
func (c *credentials) writeToAppDir(appDir string) { func (c *credentials) writeCredentials(appDir string) {
write := func(file string, s string) { write := func(file string, s string) {
optLogFatal("writeToAppDir", ioutil.WriteFile(path.Join(appDir, file), []byte(s), 0640)) 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) write(fileAccessTokenSecret, c.accessTokenSecret)
} }
func readFromAppDir(appDir string) *credentials { func readCredentials(appDir string) *credentials {
read := func(f string) string { read := func(f string) string {
s, err := goutil.ReadFile(path.Join(appDir, f)) s, err := goutil.ReadFile(path.Join(appDir, f))
optLogFatal("readFromAppDir", err) optLogFatal("readFromAppDir", err)
@ -65,11 +65,11 @@ func createAppDir(appDir string) {
at := ask("Access Token") at := ask("Access Token")
ats := ask("Access Token Secret") ats := ask("Access Token Secret")
c := credentials{ck, cs, at, ats} c := credentials{ck, cs, at, ats}
c.writeToAppDir(appDir) c.writeCredentials(appDir)
} }
func loadCredentials() *credentials { func appDir() string {
log := func(err error) { optLogFatal("loadCredentials", err) } log := func(err error) { optLogFatal("appDir", err) }
currentUser, err := user.Current() currentUser, err := user.Current()
log(err) log(err)
homeDir := currentUser.HomeDir homeDir := currentUser.HomeDir
@ -77,12 +77,11 @@ func loadCredentials() *credentials {
if !goutil.PathExists(appDir) { if !goutil.PathExists(appDir) {
createAppDir(appDir) createAppDir(appDir)
} }
c := readFromAppDir(appDir) return appDir
return c
} }
func getClient() *http.Client { func getClient() *http.Client {
c := loadCredentials() c := readCredentials(appDir())
config := oauth1.NewConfig(c.consumerKey, c.consumerSecret) config := oauth1.NewConfig(c.consumerKey, c.consumerSecret)
token := oauth1.NewToken(c.accessToken, c.accessTokenSecret) token := oauth1.NewToken(c.accessToken, c.accessTokenSecret)
return config.Client(oauth1.NoContext, token) return config.Client(oauth1.NoContext, token)

View File

@ -12,6 +12,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"path"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
@ -588,11 +589,13 @@ func (m Status) String() string {
return s return s
} }
func PrintTweets(tweets []Status) { func PrintTweets(tweets []Status, userFilter hashset) {
for _, tweet := range tweets { for _, tweet := range tweets {
if !userFilter.contains(tweet.User.Screen_name) {
fmt.Println(tweet) fmt.Println(tweet)
fmt.Println("---------") fmt.Println("---------")
} }
}
} }
func (m Status) URL() string { func (m Status) URL() string {
@ -619,29 +622,28 @@ func lookup(args []string) error {
os.Exit(-1) os.Exit(-1)
} }
tweets := _lookup(args) tweets := _lookup(args)
PrintTweets(tweets) PrintTweets(tweets, nil)
return nil return nil
} }
func timeline(endpoint string, quiet bool) []Status { func timeline(endpoint string) []Status {
log := func(err error) { optLogFatal("timeline", err) } log := func(err error) { optLogFatal("timeline", err) }
body := get(endpoint) body := get(endpoint)
var tweets []Status var tweets []Status
err := json.Unmarshal(body, &tweets) err := json.Unmarshal(body, &tweets)
log(err) log(err)
if !quiet {
PrintTweets(tweets)
}
return tweets return tweets
} }
func mentions(args []string) error { func mentions(args []string) error {
timeline(MENTIONS_ENDPOINT, false) tweets := timeline(MENTIONS_ENDPOINT)
PrintTweets(tweets, mentionsFilter)
return nil return nil
} }
func home(args []string) error { func home(args []string) error {
timeline(HOME_ENDPOINT, false) tweets := timeline(HOME_ENDPOINT)
PrintTweets(tweets, homeFilter)
return nil return nil
} }
@ -650,7 +652,8 @@ func UserTimelineParameters(screenName string) string {
} }
func userTimeline(args []string) error { func userTimeline(args []string) error {
timeline(TIMELINE_ENDPOINT+UserTimelineParameters(args[0]), false) tweets := timeline(TIMELINE_ENDPOINT + UserTimelineParameters(args[0]))
PrintTweets(tweets, nil)
return nil return nil
} }
@ -747,7 +750,7 @@ func wipeTimeline(likes bool, keepDays int) {
} }
n := 0 n := 0
now := time.Now() now := time.Now()
tweets := timeline(endpoint, true) tweets := timeline(endpoint)
for { for {
for _, tweet := range tweets { for _, tweet := range tweets {
daysSince := now.Sub(tweet.Created_at.Time).Hours() / 24 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) { if !equals(newTweets, tweets) {
tweets = newTweets tweets = newTweets
} else { } else {
@ -791,10 +794,52 @@ func wipeCommand() (goutil.CommandFlagsInit, goutil.CommandFunc) {
return flagsInit, func([]string) error { return wipe(f) } 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 client *http.Client
var homeFilter hashset
var mentionsFilter hashset
func main() { func main() {
client = getClient() client = getClient()
setFilters(appDir())
commands := []goutil.Command{ commands := []goutil.Command{
goutil.NewCommand("status", status, "post a status with message and/or media"), goutil.NewCommand("status", status, "post a status with message and/or media"),
goutil.NewCommand("home", home, "get your home timeline"), goutil.NewCommand("home", home, "get your home timeline"),