Created
December 27, 2013 06:19
-
-
Save patrickfuller/8143294 to your computer and use it in GitHub Desktop.
A basic graph traversal script using the Github API. Starting from a specified root-node Github user, this will traverse everyone they are following. This repeats in a breadth-first pattern until a threshold is reached.
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
import requests | |
import getpass | |
import sys | |
import json | |
import Queue | |
# This is a script, let's be lazy. We'll fill up this global and print it. | |
g = {"nodes": {}, "edges": []} | |
# And here's the cutoff criterion | |
MAX_NODES = 1000 | |
# Log in to avoid rate limiting | |
un = raw_input("Username: ") | |
pw = getpass.getpass() | |
r = requests.get("https://api.github.com", auth=(un, pw)) | |
if r.status_code != 200: | |
sys.stderr.write("Could not authenticate with Github. Exiting.\n") | |
sys.exit() | |
sys.stderr.write("Connected and authenticated.\n") | |
# BFS. Kick this ish off | |
queue = Queue.Queue() | |
queue.put((un, None)) | |
while queue: | |
if len(g["nodes"]) > MAX_NODES: | |
break | |
user, parent = queue.get() | |
if user in g["nodes"]: | |
continue | |
sys.stderr.write("Traversing %s.\n" % user) | |
in_deg = requests.get("https://api.github.com/users/" + user, | |
auth=(un, pw)).json()["followers"] | |
g["nodes"][user] = {"followers": in_deg} | |
if parent: | |
g["edges"].append({"source": parent, "target": user}) | |
followed = [res["login"] for res in requests.get("https://api.github.com/" | |
"users/%s/following" % user, auth=(un, pw)).json()] | |
for f in followed: | |
queue.put((f, user)) | |
# Save this result to a file | |
with open("followed.json", "w") as out_file: | |
out_file.write(json.dumps(g)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment