-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
131 lines (101 loc) · 3.53 KB
/
main.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import requests
token: str = "YOUR Github TOKEN" # don't forget to give your token the access for unfollowing users in github settings
headers: dict = {
"Authorization": f"Bearer {token}",
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
}
def get_paginated_data(url: str) -> list:
"""
Retrieve paginated data from a given GitHub API URL.
Args:
url (str): The GitHub API URL to retrieve data from.
Returns:
list: A list containing data retrieved from all paginated pages.
"""
pages_remaining: bool = True
data: list = []
while pages_remaining:
r: requests.Response = requests.get(url, headers=headers)
data += r.json()
if r.links.get("next"):
url = r.links["next"]["url"]
else:
pages_remaining = False
return data
def get_followers() -> list:
"""
Retrieve a list of followers for the authenticated GitHub user.
Returns:
list: A list of usernames representing followers.
"""
url: str = "https://api.github.com/user/followers?per_page=100"
data: list = get_paginated_data(url)
return [user["login"] for user in data]
def get_followings() -> list:
"""
Retrieve a list of users that the authenticated GitHub user is following.
Returns:
list: A list of usernames representing followings.
"""
url: str = "https://api.github.com/user/following?per_page=100"
data: list = get_paginated_data(url)
return [user["login"] for user in data]
def get_ghost_users() -> list:
"""
Identify users that the authenticated GitHub user is following but who are not following back.
Returns:
list: A list of usernames representing ghost users.
"""
followers: list = get_followers()
followings: list = get_followings()
ghosts: list = []
for user in followings:
if user not in followers:
ghosts.append(user)
return ghosts
while True:
menu_text = """MENU OPTIONS:
0. Exit
1. List Followers
2. List Followings
3. List Users Not Following Back
4. Unfollow Users Not Following Back
Enter the corresponding number:"""
choice = input(menu_text)
if choice == "1":
followers = get_followers()
for user in followers:
print(user)
print("\nTotal number of followers:", len(followers))
elif choice == "2":
followings = get_followings()
for user in followings:
print(user)
print("\nTotal number of followings:", len(followings))
elif choice == "3":
ghosts = get_ghost_users()
ghosts_number = len(ghosts)
if ghosts_number:
for user in ghosts:
print(user)
print(f"\n{ghosts_number} users are not following you back.")
else:
print("\nCongrats! You have no ghost users.")
elif choice == "4":
ghosts = get_ghost_users()
ghosts_number = len(ghosts)
if ghosts_number:
for user in ghosts:
url: str = f"https://api.github.com/user/following/{user}"
r: requests.Response = requests.delete(url, headers=headers)
print(f'{user} has been unfollowed.')
print(f"\n{ghosts_number} users have been removed.")
else:
print("\nCongrats! You have no ghost users.")
elif choice == "0":
print("Bye!")
break
else:
print("Unknown Option Selected!\nPlease Try Again.")
print("======================================================\n")