diff --git a/active_bots/twitterDMs.py b/active_bots/twitterDMs.py index e69de29..deb1a8b 100644 --- a/active_bots/twitterDMs.py +++ b/active_bots/twitterDMs.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 + +import logging +import tweepy +import re +import requests +import report +from bot import Bot + + +logger = logging.getLogger(__name__) + + +class TwitterBot(Bot): + def get_api(self, user): + keys = user.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 + return tweepy.API(auth) + + def crawl(self, user): + """ + crawls all Tweets which mention the bot from the twitter rest API. + + :return: reports: (list of report.Report objects) + """ + reports = [] + api = self.get_api(user) + last_dm = user.get_seen_dm() + try: + if last_dm == None: + mentions = api.direct_messages() + else: + mentions = api.mentions_timeline(since_id=last_dm[0]) + for status in mentions: + text = re.sub( + "(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)", + "", status.text) + 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) + except tweepy.TweepError: + logger.error("Twitter API Error: General Error", exc_info=True) + return [] + + def post(self, user, report): + pass diff --git a/active_bots/twitterbot.py b/active_bots/twitterbot.py index 5d8846f..fdf6045 100755 --- a/active_bots/twitterbot.py +++ b/active_bots/twitterbot.py @@ -39,7 +39,7 @@ class TwitterBot(Bot): "(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)", "", status.text) reports.append(report.Report(status.author.screen_name, - "twitter", + self, text, status.id, status.created_at)) diff --git a/db.py b/db.py index 3ad50f9..b6d6cbe 100644 --- a/db.py +++ b/db.py @@ -100,6 +100,15 @@ class DB(object): FOREIGN KEY(twitter_accounts_id) REFERENCES twitter_accounts(id) ); + CREATE TABLE IF NOT EXISTS seen_dms ( + id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, + user_id INTEGER, + twitter_accounts_id INTEGER, + message_id TEXT, + FOREIGN KEY(user_id) REFERENCES user(id) + FOREIGN KEY(twitter_accounts_id) + REFERENCES twitter_accounts(id) + ); CREATE TABLE IF NOT EXISTS mailinglist ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, user_id INTEGER, diff --git a/user.py b/user.py index 82a53bb..40f915a 100644 --- a/user.py +++ b/user.py @@ -108,6 +108,15 @@ class User(object): db.execute("UPDATE seen_tweets SET tweet_id = ? WHERE user_id = ?;", (tweet_id, self.uid)) + def get_seen_dm(self): + db.execute("SELECT message_id FROM seen_dms WHERE user_id = ?;", + (self.uid, )) + return db.cur.fetchone() + + def save_seen_dm(self, tweet_id): + db.execute("UPDATE seen_dms SET message_id = ? WHERE user_id = ?;", + (tweet_id, self.uid)) + def get_mailinglist(self): db.execute("SELECT email FROM mailinglist WHERE user_id = ? AND active = 1;", (self.uid, )) return db.cur.fetchone()[0]