diff --git a/active_bots/twitterbot.py b/active_bots/twitterbot.py index caf3ec5..7ba2f41 100755 --- a/active_bots/twitterbot.py +++ b/active_bots/twitterbot.py @@ -50,11 +50,11 @@ class TwitterBot(Bot): mentions = api.mentions_timeline(since_id=last_mention) user.set_last_twitter_request(time()) for status in mentions: - text = re.sub( - "(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)", - "", status.text) - username = "@" + api.me().screen_name - if username in status.text: + # don't retweet replies - only mentions. + if user.get_twitter_username() in status.extended_tweet.full_text: + text = re.sub( + "(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)", + "", status.extended_tweet.full_text) reports.append(report.Report(status.author.screen_name, self, text, diff --git a/db.py b/db.py index 8212560..70669d4 100644 --- a/db.py +++ b/db.py @@ -90,6 +90,7 @@ class DB(object): user_id INTEGER, client_id TEXT, client_secret TEXT, + screen_name TEXT, active INTEGER, FOREIGN KEY(user_id) REFERENCES user(id) ); diff --git a/user.py b/user.py index dad9b1c..65cfa6b 100644 --- a/user.py +++ b/user.py @@ -3,6 +3,7 @@ from bottle import response from db import db import jwt from mastodon import Mastodon +import tweepy from pylibscrypt import scrypt_mcf, scrypt_mcf_check @@ -267,10 +268,15 @@ schlitz "oauth_token_secret": request_token[1]} def save_twitter_token(self, access_token, access_token_secret): - db.execute("""INSERT INTO twitter_accounts( - user_id, client_id, client_secret - ) VALUES(?, ?, ?);""", - (self.uid, access_token, access_token_secret)) + auth = tweepy.OAuthHandler(consumer_key=config['twitter']['consumer_key'], + consumer_secret=config['twitter']['consumer_secret']) + auth.set_access_token(access_token, access_token_secret) + api = tweepy.API(auth, wait_on_rate_limit=True) + username = api.me().screen_name + db.execute("""INSERT INTO ( + user_id, client_id, client_secret, screen_name, active + ) VALUES(?, ?, ?, ?, ?);""", + (self.uid, access_token, access_token_secret, username, 1)) db.commit() def get_twitter_token(self): @@ -286,6 +292,11 @@ schlitz keys.append(row[1]) return keys + def get_twitter_username(self): + db.execute("SELECT screen_name FROM twitter_accounts WHERE user_id = ?", + (self.uid, )) + return db.fetchone()[0] + def update_telegram_key(self, apikey): db.execute("UPDATE telegram_accounts SET apikey = ? WHERE user_id = ?;", (apikey, self.uid)) db.commit()