Skip to content

Instantly share code, notes, and snippets.

@areski
Forked from lukebeer/gitget.py
Created May 6, 2014 13:41
Show Gist options
  • Save areski/67bf6245382c5d74fee1 to your computer and use it in GitHub Desktop.
Save areski/67bf6245382c5d74fee1 to your computer and use it in GitHub Desktop.
#!/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' lib, install on deb system with: sudo apt-get install python-pip; sudo pip install requests
- sudo wget https://gist.github.com/LukeBeer/9bb732dd6129ebd92fdc/raw/gitget.py -O /usr/local/bin/gitget; sudo chmod +x /usr/local/bin/gitget
"""
import sys
import requests
import multiprocessing
from subprocess import call
__author__ = "Luke Beer, Ed Pearson"
__license__ = "GPL"
__version__ = "0.4"
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
print
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
print
def clone(repo):
if not repo:
exit('Nothing found')
if isinstance(repo[0][0], basestring):
call(["git", "clone", repo[0][0]])
else:
make_menu(repo[0][0])
selection = raw_input("Enter number: ")
if repo[0][0][int(selection)]:
call(["git", "clone", repo[0][0][int(selection)]['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)]
clone(repo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment