Who follows you? Who follows your followers? And who follows the followers of your followers? And what about the next level?
These are pretty interesting question actually. With easy answers, if you are interested only in GitHub. The GitHub team have provided their API (application programming interface) for anyone, interested to find out how many people in total follow the followers of their followers. To access the API, just go to this site here. You may change the user name to obtain whatever information you need.
So what I did? A GitHub crawler, making a graph of the followers of your followers and going as many levels as you want (or you are allowed to by GitHub). I have tested it with 6 levels and this is what I got:
What does it tells us? Pretty much, it tells us, that the followers of our followers up to level 5 are in total 3011 unique people. (The levels are reversed, e.g. 6 is 1, 5 is 2 and so on.) Can we see them? Do we know them? Definitely, that is why the crawler is actually built for. Here are the followers of the first 19 followers in a dictionary format – as you may see, vitoshacademy is followed by ony 3 people (and one of them is me with my other account), so feel free to follow me in GitHub 🙂
Finally what the code does?
- Using the library requests of Python, it generates a Graph as a dictionary from your followers;
- The key is the name of the user, the values are its followers;
- An interesting feature for printing is added;
How the code looks like? Well, the unoptimized version looks like this:
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 |
import requests import copy class Strings: FOLLOWERS = "https://api.github.com/users/{}/followers?client_id=926275e6df122f430b26&client_secret=9fcec76b004441be11969aa391a217a3f87018c" PRINT_FOLLOWERS = "The followers of {} are {}" PRINT_FOLLOWING = "The user {} follows {}" class DirectGraph(): def __init__(self, userName): self.name = userName self.GeneralDictionary = {} self.ListOfVisitedUsers = [] def build_network_with_followers(self): self.GeneralDictionary = {} while len(self.ListOfVisitedUsers) > 0: nextUser = (self.ListOfVisitedUsers).pop(0) nextUserFollowers = self.get_followers(nextUser) self.GeneralDictionary[nextUser] = nextUserFollowers return self.GeneralDictionary def build_network(self, user, level): self.ListOfVisitedUsers = [] visited = set() myQ = [] visited.add(user) myQ.append(user) while level > 0: print("Level {} consists of {}".format(level, len(myQ))) while len(myQ): nextUser = myQ.pop(0) n extUserFollowers = self.get_followers(nextUser) for newUser in nextUserFollowers: if newUser not in visited: visited |= {newUser} self.ListOfVisitedUsers.append(newUser) myQ = copy.deepcopy(self.ListOfVisitedUsers) level -= 1 return self.ListOfVisitedUsers def print_dictionary(self): for key in sorted(self.GeneralDictionary): print("{} : {}".format(key, self.GeneralDictionary[key])) def get_followers(self, userName): userNameUrl = requests.get(self.make_url_followers(userName)) userNameJson = userNameUrl.json() userNameFollowers = [followers["login"]for followers in userNameJson] return userNameFollowers def add_edge(self, node_a, node_b): self.GeneralDictionary[node_a] = node_b def get_neighbours_for(self, node): return self.GeneralDictionary[node] def make_url_followers(self, userName): return Strings.FOLLOWERS.format(userName) def startMe(): user = "vitosh" graph = DirectGraph(user) graph.build_network(user, 3) graph.build_network_with_followers() graph.print_dictionary() if __name__ == '__main__': startMe() |
Enjoy it! 🙂