twitools/retweeter.py
2017-09-08 22:06:18 +02:00

43 lines
1.3 KiB
Python
Executable file

#!/usr/bin/env python3
import argparse, dbtools, setuptools, twitools
def getSavepoint(db, user):
db.executeQuery("SELECT MAX(id) FROM retweets WHERE LOWER(author) = '%s'" % user.lower())
try:
return int(db.getNext()[0])
except:
print("No tweets from %s stored yet." % user)
return 0
def retweet(user, two=twitools.twObject(), db=dbtools.dbHelper()):
query = "from:" + user
savepoint = getSavepoint(db, user)
last = savepoint
timeline = two.search(query, savepoint)
tw_counter = 0
for status in timeline:
if (status.full_text[0] != "@" or two.whoami() in status.full_text) and status.full_text[0:2] != "RT":
timestamp = status.created_at.strftime('%Y-%m-%d %H:%M:%S') + " +0000"
text = setuptools.unescapeText(status.full_text)
db.executeQuery("INSERT INTO retweets('id','author','created_at','text') VALUES(" + str(status.id) + ",'" + user.lower() + "','" + timestamp + "','" + text + "')")
db.commit()
two.retweet(status.id)
last = status.id
tw_counter = tw_counter + 1
db.closeConnection()
return tw_counter
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:])