-
Notifications
You must be signed in to change notification settings - Fork 0
/
followers.py
58 lines (50 loc) · 1.87 KB
/
followers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from twython import Twython
from datetime import datetime
import json
def auth():
with open("access.json", 'r') as f:
db = json.load(f)
return Twython(db["API_Key"], db["API_Secret"], db["Access_Token"], db["Access_Token_Secret"]), db["username"]
def load_followers():
with open("followers.json", 'r') as f:
out = json.load(f)
return out
def save_followers(followers):
with open("followers.json", 'w') as f:
json.dump(followers, f)
def log(lost_followers, last_tweet):
date = datetime.now().strftime("%m-%d-%Y")
time = datetime.now().strftime("%I:%M%p")
try:
with open(date+"log.txt", 'r') as f:
my_log = f.read()
except:
my_log = ""
out = time + "\n"
out += "your last tweet was: " + last_tweet + "\n"
out += "you lost: " + ", ".join(lost_followers) + "\n"
out += "\n"
out += my_log
with open(date+"log.txt", 'w') as f:
f.write(out)
def diff(old_followers, cur_followers):
return [follower for follower in old_followers.difference(cur_followers)]
def main():
twitter, user_id = auth()
cursor_str = "-1"
cur_followers = []
while cursor_str != "0":
followers_list = twitter.get_followers_ids(user_id=user_id, cursor=cursor_str)
cur_followers += followers_list['ids']
cursor_str = followers_list['next_cursor_str']
cur_followers_set = set(cur_followers)
old_followers_set = set(load_followers())
save_followers(cur_followers)
lost_followers = diff(old_followers_set, cur_followers_set)
if len(lost_followers) > 0:
last_tweet = twitter.get_user_timeline(user_id=user_id, count=1)[0]['text']
lost_followers_names = [twitter.lookup_user(user_id=user)[0]['screen_name'] for user in lost_followers]
log(lost_followers_names, last_tweet)
#run on cron every few minutes
if __name__ == "__main__":
main()