replace filter lists for home/mentions by allowlists

This commit is contained in:
gutmet 2020-10-23 19:20:34 +02:00
parent 0b52059faf
commit 9455f80034

View File

@ -19,6 +19,8 @@ import (
const ( const (
CHARACTER_LIMIT = 280 CHARACTER_LIMIT = 280
ALLOWLIST_MENTIONS = "AllowlistMentions"
ALLOWLIST_HOME = "AllowlistHome"
WIPE_KEEP_DAYS = 10 WIPE_KEEP_DAYS = 10
STATUS_ENDPOINT = "https://api.twitter.com/1.1/statuses/update.json" STATUS_ENDPOINT = "https://api.twitter.com/1.1/statuses/update.json"
MAX_TIMELINE_REQUESTS = 15 MAX_TIMELINE_REQUESTS = 15
@ -235,17 +237,11 @@ func updateStatus(args []string, previous ObjectID, embedTweet ObjectID) []Statu
return d.push(previous) return d.push(previous)
} }
func PrintTweets(tweets []Status, userFilter hashset) { func PrintTweets(tweets []Status) {
filtered := []Status{}
for _, tweet := range tweets {
if !userFilter.contains(tweet.User.Screen_name) {
filtered = append(filtered, tweet)
}
}
if formatTemplate != nil { if formatTemplate != nil {
optLogFatal("printTweets", formatTemplate.Execute(os.Stdout, filtered)) optLogFatal("printTweets", formatTemplate.Execute(os.Stdout, tweets))
} else { } else {
for _, tweet := range filtered { for _, tweet := range tweets {
fmt.Println(tweet.String()) fmt.Println(tweet.String())
fmt.Println("---------") fmt.Println("---------")
} }
@ -255,21 +251,21 @@ func PrintTweets(tweets []Status, userFilter hashset) {
func status(args []string) error { func status(args []string) error {
checkUsage(args, 1, -1, "status STATUS [FILE1 FILE2 ...]") checkUsage(args, 1, -1, "status STATUS [FILE1 FILE2 ...]")
tweets := updateStatus(args, "", "") tweets := updateStatus(args, "", "")
PrintTweets(tweets, nil) PrintTweets(tweets)
return nil return nil
} }
func reply(args []string) error { func reply(args []string) error {
checkUsage(args, 2, -1, "reply TWEET_ID MESSAGE [FILE1 FILE2 ...]") checkUsage(args, 2, -1, "reply TWEET_ID MESSAGE [FILE1 FILE2 ...]")
tweets := updateStatus(args[1:], ObjectID(args[0]), "") tweets := updateStatus(args[1:], ObjectID(args[0]), "")
PrintTweets(tweets, nil) PrintTweets(tweets)
return nil return nil
} }
func quote(args []string) error { func quote(args []string) error {
checkUsage(args, 2, -1, "quote TWEET_ID MESSAGE [FILE1 FILE2 ...]") checkUsage(args, 2, -1, "quote TWEET_ID MESSAGE [FILE1 FILE2 ...]")
tweets := updateStatus(args[1:], "", ObjectID(args[0])) tweets := updateStatus(args[1:], "", ObjectID(args[0]))
PrintTweets(tweets, nil) PrintTweets(tweets)
return nil return nil
} }
@ -285,7 +281,7 @@ func _lookup(ids []string) []Status {
func lookup(args []string) error { func lookup(args []string) error {
checkUsage(args, 1, -1, "lookup TWEET_ID1 [TWEET_ID2 TWEET_ID3 ...]") checkUsage(args, 1, -1, "lookup TWEET_ID1 [TWEET_ID2 TWEET_ID3 ...]")
tweets := _lookup(args) tweets := _lookup(args)
PrintTweets(tweets, nil) PrintTweets(tweets)
return nil return nil
} }
@ -301,7 +297,7 @@ func timeline(endpoint string, maxID string) []Status {
return tweets return tweets
} }
func timelineLoop(endpoint string, flags timelineFlags) (tweets []Status) { func timelineLoop(endpoint string, flags timelineFlags, allowlist hashset) (tweets []Status) {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
fmt.Fprintln(os.Stderr, "INFO:", r) fmt.Fprintln(os.Stderr, "INFO:", r)
@ -317,7 +313,11 @@ func timelineLoop(endpoint string, flags timelineFlags) (tweets []Status) {
var lowestSoFar int64 var lowestSoFar int64
lowestSoFar, _ = strconv.ParseInt(tmp[len(tmp)-1].Id_str, 10, 64) lowestSoFar, _ = strconv.ParseInt(tmp[len(tmp)-1].Id_str, 10, 64)
maxID = strconv.FormatInt(lowestSoFar-1, 10) maxID = strconv.FormatInt(lowestSoFar-1, 10)
tweets = append(tweets, tmp...) for _, tweet := range tmp {
if allowlist == nil || allowlist.contains(tweet.User.Screen_name) {
tweets = append(tweets, tweet)
}
}
if len(tweets) > flags.count { if len(tweets) > flags.count {
tweets = tweets[:flags.count] tweets = tweets[:flags.count]
} }
@ -340,8 +340,9 @@ func timelineFlagsVars(s *flag.FlagSet, f *timelineFlags) {
func mentions(flags timelineFlags, args []string) error { func mentions(flags timelineFlags, args []string) error {
checkUsage(args, 0, 0, "mentions") checkUsage(args, 0, 0, "mentions")
tweets := timelineLoop(MENTIONS_ENDPOINT, flags) allowlist := getHashset(ALLOWLIST_MENTIONS)
PrintTweets(tweets, mentionsFilter) tweets := timelineLoop(MENTIONS_ENDPOINT, flags, allowlist)
PrintTweets(tweets)
return nil return nil
} }
@ -355,8 +356,9 @@ func mentionsCommand() (goutil.CommandFlagsInit, goutil.CommandFunc) {
func home(flags timelineFlags, args []string) error { func home(flags timelineFlags, args []string) error {
checkUsage(args, 0, 0, "home") checkUsage(args, 0, 0, "home")
tweets := timelineLoop(HOME_ENDPOINT, flags) allowlist := getHashset(ALLOWLIST_HOME)
PrintTweets(tweets, homeFilter) tweets := timelineLoop(HOME_ENDPOINT, flags, allowlist)
PrintTweets(tweets)
return nil return nil
} }
@ -370,8 +372,8 @@ func homeCommand() (goutil.CommandFlagsInit, goutil.CommandFunc) {
func userTimeline(flags userTimelineFlags, args []string) error { func userTimeline(flags userTimelineFlags, args []string) error {
checkUsage(args, 1, 1, "timeline USER") checkUsage(args, 1, 1, "timeline USER")
tweets := timelineLoop(TIMELINE_ENDPOINT+UserTimelineParameters(flags, args[0]), flags.timelineFlags) tweets := timelineLoop(TIMELINE_ENDPOINT+UserTimelineParameters(flags, args[0]), flags.timelineFlags, nil)
PrintTweets(tweets, nil) PrintTweets(tweets)
return nil return nil
} }
@ -401,7 +403,7 @@ func retweet(args []string) error {
var retweet Status var retweet Status
err := json.Unmarshal(body, &retweet) err := json.Unmarshal(body, &retweet)
log(err) log(err)
PrintTweets([]Status{retweet}, nil) PrintTweets([]Status{retweet})
return nil return nil
} }
@ -412,7 +414,7 @@ func like(args []string) error {
var tweet Status var tweet Status
err := json.Unmarshal(body, &tweet) err := json.Unmarshal(body, &tweet)
log(err) log(err)
PrintTweets([]Status{tweet}, nil) PrintTweets([]Status{tweet})
return nil return nil
} }
@ -513,20 +515,18 @@ func (s hashset) contains(member string) bool {
} }
} }
func setFilters(appDir string) { func getHashset(name string) (set hashset) {
getHashset := func(s string, err error) hashset { fullpath := filepath.Join(appDir(), name)
set := makeHashset() s, err := goutil.ReadFile(fullpath)
if err == nil { if err == nil {
set = makeHashset()
for _, id := range strings.Split(s, "\n") { for _, id := range strings.Split(s, "\n") {
if trimmed := strings.TrimSpace(id); trimmed != "" { if trimmed := strings.TrimSpace(id); trimmed != "" {
set.add(trimmed) set.add(trimmed)
} }
} }
} }
return set return
}
homeFilter = getHashset(goutil.ReadFile(filepath.Join(appDir, "FilterHome")))
mentionsFilter = getHashset(goutil.ReadFile(filepath.Join(appDir, "FilterMentions")))
} }
type generalFlags struct { type generalFlags struct {
@ -559,8 +559,6 @@ func wrapCommandFl(cmd func() (goutil.CommandFlagsInit, goutil.CommandFunc)) fun
} }
var client *http.Client var client *http.Client
var homeFilter hashset
var mentionsFilter hashset
var formatTemplate *template.Template var formatTemplate *template.Template
func main() { func main() {
@ -571,7 +569,6 @@ func main() {
} }
}() }()
client = getClient() client = getClient()
setFilters(appDir())
commands := []goutil.Command{ commands := []goutil.Command{
goutil.NewCommandWithFlags("status", wrapCommand(status), "post a status with message and/or media"), goutil.NewCommandWithFlags("status", wrapCommand(status), "post a status with message and/or media"),
goutil.NewCommandWithFlags("home", wrapCommandFl(homeCommand), "get your home timeline"), goutil.NewCommandWithFlags("home", wrapCommandFl(homeCommand), "get your home timeline"),