This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/disabled_stuff/twitter.py

179 lines
5.0 KiB
Python
Raw Normal View History

2013-07-01 17:28:46 +02:00
import re
import random
from datetime import datetime
2011-11-20 10:23:31 +01:00
2014-02-14 04:36:57 +01:00
import tweepy
from util import hook, timesince
2013-12-01 07:28:24 +01:00
TWITTER_RE = (r"(?:(?:www.twitter.com|twitter.com)/(?:[-_a-zA-Z0-9]+)/status/)([0-9]+)", re.I)
2012-05-17 01:52:31 +02:00
2013-12-01 07:37:12 +01:00
def get_api(bot):
2013-07-01 17:28:46 +02:00
consumer_key = bot.config.get("api_keys", {}).get("twitter_consumer_key")
consumer_secret = bot.config.get("api_keys", {}).get("twitter_consumer_secret")
2011-11-20 10:23:31 +01:00
2013-07-01 17:28:46 +02:00
oauth_token = bot.config.get("api_keys", {}).get("twitter_access_token")
oauth_secret = bot.config.get("api_keys", {}).get("twitter_access_secret")
2011-11-20 10:23:31 +01:00
2013-07-01 17:28:46 +02:00
if not consumer_key:
2013-12-01 07:37:12 +01:00
return False
2011-11-20 10:23:31 +01:00
2013-07-01 17:28:46 +02:00
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(oauth_token, oauth_secret)
2011-11-20 10:23:31 +01:00
2013-12-01 07:37:12 +01:00
return tweepy.API(auth)
@hook.regex(*TWITTER_RE)
def twitter_url(match, bot=None):
2014-03-19 05:40:21 +01:00
# Find the tweet ID from the URL
2013-12-01 07:37:12 +01:00
tweet_id = match.group(1)
2014-02-13 02:34:50 +01:00
2014-03-19 05:40:21 +01:00
# Get the tweet using the tweepy API
2013-12-01 07:37:12 +01:00
api = get_api(bot)
if not api:
return
try:
tweet = api.get_status(tweet_id)
user = tweet.user
except tweepy.error.TweepError:
return
2014-03-19 05:40:21 +01:00
# Format the return the text of the tweet
2013-12-01 07:37:12 +01:00
text = " ".join(tweet.text.split())
if user.verified:
prefix = u"\u2713"
else:
prefix = ""
time = timesince.timesince(tweet.created_at, datetime.utcnow())
return u"{}@\x02{}\x02 ({}): {} ({} ago)".format(prefix, user.screen_name, user.name, text, time)
2014-02-13 02:34:50 +01:00
2013-12-01 07:37:12 +01:00
@hook.command("tw")
@hook.command("twatter")
@hook.command
def twitter(inp, bot=None):
2014-02-13 12:47:01 +01:00
"""twitter <user> [n] -- Gets last/[n]th tweet from <user>"""
2013-12-01 07:37:12 +01:00
api = get_api(bot)
if not api:
return "Error: No Twitter API details."
2012-03-23 01:32:48 +01:00
if re.match(r'^\d+$', inp):
# user is getting a tweet by id
try:
# get tweet by id
tweet = api.get_status(inp)
except tweepy.error.TweepError as e:
if e[0][0]['code'] == 34:
return "Could not find tweet."
else:
2013-11-28 23:13:11 +01:00
return u"Error {}: {}".format(e[0][0]['code'], e[0][0]['message'])
user = tweet.user
elif re.match(r'^\w{1,15}$', inp) or re.match(r'^\w{1,15}\s+\d+$', inp):
# user is getting a tweet by name
2013-07-01 17:28:46 +02:00
if inp.find(' ') == -1:
username = inp
tweet_number = 0
else:
username, tweet_number = inp.split()
tweet_number = int(tweet_number) - 1
2012-02-29 09:29:53 +01:00
2013-07-01 17:28:46 +02:00
if tweet_number > 300:
return "This command can only find the last \x02300\x02 tweets."
try:
# try to get user by username
user = api.get_user(username)
except tweepy.error.TweepError as e:
if e[0][0]['code'] == 34:
return "Could not find user."
else:
2013-11-28 23:13:11 +01:00
return u"Error {}: {}".format(e[0][0]['code'], e[0][0]['message'])
# get the users tweets
user_timeline = api.user_timeline(id=user.id, count=tweet_number + 1)
# if the timeline is empty, return an error
if not user_timeline:
2013-11-28 23:13:11 +01:00
return u"The user \x02{}\x02 has no tweets.".format(user.screen_name)
# grab the newest tweet from the users timeline
try:
tweet = user_timeline[tweet_number]
except IndexError:
tweet_count = len(user_timeline)
2013-11-28 23:13:11 +01:00
return u"The user \x02{}\x02 only has \x02{}\x02 tweets.".format(user.screen_name, tweet_count)
elif re.match(r'^#\w+$', inp):
# user is searching by hashtag
search = api.search(inp)
if not search:
return "No tweets found."
tweet = random.choice(search)
user = tweet.user
2014-02-13 12:47:01 +01:00
else:
# ???
2014-03-19 05:40:21 +01:00
return "Invalid Input"
2013-07-01 17:28:46 +02:00
2014-03-19 05:40:21 +01:00
# Format the return the text of the tweet
text = " ".join(tweet.text.split())
if user.verified:
2013-11-28 23:18:25 +01:00
prefix = u"\u2713"
else:
prefix = ""
time = timesince.timesince(tweet.created_at, datetime.utcnow())
2012-02-29 09:29:53 +01:00
return u"{}@\x02{}\x02 ({}): {} ({} ago)".format(prefix, user.screen_name, user.name, text, time)
2012-02-25 14:32:48 +01:00
2011-11-20 10:23:31 +01:00
@hook.command("twinfo")
2011-11-20 10:23:31 +01:00
@hook.command
2013-07-01 17:28:46 +02:00
def twuser(inp, bot=None):
2013-09-04 12:30:04 +02:00
"""twuser <user> -- Get info on the Twitter user <user>"""
2013-07-01 17:28:46 +02:00
2013-12-01 07:37:12 +01:00
api = get_api(bot)
if not api:
return "Error: No Twitter API details."
2013-07-01 17:28:46 +02:00
2011-11-20 10:23:31 +01:00
try:
2013-07-01 17:28:46 +02:00
# try to get user by username
user = api.get_user(inp)
except tweepy.error.TweepError as e:
if e[0][0]['code'] == 34:
return "Could not find user."
else:
return "Unknown error"
2011-11-20 10:23:31 +01:00
if user.verified:
2013-11-28 23:18:25 +01:00
prefix = u"\u2713"
else:
prefix = ""
if user.location:
2013-11-28 23:13:11 +01:00
loc_str = u" is located in \x02{}\x02 and".format(user.location)
else:
loc_str = ""
if user.description:
2013-11-28 23:13:11 +01:00
desc_str = u" The users description is \"{}\"".format(user.description)
else:
desc_str = ""
return u"{}@\x02{}\x02 ({}){} has \x02{:,}\x02 tweets and \x02{:,}\x02 followers.{}" \
"".format(prefix, user.screen_name, user.name, loc_str, user.statuses_count, user.followers_count,
desc_str)