From f87ad68b8bebdb9bc56ed2693f36049d422061f8 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Mon, 1 Aug 2016 21:46:57 +0200 Subject: [PATCH] Optimize follower/following handling, print changes when running filler.py --- filler.py | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/filler.py b/filler.py index 00ca02b..146b5ec 100755 --- a/filler.py +++ b/filler.py @@ -54,38 +54,58 @@ def getMessages(db=dbtools.dbHelper(), two=twitools.twObject()): def getFollowers(db=dbtools.dbHelper(), two=twitools.twObject()): current = db.getFollowers() - new = twitools.getNamesByIDs(twitools.getFollowerIDs()) + 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 = twitools.getNamesByIDs(twitools.getFollowingIDs()) + 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)) - getFollowers() - print("Processed followers.") - getFollowing() - print("Processed following.") + gained, lost = getFollowers() + print("Gained %i followers, lost %i." % (gained, lost)) + gained, lost = getFollowing() + print("Started following %i, stopped following %i." % (gained, lost))