From 114fc04b4377ba18a76721cf40b0848079360455 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Mon, 1 Aug 2016 18:02:11 +0200 Subject: [PATCH] Implement following/follower monitoring --- dbtools/__init__.py | 14 ++++++++++++++ filler.py | 34 +++++++++++++++++++++++++++++++++- twitools/__init__.py | 3 +++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/dbtools/__init__.py b/dbtools/__init__.py index f895dfb..a0df836 100644 --- a/dbtools/__init__.py +++ b/dbtools/__init__.py @@ -50,6 +50,9 @@ class dbObject: def executeQuery(self, query): return self.cur.execute(query) + def getAll(self): + return self.cur.fetchall() + def getNext(self): return self.cur.fetchone() @@ -71,6 +74,17 @@ class dbObject: self.executeQuery("SELECT %s(SUBSTR(timestamp,0,11)) FROM tweets" % mode) return setuptools.getDate(str(self.getNext()["%s(SUBSTR(timestamp,0,11))" % mode])) + def getFollowers(db): + db.executeQuery("SELECT id FROM followers;") + for i in db.getAll(): + yield i[0] + + def getFollowing(db): + db.executeQuery("SELECT id FROM following;") + for i in db.getAll(): + yield i[0] + + def getLatestMessage(db): db.executeQuery("SELECT max(id) FROM messages") try: diff --git a/filler.py b/filler.py index 25fad8a..00ca02b 100755 --- a/filler.py +++ b/filler.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -import dbtools, setuptools, twitools +import dbtools, setuptools, time, twitools def getTweets(db=dbtools.dbHelper(), user=twitools.twObject().whoami(), two=twitools.twObject()): query = "from:" + user @@ -52,8 +52,40 @@ def getMessages(db=dbtools.dbHelper(), two=twitools.twObject()): return mcount, savepoint or 0, db.getLatestMessage() +def getFollowers(db=dbtools.dbHelper(), two=twitools.twObject()): + current = db.getFollowers() + new = twitools.getNamesByIDs(twitools.getFollowerIDs()) + + for follower in new: + if follower not in current: + db.executeQuery("INSERT INTO followers VALUES('%s', %i, NULL)" % (follower, int(time.time()))) + + 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)) + + db.commit() + +def getFollowing(db=dbtools.dbHelper(), two=twitools.twObject()): + current = db.getFollowing() + new = twitools.getNamesByIDs(twitools.getFollowingIDs()) + + for following in new: + if following not in current: + db.executeQuery("INSERT INTO following VALUES('%s', %i, NULL)" % (following, int(time.time()))) + + 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)) + + db.commit() + 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.") diff --git a/twitools/__init__.py b/twitools/__init__.py index 730d7de..ff18d7a 100644 --- a/twitools/__init__.py +++ b/twitools/__init__.py @@ -22,6 +22,9 @@ def getFollowerIDs(two=twObject()): ''' Returns 5,000 follower IDs at most ''' return two.api.followers_ids(screen_name=twObject().whoami()) +def getFollowingIDs(two=twObject()): + return two.api.friends_ids(screen_name=twObject().whoami()) + def getNamesByIDs(fids=getFollowerIDs(), two=twObject()): for page in setuptools.paginate(fids, 100): followers = two.api.lookup_users(user_ids=page)