2018-04-15 09:58:19 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import tweepy
|
|
|
|
import re
|
|
|
|
import requests
|
|
|
|
import report
|
2018-10-06 08:44:07 +00:00
|
|
|
from time import time
|
2018-04-15 09:58:19 +00:00
|
|
|
from bot import Bot
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-10-11 19:24:53 +00:00
|
|
|
class TwitterDMListener(Bot):
|
2018-10-11 19:29:02 +00:00
|
|
|
|
2018-04-15 09:58:19 +00:00
|
|
|
def get_api(self, user):
|
2018-10-11 19:29:02 +00:00
|
|
|
keys = user.get_twitter_credentials()
|
2018-04-15 09:58:19 +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)
|
2018-04-15 09:58:19 +00:00
|
|
|
|
|
|
|
def crawl(self, user):
|
|
|
|
"""
|
|
|
|
crawls all Tweets which mention the bot from the twitter rest API.
|
|
|
|
|
|
|
|
:return: reports: (list of report.Report objects)
|
|
|
|
"""
|
|
|
|
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-08-08 15:09:26 +00:00
|
|
|
try:
|
|
|
|
api = self.get_api(user)
|
2018-10-11 19:30:55 +00:00
|
|
|
except TypeError:
|
2018-08-08 15:09:26 +00:00
|
|
|
return reports # no twitter account for this user.
|
2018-04-15 09:58:19 +00:00
|
|
|
last_dm = user.get_seen_dm()
|
|
|
|
try:
|
2018-09-14 10:45:49 +00:00
|
|
|
if last_dm is None:
|
2018-04-15 09:58:19 +00:00
|
|
|
mentions = api.direct_messages()
|
|
|
|
else:
|
2018-10-08 21:32:33 +00:00
|
|
|
mentions = api.direct_messages(since_id=last_dm[0])
|
2018-10-07 20:10:48 +00:00
|
|
|
user.set_last_twitter_request(time())
|
2018-04-15 09:58:19 +00:00
|
|
|
for status in mentions:
|
|
|
|
text = re.sub(
|
2018-09-15 16:50:37 +00:00
|
|
|
"(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)",
|
|
|
|
"", status.text)
|
2018-04-15 09:58:19 +00:00
|
|
|
reports.append(report.Report(status.author.screen_name,
|
|
|
|
"twitterDM",
|
|
|
|
text,
|
|
|
|
status.id,
|
|
|
|
status.created_at))
|
|
|
|
user.save_seen_dm(last_dm)
|
|
|
|
return reports
|
|
|
|
except tweepy.RateLimitError:
|
|
|
|
logger.error("Twitter API Error: Rate Limit Exceeded",
|
|
|
|
exc_info=True)
|
|
|
|
# :todo implement rate limiting
|
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
logger.error("Twitter API Error: Bad Connection", exc_info=True)
|
2018-10-11 20:22:37 +00:00
|
|
|
except tweepy.TweepError as terror:
|
|
|
|
# Waiting for https://github.com/tweepy/tweepy/pull/1109 to get
|
|
|
|
# merged, so direct messages work again
|
|
|
|
if terror.api_code == 34:
|
|
|
|
return reports
|
2018-04-15 09:58:19 +00:00
|
|
|
logger.error("Twitter API Error: General Error", exc_info=True)
|
2018-10-11 20:22:37 +00:00
|
|
|
return reports
|
2018-04-15 09:58:19 +00:00
|
|
|
|
|
|
|
def post(self, user, report):
|
|
|
|
pass
|