#!/usr/bin/python # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """A bot that republishes DM Twitter messages sent to it. Put your settings in settings.py. Add """ __author__ = 'eliot@robotskirts.com' __version__ = '0.1' import sys import re import sqlite3 import datetime import twitter import settings def retweet(initial_status_id=None): DB_PATH = settings.DB_DIR + "%s-dm.sqlite" for USER, PASS in settings.ACCOUNTS: connection = sqlite3.connect(DB_PATH % USER) cursor = connection.cursor() if initial_status_id is not None: try: cursor.execute("""INSERT INTO retweets (status_id, timestamp) VALUES ('%s', '%s')""" % (initial_status_id, datetime.datetime.now())) connection.commit() except sqlite3.IntegrityError: # Already inserted pass # Find the last tweet we retweeted cursor.execute("""SELECT MAX(status_id) FROM retweets""") rows = cursor max_status_id = None for row in rows: max_status_id = row[0] break # Create privileged twitter API api = twitter.Api(username=USER, password=PASS) api.SetSource("retweetpy") # Get DMs to our account dms = api.GetDirectMessages(since_id=max_status_id) if len(dms) > 0: for dm in dms: is_admin = False # Build the new tweet retweeting_from = dm.sender_screen_name.lower() # Check if this user is banned. cursor.execute("""SELECT username FROM admins WHERE username='%s'""" % retweeting_from) rows = cursor for row in rows: is_admin = True break if is_admin: new_tweet = dm.text try: # Send it. api.PostUpdate(new_tweet) cursor.execute("""INSERT INTO retweets (status_id, timestamp) VALUES ('%s', '%s')""" % (dm.id, datetime.datetime.now())) connection.commit() except Exception, e: print e cursor.close() connection.close() def admin(username, account): DB_PATH = settings.DB_DIR + "%s-dm.sqlite" connection = sqlite3.connect(DB_PATH % account) cursor = connection.cursor() try: cursor.execute("""INSERT INTO admins (username) VALUES ('%s')""" % username) connection.commit() except sqlite3.IntegrityError: print "Already admin." cursor.close() connection.close() def setup(): DB_PATH = settings.DB_DIR + "%s-dm.sqlite" for USER, PASS in settings.ACCOUNTS: connection = sqlite3.connect(DB_PATH % USER) cursor = connection.cursor() cursor.execute("""SELECT tbl_name FROM sqlite_master""") table_rows = cursor.fetchall() tables = [] for table in table_rows: tables.append(table[0]) if "admins" not in tables: cursor.execute("""CREATE TABLE admins (username TEXT PRIMARY KEY)""") connection.commit() if "retweets" not in tables: cursor.execute("""CREATE TABLE retweets (status_id INTEGER PRIMARY KEY, timestamp TEXT)""") connection.commit() cursor.close() connection.close() if __name__ == "__main__": setup() if len(sys.argv) > 1: to_admin = None admin_account = None for arg in sys.argv: if arg.startswith("--admin="): to_admin = arg.split("=")[1] elif arg.startswith("--account="): admin_account = arg.split("=")[1] if to_admin and admin_account: admin(username=to_admin, account=admin_account) else: retweet(initial_status_id=sys.argv[1]) else: retweet()