Last active
December 8, 2015 20:08
-
-
Save lukebeer/9bb732dd6129ebd92fdc to your computer and use it in GitHub Desktop.
gitget :: gist to search Github for repos, return results and clone via menu selection.
This file contains hidden or 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
#!/usr/bin/env python | |
""" | |
- Search Github for repos, return results and clone via menu selection. | |
- Created because we all spend far too much time looking for the repo addresses. | |
- Requires 'requests', pip install requests | |
- wget https://gist.githubusercontent.com/lukebeer/9bb732dd6129ebd92fdc/raw/gitget.py -O /usr/local/bin/gitget; chmod +x /usr/local/bin/gitget; ln -s /usr/local/bin/gitget /usr/local/bin/gg | |
""" | |
import sys | |
import requests | |
import multiprocessing | |
from subprocess import call | |
__author__ = "https://luke.beer" | |
__license__ = "GPL" | |
__version__ = "0.8" | |
def search_github(name): | |
try: | |
response = requests.get("https://api.github.com/search/repositories?q=%s&page=1&per_page=30" % name) | |
if response.status_code == 200: | |
if len(response.json()['items']) > 1: | |
return response.json()['items'] | |
else: | |
return response.json()['items'][0]['clone_url'] | |
except Exception: | |
pass | |
def make_menu(items): | |
if not items: | |
exit('Nothing found') | |
count = 0 | |
width = max([len(item['full_name']) for item in items]) | |
watch = max([len(str(item['watchers_count'])) for item in items]) | |
print "ID Name " + " "*width + "Watchers Description" | |
for item in items: | |
if count <= 9: | |
xpad = 1 | |
else: | |
xpad = 0 | |
npadding = width + 2 - len(item['full_name']) + xpad | |
wpadding = watch + 5 - len(str(item['watchers_count'])) | |
print "%d. %s %s %s %s %s" % (count, item['full_name'], " "*npadding, item['watchers_count'], " "*wpadding, item['description']) | |
count += 1 | |
return items[int(raw_input("Enter number: "))] | |
def clone(repo): | |
if not repo: | |
exit('Nothing found') | |
if isinstance(repo['clone_url'], basestring): | |
call(["git", "clone", repo['clone_url']]) | |
if __name__ == "__main__": | |
if len(sys.argv) < 2: | |
exit("usage: %s <search as space separated list>" % sys.argv[0]) | |
args = sys.argv[1:] | |
num = min(5, len(args)) | |
p = multiprocessing.Pool(num) | |
repo = [p.map(search_github, args)] | |
cpool = multiprocessing.Pool(len(repo[0])) | |
repos = [make_menu(item) for item in repo[0]] | |
results = [cpool.map(clone, repos)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment