aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar jesopo2019-05-29 12:19:53 +0100
committerGravatar jesopo2019-05-29 12:19:53 +0100
commit2f6dda903097f2442f2dd88279e853ea7ccefba9 (patch)
tree20f86a89fe271cc28042c57648e1ad1afac45c60
parentBetter CHANGELOG line about #56 (diff)
signature
First draft of following/unfollowing tweets in channels
-rw-r--r--modules/tweets/__init__.py44
1 files changed, 41 insertions, 3 deletions
diff --git a/modules/tweets/__init__.py b/modules/tweets/__init__.py
index 10e3ee5b..b0af2bd9 100644
--- a/modules/tweets/__init__.py
+++ b/modules/tweets/__init__.py
@@ -1,3 +1,5 @@
+#--depends-on commands
+#--depends-on permissions
#--require-config twitter-api-key
#--require-config twitter-api-secret
#--require-config twitter-access-token
@@ -34,6 +36,7 @@ class BitBotStreamListener(tweepy.StreamListener):
for server, channel in follows:
self.events.on("send.stdout").call(target=channel,
module_name="Tweets", server=server, message=tweet)
+
@utils.export("channelset", {"setting": "auto-tweet",
"help": "Enable/disable automatically getting tweet info",
"validate": utils.bool_or_none, "example": "on"})
@@ -71,14 +74,49 @@ class Module(ModuleManager.BaseModule):
def _start_stream(self):
self._dispose_stream()
- auth = self._get_auth()
- self._stream = tweepy.Stream(auth=auth, listener=BitBotStreamListener)
-
usernames = set([])
for server_id, channel_name, value in _get_follows():
usernames.add(value)
+ if not usernames:
+ return False
+
+ auth = self._get_auth()
+ self._stream = tweepy.Stream(auth=auth, listener=BitBotStreamListener)
self._stream.filter(follow=list(usernames), is_async=True)
+ return True
+
+ @utils.hook("received.command.tfollow", min_args=2, channel_only=True)
+ def tfollow(self, event):
+ """
+ :help: Stream tweets from a given account to the current channel
+ :usage: add|remove @<username>
+ :permission: twitter-follow
+ """
+ username = event["args_split"][0]
+ if username.startswith("@"):
+ username = username[1:]
+
+ subcommand = event["args_split"][0].lower()
+ follows = event["target"].get_setting("twitter-follow", [])
+ action = None
+
+ if subcommand == "add":
+ action = "followed"
+ if username in follows:
+ raise utils.EventError("Already following %s" % username)
+ follows.append(username)
+ elif subcommand == "remove":
+ action = "unfollowed"
+ if not username in follows:
+ raise utils.EventError("Not following %s" % username)
+ follows.remove(username)
+ else:
+ raise utils.EventError("Unknown subcommand")
+
+ event["target"].set_setting("twitter-follow", follows)
+ self._start_stream()
+ event["stdout"].write("%s @%s" % (action.title(), username))
@utils.hook("received.command.tw", alias_of="tweet")
@utils.hook("received.command.tweet")