twitools/retweeter.py

42 lines
1.1 KiB
Python
Raw Normal View History

2016-12-06 21:42:06 +00:00
import argparse, tools
def getSavepoint(db, user):
db.executeQuery("SELECT max(tweet_id) FROM retweets WHERE author = '%s'" % user)
try:
return int(db.getNext()[0])
except:
print("No tweets from %s stored yet." % user)
return 0
def retweet(user, two=tools.twObject(), db=tools.dbHelper(tools.dbpath())):
query = "from:" + user
savepoint = getSavepoint(db, user)
last = savepoint
timeline = two.search(query, savepoint)
tw_counter = 0
for status in timeline:
if status.text[0] != "@" or two.whoami() in status.text:
timestamp = status.created_at.strftime('%Y-%m-%d %H:%M:%S') + " +0000"
text = unescapeText(status.text)
db.executeQuery("INSERT INTO retweets('id','created_at','text') VALUES(" + str(status.id) + ",'" + timestamp + "','" + text + "')")
db.commit()
two.retweet(status.id)
last = status.id
tw_counter = tw_counter + 1
db.closeConnection()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("user", help="handle (user name) of the source user")
user = parser.parse_args().user
retweet(user if user[0] != "@" else user[1:])