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