ticketfrei/active_bots/twitterbot.py

191 lines
6.0 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2017-06-17 17:55:52 +00:00
2018-03-24 15:26:35 +00:00
from config import config
import logging
import tweepy
import re
2017-06-17 17:55:52 +00:00
import requests
from time import sleep
import report
2018-03-23 16:35:04 +00:00
from user import User
2018-03-24 15:26:35 +00:00
logger = logging.getLogger(__name__)
class TwitterBot(object):
2017-06-17 17:55:52 +00:00
"""
This bot retweets all tweets which
1) mention him,
2) contain at least one of the triggerwords provided.
2017-06-17 17:55:52 +00:00
2017-06-25 17:31:40 +00:00
api: The api object, generated with your oAuth keys, responsible for
communication with twitter rest API
2017-06-17 17:55:52 +00:00
last_mention: the ID of the last tweet which mentioned you
"""
2018-03-28 18:24:21 +00:00
def __init__(self, uid):
2017-06-17 17:55:52 +00:00
"""
Initializes the bot and loads all the necessary data.
2017-06-25 17:31:40 +00:00
Tweet
2017-06-17 17:55:52 +00:00
"""
2018-03-28 18:24:21 +00:00
self.user = User(uid)
# initialize API access
keys = self.get_api_keys()
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
self.api = tweepy.API(auth)
2018-03-23 16:35:04 +00:00
self.last_mention = self.user.get_seen_tweet()
self.waitcounter = 0
def get_api_keys(self):
2017-06-17 17:55:52 +00:00
"""
How to get these keys is described in doc/twitter_api.md
2017-12-30 09:32:20 +00:00
After you received keys, store them in your config.toml like this:
[tapp]
consumer_key = "..."
consumer_secret = "..."
[tuser]
access_token_key = "..."
access_token_secret = "..."
:return: keys: list of these 4 strings.
2017-06-17 17:55:52 +00:00
"""
2018-03-24 15:26:35 +00:00
keys = [config['twitter']['consumer_key'],
config['twitter']['consumer_secret']]
2018-03-23 16:35:04 +00:00
row = self.user.get_twitter_token()
keys.append(row[0])
keys.append(row[1])
2017-06-17 17:55:52 +00:00
return keys
def save_last(self):
2017-07-19 10:17:15 +00:00
""" Saves the last retweeted tweet in last_mention. """
2018-03-23 16:35:04 +00:00
self.user.save_seen_tweet(self.last_mention)
2017-07-19 10:17:15 +00:00
def waiting(self):
"""
If the counter is not 0, you should be waiting instead.
:return: self.waitcounter(int): if 0, do smth.
"""
if self.waitcounter > 0:
sleep(1)
self.waitcounter -= 1
return self.waitcounter
def crawl(self):
"""
crawls all Tweets which mention the bot from the twitter rest API.
:return: reports: (list of report.Report objects)
"""
reports = []
try:
if not self.waiting():
if self.last_mention == 0:
mentions = self.api.mentions_timeline()
else:
2018-03-24 15:26:35 +00:00
mentions = self.api.mentions_timeline(
since_id=self.last_mention)
for status in mentions:
2018-03-24 15:26:35 +00:00
text = re.sub(
"(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)",
"", status.text)
reports.append(report.Report(status.author.screen_name,
"twitter",
text,
status.id,
status.created_at))
self.save_last()
return reports
except tweepy.RateLimitError:
2018-03-24 15:26:35 +00:00
logger.error("Twitter API Error: Rate Limit Exceeded",
exc_info=True)
self.waitcounter += 60*15 + 1
except requests.exceptions.ConnectionError:
2018-03-24 15:26:35 +00:00
logger.error("Twitter API Error: Bad Connection", exc_info=True)
self.waitcounter += 10
except tweepy.TweepError:
2018-03-24 15:26:35 +00:00
logger.error("Twitter API Error: General Error", exc_info=True)
return []
def repost(self, status):
"""
Retweets a given tweet.
:param status: (report.Report object)
:return: toot: string of the tweet, to toot on mastodon.
"""
while 1:
try:
self.api.retweet(status.id)
2018-03-24 15:26:35 +00:00
logger.info("Retweeted: " + status.format())
2017-07-19 10:17:15 +00:00
if status.id > self.last_mention:
self.last_mention = status.id
2018-01-18 19:15:41 +00:00
self.save_last()
return status.format()
except requests.exceptions.ConnectionError:
2018-03-24 15:26:35 +00:00
logger.error("Twitter API Error: Bad Connection",
exc_info=True)
sleep(10)
# maybe one day we get rid of this error:
except tweepy.TweepError:
2018-03-24 15:26:35 +00:00
logger.error("Twitter Error", exc_info=True)
if status.id > self.last_mention:
self.last_mention = status.id
2018-01-18 19:15:41 +00:00
self.save_last()
return None
def post(self, status):
"""
Tweet a post.
:param status: (report.Report object)
"""
text = status.format()
if len(text) > 280:
text = status.text[:280 - 4] + u' ...'
while 1:
try:
self.api.update_status(status=text)
return
except requests.exceptions.ConnectionError:
2018-03-24 15:26:35 +00:00
logger.error("Twitter API Error: Bad Connection",
exc_info=True)
sleep(10)
def flow(self, trigger, to_tweet=()):
""" The flow of crawling mentions and retweeting them.
:param to_tweet: list of strings to tweet
:return list of retweeted tweets, to toot on mastodon
"""
# Tweet the reports from other sources
for post in to_tweet:
self.post(post)
2017-06-17 17:55:52 +00:00
# Store all mentions in a list of Status Objects
mentions = self.crawl()
# initialise list of strings for other bots
all_tweets = []
for status in mentions:
# Is the Text of the Tweet in the triggerlist?
if trigger.is_ok(status.text):
# Retweet status
toot = self.repost(status)
if toot:
all_tweets.append(toot)
# Return Retweets for posting on other bots
return all_tweets