from github import Github
import csv

# set this in your GH profile settings
gh_token = ""

def parse_contents(content_list, row):
    pms = {"Java":"pom.xml", "PHP":"composer.json", "JavaScript":"package.json", "Ruby":"Gemfile","Python":"requirements.txt"}
    
    for f in content_list:
        if "README" in f.name:
            row['has_readme'] = 'T'
        if "LICENSE" in f.name:
            row['has_license'] = 'T'
        if ".travis.yml" == f.name:
            row['uses_ci'] = 'T'
        if 'test' in f.name and f.type == 'dir':
            row['has_tests'] = 'T'
        if pms[row['language']] in f.name:
            row['uses_pm'] = 'T'

if __name__ == '__main__':
    g = Github(login_or_token=gh_token)
    
    with open("data.csv", "w") as f:
        fieldnames = ['name', 'language', 'stars', 'forks', 'updated_at', 'created_at', 'has_readme', 'has_license', 'has_tests', 'uses_ci', 'uses_pm']
        writer = csv.DictWriter(f, fieldnames=fieldnames)
        writer.writeheader()
        
        ct = 0
        
        q = "stars:>100 language:javascript language:php language:ruby language:java language:python"
        for repo in g.search_repositories(q, sort="stars", order="desc"):
            row = {
                'name' : repo.name,
                'language' : repo.language,
                'stars' : repo.stargazers_count,
                'forks' : repo.forks_count,
                'updated_at' : repo.updated_at,
                'created_at' : repo.created_at,
                'has_readme' : 'F',
                'has_license' : 'F',
                'has_tests' : 'F',
                'uses_ci' : 'F',
                'uses_pm' : 'F'
            }
            parse_contents(repo.get_dir_contents("."), row)
            writer.writerow(row)

            ct += 1
            print(ct, row['name'])
            
            if ct % 100 == 0:
                f.flush()
