2019-05-29 11:19:53 +00:00
|
|
|
#--depends-on commands
|
|
|
|
#--depends-on permissions
|
2019-06-25 16:53:00 +00:00
|
|
|
#--depends-on shorturl
|
2019-05-27 13:52:08 +00:00
|
|
|
#--require-config twitter-api-key
|
|
|
|
#--require-config twitter-api-secret
|
|
|
|
#--require-config twitter-access-token
|
|
|
|
#--require-config twitter-access-secret
|
|
|
|
|
2019-06-09 14:15:50 +00:00
|
|
|
import json, re, threading
|
2019-05-27 13:52:08 +00:00
|
|
|
from src import ModuleManager, utils
|
|
|
|
from . import format
|
|
|
|
import tweepy
|
|
|
|
|
|
|
|
_bot = None
|
|
|
|
_events = None
|
|
|
|
_exports = None
|
2019-06-28 06:24:06 +00:00
|
|
|
_log = None
|
2019-05-27 13:52:08 +00:00
|
|
|
|
|
|
|
REGEX_TWITTERURL = re.compile(
|
2019-10-20 16:10:01 +00:00
|
|
|
"https?://(?:www\.|mobile\.)?twitter.com/[^/]+/status/(\d+)", re.I)
|
2019-05-27 13:52:08 +00:00
|
|
|
|
|
|
|
def _get_follows():
|
|
|
|
return _bot.database.channel_settings.find_by_setting("twitter-follow")
|
|
|
|
|
|
|
|
class BitBotStreamListener(tweepy.StreamListener):
|
|
|
|
def on_status(self, status):
|
2019-06-07 21:01:13 +00:00
|
|
|
_bot.trigger(lambda: self._on_status(status))
|
|
|
|
def _on_status(self, status):
|
2019-06-28 06:24:06 +00:00
|
|
|
_log.debug("Got tweet from stream: %s", [status])
|
2019-06-07 21:01:13 +00:00
|
|
|
given_username = status.user.screen_name.lower()
|
2019-05-27 13:52:08 +00:00
|
|
|
|
|
|
|
follows = []
|
|
|
|
for server_id, channel_name, value in _get_follows():
|
2019-06-07 21:01:13 +00:00
|
|
|
for username in value:
|
|
|
|
if username.lower() == given_username:
|
|
|
|
server = _bot.get_server_by_id(server_id)
|
|
|
|
if server and channel_name in server.channels:
|
|
|
|
follows.append([server, server.channels.get(channel_name)])
|
2019-05-27 13:52:08 +00:00
|
|
|
|
|
|
|
for server, channel in follows:
|
2019-06-25 16:53:00 +00:00
|
|
|
tweet = format._tweet(_exports, server, status)
|
2019-06-07 21:01:13 +00:00
|
|
|
_events.on("send.stdout").call(target=channel,
|
2019-05-27 13:52:08 +00:00
|
|
|
module_name="Tweets", server=server, message=tweet)
|
2019-05-29 11:19:53 +00:00
|
|
|
|
2019-06-28 22:16:05 +00:00
|
|
|
@utils.export("channelset", utils.BoolSetting("auto-tweet",
|
|
|
|
"Enable/disable automatically getting tweet info"))
|
2019-05-27 13:52:08 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
|
|
|
_stream = None
|
|
|
|
def on_load(self):
|
2019-06-09 14:15:50 +00:00
|
|
|
self._thread = None
|
|
|
|
|
2019-05-27 13:52:08 +00:00
|
|
|
global _bot
|
|
|
|
global _events
|
|
|
|
global _exports
|
2019-06-28 06:24:06 +00:00
|
|
|
global _log
|
2019-05-27 13:52:08 +00:00
|
|
|
_bot = self.bot
|
|
|
|
_events = self.events
|
|
|
|
_exports = self.exports
|
2019-06-28 06:24:06 +00:00
|
|
|
_log = self.log
|
2019-06-07 21:01:13 +00:00
|
|
|
self._start_stream()
|
|
|
|
|
2019-05-27 13:52:08 +00:00
|
|
|
def unload(self):
|
|
|
|
self._dispose_stream()
|
|
|
|
|
|
|
|
def _dispose_stream(self):
|
|
|
|
if not self._stream == None:
|
|
|
|
self._stream.disconnect()
|
|
|
|
|
|
|
|
def _get_auth(self):
|
|
|
|
auth = tweepy.OAuthHandler(self.bot.config["twitter-api-key"],
|
|
|
|
self.bot.config["twitter-api-secret"])
|
|
|
|
auth.set_access_token(self.bot.config["twitter-access-token"],
|
|
|
|
self.bot.config["twitter-access-secret"])
|
|
|
|
return auth
|
|
|
|
def _get_api(self, auth):
|
|
|
|
return tweepy.API(auth)
|
|
|
|
|
|
|
|
def _from_id(self, tweet_id):
|
2019-07-15 13:27:37 +00:00
|
|
|
return self._get_api(self._get_auth()).get_status(tweet_id,
|
|
|
|
tweet_mode="extended")
|
2019-05-27 13:52:08 +00:00
|
|
|
def _from_username(self, username):
|
|
|
|
return self._get_api(self._get_auth()).user_timeline(
|
2019-09-05 13:46:39 +00:00
|
|
|
screen_name=username, count=1, tweet_mode="extended")[0]
|
2019-05-27 13:52:08 +00:00
|
|
|
|
|
|
|
def _start_stream(self):
|
|
|
|
self._dispose_stream()
|
|
|
|
|
|
|
|
usernames = set([])
|
|
|
|
for server_id, channel_name, value in _get_follows():
|
2019-06-07 21:01:13 +00:00
|
|
|
for username in value:
|
|
|
|
usernames.add(username)
|
2019-05-29 11:19:53 +00:00
|
|
|
if not usernames:
|
|
|
|
return False
|
|
|
|
|
|
|
|
auth = self._get_auth()
|
2019-06-07 21:01:13 +00:00
|
|
|
api = self._get_api(auth)
|
|
|
|
|
|
|
|
user_ids = []
|
|
|
|
for username in usernames:
|
|
|
|
user_ids.append(str(api.get_user(screen_name=username).id))
|
|
|
|
|
2019-09-25 14:03:43 +00:00
|
|
|
self._stream = tweepy.Stream(auth=auth, listener=BitBotStreamListener(),
|
|
|
|
tweet_mode="extended")
|
2019-05-27 13:52:08 +00:00
|
|
|
|
2019-06-09 14:15:50 +00:00
|
|
|
self._thread = threading.Thread(
|
|
|
|
target=lambda: self._stream.filter(follow=user_ids))
|
|
|
|
self._thread.daemon = True
|
|
|
|
self._thread.start()
|
2019-05-29 11:19:53 +00:00
|
|
|
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
|
|
|
|
"""
|
2019-06-07 21:01:13 +00:00
|
|
|
username = event["args_split"][1]
|
2019-05-29 11:19:53 +00:00
|
|
|
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))
|
2019-05-27 13:52:08 +00:00
|
|
|
|
|
|
|
@utils.hook("received.command.tw", alias_of="tweet")
|
|
|
|
@utils.hook("received.command.tweet")
|
|
|
|
def tweet(self, event):
|
|
|
|
"""
|
|
|
|
:help: Get/find a tweet
|
|
|
|
:usage: [@username/URL/ID]
|
|
|
|
"""
|
|
|
|
|
|
|
|
if event["args"]:
|
|
|
|
target = event["args"]
|
|
|
|
else:
|
|
|
|
target = event["target"].buffer.find(REGEX_TWITTERURL)
|
|
|
|
if target:
|
2019-08-13 12:48:03 +00:00
|
|
|
target = target.match
|
|
|
|
|
2019-05-27 13:52:08 +00:00
|
|
|
if target:
|
|
|
|
url_match = re.search(REGEX_TWITTERURL, target)
|
|
|
|
if url_match or target.isdigit():
|
|
|
|
tweet_id = url_match.group(1) if url_match else target
|
|
|
|
tweet = self._from_id(tweet_id)
|
|
|
|
else:
|
|
|
|
if target.startswith("@"):
|
|
|
|
target = target[1:]
|
|
|
|
tweet = self._from_username(target)
|
|
|
|
|
|
|
|
if tweet:
|
2019-10-08 13:21:15 +00:00
|
|
|
tweet_str = format._tweet(self.exports, event["server"], tweet,
|
|
|
|
from_url=not url_match==None)
|
2019-05-27 13:52:08 +00:00
|
|
|
event["stdout"].write(tweet_str)
|
|
|
|
else:
|
|
|
|
event["stderr"].write("Invalid tweet identifiers provided")
|
|
|
|
else:
|
|
|
|
event["stderr"].write("No tweet provided to get information about")
|
|
|
|
|
2019-06-26 13:37:26 +00:00
|
|
|
@utils.hook("command.regex")
|
|
|
|
@utils.kwarg("ignore_action", False)
|
|
|
|
@utils.kwarg("command", "tweet")
|
|
|
|
@utils.kwarg("pattern", REGEX_TWITTERURL)
|
2019-05-27 13:52:08 +00:00
|
|
|
def regex(self, event):
|
|
|
|
if event["target"].get_setting("auto-tweet", False):
|
|
|
|
event.eat()
|
|
|
|
tweet_id = event["match"].group(1)
|
|
|
|
tweet = self._from_id(tweet_id)
|
|
|
|
if tweet:
|
2019-10-08 13:21:15 +00:00
|
|
|
tweet_str = format._tweet(self.exports, event["server"], tweet,
|
|
|
|
from_url=True)
|
2019-05-27 13:52:08 +00:00
|
|
|
event["stdout"].write(tweet_str)
|
|
|
|
|