Last active
September 7, 2021 02:26
-
-
Save amane-katagiri/99030737e54659b5395b0931e5d00118 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"consumer_key": "", | |
"consumer_secret": "", | |
"token": "", | |
"token_secret": "" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from collections import ChainMap | |
import json | |
from pathlib import Path | |
from twitter import OAuth | |
from twitter import Twitter | |
def _get_file_path(filename: str) -> str: | |
return str(Path.home() / filename) | |
def _json_dump(filename: str, obj) -> None: | |
with open(filename, "w") as f: | |
json.dump(obj, f) | |
def _json_load_or_default(filename: str, default=None): | |
try: | |
with open(filename) as f: | |
return json.load(f) | |
except Exception: | |
return default | |
CREDENTIALS = _json_load_or_default( | |
_get_file_path(".twitter.json"), dict() | |
) | |
twi = Twitter(auth=OAuth(consumer_key=CREDENTIALS.get("consumer_key"), | |
consumer_secret=CREDENTIALS.get("consumer_secret"), | |
token=CREDENTIALS.get("token"), | |
token_secret=CREDENTIALS.get("token_secret"))) | |
def main() -> None: | |
"""Compare followers and followees list with local F/F list.""" | |
cur = twi.followers.list(count=200, cursor=-1) | |
followers = { | |
x["id_str"]: (x["screen_name"], x["name"]) | |
for x in cur["users"] | |
} | |
while cur["next_cursor"]: | |
cur = twi.followers.list(count=200, cursor=cur["next_cursor"]) | |
followers.update({ | |
x["id_str"]: (x["screen_name"], x["name"]) | |
for x in cur["users"]} | |
) | |
cur = twi.friends.list(count=200, cursor=-1) | |
friends = { | |
x["id_str"]: (x["screen_name"], x["name"]) | |
for x in cur["users"] | |
} | |
while cur["next_cursor"]: | |
cur = twi.friends.list(count=200, cursor=cur["next_cursor"]) | |
friends.update({ | |
x["id_str"]: (x["screen_name"], x["name"]) | |
for x in cur["users"] | |
}) | |
friends_before = _json_load_or_default( | |
_get_file_path(".friends.json"), dict() | |
) | |
followers_before = _json_load_or_default( | |
_get_file_path(".followers.json"), dict() | |
) | |
_json_dump(_get_file_path(".followers.json"), followers) | |
_json_dump(_get_file_path(".friends.json"), friends) | |
users = ChainMap(friends, followers, friends_before, followers_before) | |
text = "" | |
if set(followers_before.keys()) != set(followers.keys()): | |
text += """followers({}->{}): | |
{} | |
""".format( | |
len(set(followers_before.keys())), | |
len(set(followers.keys())), | |
"\n".join([ | |
"- {}: https://twitter.com/{} ({})".format(x, *users[x]) | |
for x in set(followers_before.keys()) - set(followers.keys()) | |
] + [ | |
"+ {}: https://twitter.com/{} ({})".format( | |
x, *users[x] | |
) for x in set(followers.keys()) - set(followers_before.keys()) | |
]) | |
) | |
if set(friends_before.keys()) != set(friends.keys()): | |
text += """followees({}->{}): | |
{} | |
""".format( | |
len(set(friends_before.keys())), | |
len(set(friends.keys())), | |
"\n".join([ | |
"- {}: https://twitter.com/{} ({})".format(x, *users[x]) | |
for x in set(friends_before.keys()) - set(friends.keys()) | |
] + [ | |
"+ {}: https://twitter.com/{} ({})".format(x, *users[x]) | |
for x in set(friends.keys()) - set(friends_before.keys()) | |
]) | |
) | |
print(text) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment