twitools/retweeter.py

44 lines
1.2 KiB
Python
Raw Normal View History

2016-12-07 11:13:47 +00:00
#!/usr/bin/env python3
2016-12-06 21:42:06 +00:00
import argparse, tools
def getSavepoint(db, user):
2016-12-07 11:12:17 +00:00
db.executeQuery("SELECT MAX(tweet_id) FROM retweets WHERE LOWER(author) = '%s'" % user.lower())
2016-12-06 21:42:06 +00:00
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:
2016-12-07 11:12:17 +00:00
if (status.text[0] != "@" or two.whoami() in status.text) and status.text[0:2] != "RT":
2016-12-06 21:42:06 +00:00
timestamp = status.created_at.strftime('%Y-%m-%d %H:%M:%S') + " +0000"
text = unescapeText(status.text)
2016-12-07 11:12:17 +00:00
db.executeQuery("INSERT INTO retweets('id','author','created_at','text') VALUES(" + str(status.id) + ",'" + user.lower() + "','" + timestamp + "','" + text + "')")
2016-12-06 21:42:06 +00:00
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:])