Last active
August 29, 2015 13:57
-
-
Save adampetrovic/9822290 to your computer and use it in GitHub Desktop.
A search script for finding popular users on Github using their API. Useful for recruiting purposes ;)
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 time | |
import sys | |
URL = "https://api.github.com/search/users" | |
MAX_PAGE = 100 | |
class GithubQuery(object): | |
results = {} | |
def __init__(self, **kwargs): | |
self.location = kwargs.get('location', '') | |
self.language = kwargs.get('language', '') | |
self.followers = kwargs.get('followers', 0) | |
def fetch(self, page): | |
url = URL + "?q=location:{loc}+language:{lang}+followers:>={foll}&page={page}&per_page={per_page}".format( | |
loc=self.location, | |
lang=self.language, | |
foll=self.followers, | |
page=page, | |
per_page=MAX_PAGE | |
) | |
resp = requests.get(url) | |
reset = int(resp.headers.get('X-RateLimit-Reset')) | |
now = int(time.time()) | |
rem = int(resp.headers.get('X-RateLimit-Remaining')) | |
if not resp.ok and not rem: | |
print "Rate Limited... Sleeping for {}s".format(reset-now) | |
time.sleep(reset - (now-1)) # sleep until rate limit lifted (add extra second to avoid race condition) | |
resp = requests.get(url) | |
return resp | |
def execute(self): | |
page = 1 | |
count = 0 | |
total = 0 | |
while not count or count < total: | |
resp = self.fetch(page) | |
result = resp.json() | |
total = result.get('total_count') | |
for item in result.get('items', []): | |
self.results[item.get('login')] = item | |
count += len(result.get('items', [])) | |
page += 1 | |
def output_result(self, output): | |
with open(output, 'w') as f: | |
print "Found {} unique profiles. Writing to {}".format( | |
len(self.results), | |
output | |
) | |
for username, profile in self.results.iteritems(): | |
f.write("<a href='{url}'>{username}</a><br/>".format( | |
url=profile.get('html_url'), | |
username=username | |
)) | |
if __name__ == "__main__": | |
if len(sys.argv) < 2: | |
print "Usage: ./{} <output>".format(sys.argv[0]) | |
sys.exit(1) | |
gh = GithubQuery( | |
location='vancouver', # where the user is | |
language='python', # languages they use | |
followers=10, # min. number of followers they have | |
) | |
gh.execute() # | |
gh.output_result(sys.argv[1]) # save to file passed in as args |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment