#!/usr/bin/env python3 import dbtools, setuptools, time, twitools def getTweets(db=dbtools.dbHelper(), user=twitools.twObject().whoami(), two=twitools.twObject()): query = "from:" + user savepoint = db.getLatestTweet() + 1 last = savepoint timeline = two.search(query, savepoint) tw_counter = 0 for status in timeline: timestamp = status.created_at.strftime('%Y-%m-%d %H:%M:%S') + " +0000" text = setuptools.unescapeText(status.text) try: db.executeQuery("INSERT INTO tweets(tweet_id,timestamp,text) VALUES(" + str(status.id) + ",'" + timestamp + "','" + text + "')") except: pass db.commit() last = status.id tw_counter = tw_counter + 1 db.closeConnection() return tw_counter, last, savepoint def getMessages(db=dbtools.dbHelper(), two=twitools.twObject()): mcount = 0 savepoint = db.getLatestMessage() + 1 new_messages = two.api.direct_messages(since_id=savepoint, count=200, full_text=True) new_out_messages = two.api.sent_direct_messages(since_id=savepoint, count=200, full_text=True) for m in new_messages: try: db.executeQuery("INSERT INTO messages VALUES(%s, '%s', %s, %s, '%s')" % (m.id, setuptools.unescapeText(m.text), m.sender_id, m.recipient_id, m.created_at)) mcount += 1 except: pass for m in new_out_messages: try: db.executeQuery("INSERT INTO messages VALUES(%s, '%s', %s, %s, '%s')" % (m.id, setuptools.unescapeText(m.text), m.sender_id, m.recipient_id, m.created_at)) mcount += 1 except: pass db.commit() return mcount, savepoint or 0, db.getLatestMessage() def getFollowers(db=dbtools.dbHelper(), two=twitools.twObject()): current = db.getFollowers() new = list(twitools.getNamesByIDs(twitools.getFollowerIDs())) gained = 0 lost = 0 if len(new) == 0: print("Something went wrong here. -.-") return 0,0 for follower in new: if follower not in current: db.executeQuery("INSERT INTO followers VALUES('%s', %i, NULL)" % (follower, int(time.time()))) print("New follower: %s" % follower) gained += 1 for follower in current: if follower not in new: db.executeQuery("UPDATE followers SET `until` = %i WHERE `id` = '%s' AND `until` IS NULL" % (int(time.time()), follower)) print("Lost follower: %s" % follower) lost += 1 db.commit() return gained, lost def getFollowing(db=dbtools.dbHelper(), two=twitools.twObject()): current = db.getFollowing() new = list(twitools.getNamesByIDs(twitools.getFollowingIDs())) gained = 0 lost = 0 for following in new: if following not in current: db.executeQuery("INSERT INTO following VALUES('%s', %i, NULL)" % (following, int(time.time()))) print("You started following: %s" % following) gained += 1 for following in current: if following not in new: db.executeQuery("UPDATE following SET `until` = %i WHERE `id` = %s AND `until` IS NULL" % (int(time.time()), following)) print("You no longer follow: %s" % following) lost += 1 db.commit() return gained, lost if __name__ == "__main__": count, last, first = getTweets() print("Stored %i tweets after %i until %i." % (count, first, last)) count, last, first = getMessages() print("Stored %i messages after %i until %i." % (count, first, last)) gained, lost = getFollowers() print("Gained %i followers, lost %i." % (gained, lost)) gained, lost = getFollowing() print("Started following %i, stopped following %i." % (gained, lost))