Skip to content

Instantly share code, notes, and snippets.

@kxxoling
Last active March 18, 2016 05:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kxxoling/a8dc64fbe8bd6b1260e4 to your computer and use it in GitHub Desktop.
Save kxxoling/a8dc64fbe8bd6b1260e4 to your computer and use it in GitHub Desktop.
Used to count repo stars of some github user.
from __future__ import print_function
import os
import requests
github_user = os.environ.get('githubuser')
api_url = 'https://api.github.com/users/{github_user}/repos?per_page=100'.format(github_user=github_user)
if github_user is None:
github_user = raw_input('No GitHub username detected, please enter it here: ')
def count_stars(url):
rsp = requests.get(url)
repos = rsp.json()
star_count = sum([repo['stargazers_count'] for repo in repos])
return rsp, star_count
rsp, star_counted = count_stars(api_url)
if rsp.links:
last_url = rsp.links['last']['url']
page_count = int(last_url.rsplit('=', 1)[-1])
pages = [api_url+'&page=%d'%i for i in range(2, page_count+1)]
star_counted = sum([item[-1] for item in map(count_stars, pages)], star_counted)
print(star_counted)
@kxxoling
Copy link
Author

Run this in command line by doing:

run export githubuser=xxxx

run curl https://gist.githubusercontent.com/kxxoling/a8dc64fbe8bd6b1260e4/raw/ | python .

@kxxoling
Copy link
Author

Use prompt to set username can be: TMP="/tmp/counter.py"; curl https://gist.githubusercontent.com/kxxoling/a8dc64fbe8bd6b1260e4/raw/ > $TMP; python $TMP; rm $TMP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment