2017-12-30 00:11:28 +00:00
|
|
|
#!/usr/bin/env python3
|
2017-06-17 17:55:52 +00:00
|
|
|
|
2018-03-24 15:26:35 +00:00
|
|
|
import logging
|
2017-12-30 00:11:28 +00:00
|
|
|
import tweepy
|
2018-01-18 13:48:53 +00:00
|
|
|
import re
|
2017-06-17 17:55:52 +00:00
|
|
|
import requests
|
2018-09-09 15:29:06 +00:00
|
|
|
from time import time
|
2018-01-18 12:54:32 +00:00
|
|
|
import report
|
2018-03-28 21:33:04 +00:00
|
|
|
from bot import Bot
|
2018-03-23 16:35:04 +00:00
|
|
|
|
2018-01-07 19:22:32 +00:00
|
|
|
|
2018-03-24 15:26:35 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-03-28 21:33:04 +00:00
|
|
|
class TwitterBot(Bot):
|
2018-08-08 15:09:26 +00:00
|
|
|
|
2018-03-28 21:33:04 +00:00
|
|
|
def get_api(self, user):
|
2018-07-14 14:39:53 +00:00
|
|
|
keys = user.get_twitter_credentials()
|
2017-12-30 00:11:28 +00:00
|
|
|
auth = tweepy.OAuthHandler(consumer_key=keys[0],
|
|
|
|
consumer_secret=keys[1])
|
|
|
|
auth.set_access_token(keys[2], # access_token_key
|
|
|
|
keys[3]) # access_token_secret
|
2018-09-01 12:01:03 +00:00
|
|
|
return tweepy.API(auth, wait_on_rate_limit=True)
|
2017-09-17 17:07:04 +00:00
|
|
|
|
2018-03-28 21:33:04 +00:00
|
|
|
def crawl(self, user):
|
2017-06-17 18:34:18 +00:00
|
|
|
"""
|
|
|
|
crawls all Tweets which mention the bot from the twitter rest API.
|
|
|
|
|
2018-01-18 12:54:32 +00:00
|
|
|
:return: reports: (list of report.Report objects)
|
2017-06-17 18:34:18 +00:00
|
|
|
"""
|
2018-01-18 12:54:32 +00:00
|
|
|
reports = []
|
2018-10-07 20:10:48 +00:00
|
|
|
try:
|
|
|
|
if user.get_last_twitter_request() + 60 > time():
|
|
|
|
return reports
|
|
|
|
except TypeError:
|
|
|
|
user.set_last_twitter_request(time())
|
2018-07-14 14:39:53 +00:00
|
|
|
try:
|
|
|
|
api = self.get_api(user)
|
2018-10-06 08:44:07 +00:00
|
|
|
except TypeError:
|
|
|
|
# When there is no twitter account for this bot, we want to
|
|
|
|
# seamlessly continue.
|
2018-07-14 15:17:36 +00:00
|
|
|
#logger.error("Error Authenticating Twitter", exc_info=True)
|
2018-07-14 14:39:53 +00:00
|
|
|
return reports
|
2018-03-28 21:33:04 +00:00
|
|
|
last_mention = user.get_seen_tweet()
|
2017-09-17 17:07:04 +00:00
|
|
|
try:
|
2018-03-28 21:33:04 +00:00
|
|
|
if last_mention == 0:
|
|
|
|
mentions = api.mentions_timeline()
|
|
|
|
else:
|
2018-04-15 09:42:34 +00:00
|
|
|
mentions = api.mentions_timeline(since_id=last_mention)
|
2018-10-07 20:10:48 +00:00
|
|
|
user.set_last_twitter_request(time())
|
2018-03-28 21:33:04 +00:00
|
|
|
for status in mentions:
|
2018-10-08 21:32:33 +00:00
|
|
|
if status._json['in_reply_to_status_id'] == None:
|
|
|
|
text = re.sub(
|
|
|
|
"(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)",
|
|
|
|
"", status.text)
|
2018-10-05 09:02:12 +00:00
|
|
|
reports.append(report.Report(status.author.screen_name,
|
|
|
|
self,
|
|
|
|
text,
|
|
|
|
status.id,
|
|
|
|
status.created_at))
|
2018-09-24 21:08:29 +00:00
|
|
|
user.save_seen_tweet(status.id)
|
2018-03-28 21:33:04 +00:00
|
|
|
return reports
|
2017-12-30 00:11:28 +00:00
|
|
|
except tweepy.RateLimitError:
|
2018-03-24 15:26:35 +00:00
|
|
|
logger.error("Twitter API Error: Rate Limit Exceeded",
|
|
|
|
exc_info=True)
|
2018-03-28 21:33:04 +00:00
|
|
|
# :todo implement rate limiting
|
2017-09-17 17:07:04 +00:00
|
|
|
except requests.exceptions.ConnectionError:
|
2018-03-24 15:26:35 +00:00
|
|
|
logger.error("Twitter API Error: Bad Connection", exc_info=True)
|
2018-01-18 20:48:36 +00:00
|
|
|
except tweepy.TweepError:
|
2018-03-24 15:26:35 +00:00
|
|
|
logger.error("Twitter API Error: General Error", exc_info=True)
|
2018-01-05 16:13:41 +00:00
|
|
|
return []
|
2017-06-17 18:16:03 +00:00
|
|
|
|
2018-03-28 21:33:04 +00:00
|
|
|
def post(self, user, report):
|
2018-08-08 15:09:26 +00:00
|
|
|
try:
|
|
|
|
api = self.get_api(user)
|
2019-02-19 15:16:21 +00:00
|
|
|
except TypeError:
|
2018-08-08 15:09:26 +00:00
|
|
|
return # no twitter account for this user.
|
2018-03-28 21:33:04 +00:00
|
|
|
try:
|
|
|
|
if report.source == self:
|
|
|
|
api.retweet(report.id)
|
|
|
|
else:
|
2018-04-15 09:42:34 +00:00
|
|
|
text = report.text
|
|
|
|
if len(text) > 280:
|
|
|
|
text = text[:280 - 4] + u' ...'
|
|
|
|
api.update_status(status=text)
|
2018-03-28 21:33:04 +00:00
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
logger.error("Twitter API Error: Bad Connection",
|
|
|
|
exc_info=True)
|
2018-09-24 21:02:10 +00:00
|
|
|
except tweepy.error.TweepError:
|
|
|
|
logger.error("Twitter API Error", exc_info=True)
|