2015-04-13 20:58:32 +00:00
|
|
|
#!/usr/bin/env python3
|
2015-03-09 17:32:24 +00:00
|
|
|
|
2016-08-01 16:02:11 +00:00
|
|
|
import dbtools, setuptools, time, twitools
|
2015-03-09 17:32:24 +00:00
|
|
|
|
2016-06-30 10:33:09 +00:00
|
|
|
def getTweets(db=dbtools.dbHelper(), user=twitools.twObject().whoami(), two=twitools.twObject()):
|
2015-04-22 00:06:32 +00:00
|
|
|
query = "from:" + user
|
2016-06-30 10:33:09 +00:00
|
|
|
savepoint = db.getLatestTweet() + 1
|
2015-04-22 00:06:32 +00:00
|
|
|
last = savepoint
|
2015-03-09 17:32:24 +00:00
|
|
|
|
2015-04-22 00:06:32 +00:00
|
|
|
timeline = two.search(query, savepoint)
|
2015-03-09 17:32:24 +00:00
|
|
|
|
2015-04-15 22:53:15 +00:00
|
|
|
tw_counter = 0
|
2015-03-09 17:32:24 +00:00
|
|
|
|
2015-04-15 22:53:15 +00:00
|
|
|
for status in timeline:
|
|
|
|
timestamp = status.created_at.strftime('%Y-%m-%d %H:%M:%S') + " +0000"
|
2016-05-30 18:40:21 +00:00
|
|
|
text = setuptools.unescapeText(status.text)
|
2015-04-22 00:06:32 +00:00
|
|
|
|
2016-06-30 10:33:09 +00:00
|
|
|
try:
|
|
|
|
db.executeQuery("INSERT INTO tweets(tweet_id,timestamp,text) VALUES(" + str(status.id) + ",'" + timestamp + "','" + text + "')")
|
|
|
|
except:
|
|
|
|
pass
|
2015-04-22 00:06:32 +00:00
|
|
|
db.commit()
|
2015-03-09 17:32:24 +00:00
|
|
|
|
2015-04-15 22:53:15 +00:00
|
|
|
last = status.id
|
|
|
|
tw_counter = tw_counter + 1
|
2015-03-09 17:32:24 +00:00
|
|
|
|
2015-04-15 22:53:15 +00:00
|
|
|
return tw_counter, last, savepoint
|
2015-03-09 17:32:24 +00:00
|
|
|
|
2016-06-30 10:33:09 +00:00
|
|
|
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()
|
|
|
|
|
2016-08-01 16:02:11 +00:00
|
|
|
def getFollowers(db=dbtools.dbHelper(), two=twitools.twObject()):
|
|
|
|
current = db.getFollowers()
|
2016-08-01 19:46:57 +00:00
|
|
|
new = list(twitools.getNamesByIDs(twitools.getFollowerIDs()))
|
|
|
|
gained = 0
|
|
|
|
lost = 0
|
|
|
|
|
2016-08-01 16:02:11 +00:00
|
|
|
for follower in new:
|
|
|
|
if follower not in current:
|
|
|
|
db.executeQuery("INSERT INTO followers VALUES('%s', %i, NULL)" % (follower, int(time.time())))
|
2016-08-01 19:46:57 +00:00
|
|
|
print("New follower: %s" % follower)
|
|
|
|
gained += 1
|
2016-08-01 16:02:11 +00:00
|
|
|
|
|
|
|
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))
|
2016-08-01 19:46:57 +00:00
|
|
|
print("Lost follower: %s" % follower)
|
|
|
|
lost += 1
|
2016-08-01 16:02:11 +00:00
|
|
|
|
|
|
|
db.commit()
|
|
|
|
|
2016-08-01 19:46:57 +00:00
|
|
|
return gained, lost
|
|
|
|
|
2016-08-01 16:02:11 +00:00
|
|
|
def getFollowing(db=dbtools.dbHelper(), two=twitools.twObject()):
|
|
|
|
current = db.getFollowing()
|
2016-08-01 19:46:57 +00:00
|
|
|
new = list(twitools.getNamesByIDs(twitools.getFollowingIDs()))
|
|
|
|
gained = 0
|
|
|
|
lost = 0
|
2016-08-01 16:02:11 +00:00
|
|
|
|
|
|
|
for following in new:
|
|
|
|
if following not in current:
|
|
|
|
db.executeQuery("INSERT INTO following VALUES('%s', %i, NULL)" % (following, int(time.time())))
|
2016-08-01 19:46:57 +00:00
|
|
|
print("You started following: %s" % following)
|
|
|
|
gained += 1
|
2016-08-01 16:02:11 +00:00
|
|
|
|
|
|
|
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))
|
2016-08-01 19:46:57 +00:00
|
|
|
print("You no longer follow: %s" % following)
|
|
|
|
lost += 1
|
2016-08-01 16:02:11 +00:00
|
|
|
|
|
|
|
db.commit()
|
2016-08-01 19:46:57 +00:00
|
|
|
|
|
|
|
return gained, lost
|
2016-08-01 16:02:11 +00:00
|
|
|
|
2015-04-15 22:53:15 +00:00
|
|
|
if __name__ == "__main__":
|
2016-08-04 16:17:59 +00:00
|
|
|
db = dbtools.dbHelper()
|
|
|
|
count, last, first = getTweets(db)
|
2016-08-01 23:17:08 +00:00
|
|
|
print("Stored %i tweets." % count)
|
2016-08-04 16:17:59 +00:00
|
|
|
count, last, first = getMessages(db)
|
2016-08-01 23:17:08 +00:00
|
|
|
print("Stored %i messages." % count)
|
2016-08-04 16:17:59 +00:00
|
|
|
gained, lost = getFollowers(db)
|
2016-08-01 19:46:57 +00:00
|
|
|
print("Gained %i followers, lost %i." % (gained, lost))
|
2016-08-04 16:17:59 +00:00
|
|
|
gained, lost = getFollowing(db)
|
2016-08-01 19:46:57 +00:00
|
|
|
print("Started following %i, stopped following %i." % (gained, lost))
|