import json
import os
import subprocess

TARGET_DIRECTORY="/Users/noa/slask/spotify_github_repos"


def clone(url, target_dir):
    subprocess.call(["git", "clone", url, target_dir])


def get_ignored_repos(ignored_filename):
    with open(ignored_filename) as f:
        return f.readlines()


def clone_repos(repos_json_filename):
    ignored_repos = get_ignored_repos("../ignored-repos.txt")
    with open(repos_json_filename) as f:
        repos = json.load(f)
        for repo in repos:
            name = repo["name"]
            if repo["fork"]:
                print "skipping as %s is a fork" % name
                continue
            if name in ignored_repos:
                print "skipping %s as it is in ignored repos list" % name
                continue
            print "cloning %s from %s" % (name, repo["clone_url"])
            clone(repo["clone_url"], os.path.join(TARGET_DIRECTORY, name))

if __name__ == '__main__':
    clone_repos("rs.json")