import json

# file directory
### CHANGE ME
fn = "nvdcve-1.1-2020.json"
fd = "nvd/"
###

# collecting the nvd data
data = None
with open("{}{}".format(fd, fn), 'r') as f:
    data = json.load(f)

# saving all the useful data
github_meta = []
for i in range(len(data['CVE_Items'])):
    sites = data['CVE_Items'][i]['cve']['references']['reference_data']

    # each cve entry has different sites -- need to check which sites are github li
    for site in sites:
        # useful info gets saved into a dictionary
        github_dict = {}

        # get url
        site_url = site['url']
        if "github" in site_url and "commit" in site_url:

            if not bool(data['CVE_Items'][i]['impact']):
                break

            if not bool(data['CVE_Items'][i]['configurations']['nodes'][0]):
                break

            else:
                if data['CVE_Items'][i]['configurations']['nodes'][0]['operator'] == 'OR':
                    github_dict['vulnerable']    = data['CVE_Items'][i]['configurations']['nodes'][0]['cpe_match'][0]['vulnerable']
                else:
                    for child in data['CVE_Items'][i]['configurations']['nodes'][0]['children']:
                        if child['cpe_match'][0]['vulnerable'] == False:
                            github_dict['vulnerable']    = child['cpe_match'][0]['vulnerable']

            if github_dict['vulnerable'] == False:
                print(github_dict['vulnerable'])
              
            github_dict['url']           = site_url
            github_dict['publishedDate'] = data['CVE_Items'][i]['publishedDate']
            github_dict['severity']      = data['CVE_Items'][i]['impact']['baseMetricV2']['severity']

            github_meta.append(github_dict)

'''for gm in github_meta:
    print(gm)
    print("+++++\n")
'''
