#!/usr/bin/env python
from __future__ import print_function
import sys
import json
from util import get_url
import csv

data = {} 
ADVISORY_ID = "advisory_id" 
ADVISORY_TITLE = "advisory_title" 
BUG_IDS = "bug_ids" 
CVES = "cves" 
CVRF_URL = "cvrfUrl" 
CVSS_BASE_SCORE = "cvss_base_score" 
CWE = "cwe" 
FIRST_PUBLISHED = "first_published" 
IPS_SIGNATURES = "ips_signatures" 
LAST_UPDATED = "last_updated" 
PRODUCT_NAMES = "product_names" 
PUBLICATION_URL = "publication_url" 
SIR = "sir" 
SUMMARY = "summary"

def list_single_device(ip):
    return get_url("network-device/ip-address/%s" % ip)

def list_network_devices():
    return get_url("network-device")

def append_to_json(filepath, data):

    # construct JSON fragment as new file ending
    new_ending = ", " + "{"+ json.dumps(data)[1:-1] + "}\n" + "]"

    # edit the file in situ - first open it in read/write mode
    with open(filepath, 'r+') as f:

        f.seek(0, 2)        # move to end of file
        index = f.tell()    # find index of last byte

        # walking back from the end of file, find the index
        # of the original JSON's closing '}'
        while not f.read().startswith(']'):
            index -= 1
            if index == 0:
                raise ValueError("can't find JSON object in {!r}".format(filepath))
            f.seek(index)

        # starting at the original ending } position, write out
        # the new ending
        f.seek(index)
        f.write(new_ending)

if __name__ == "__main__":
    if len(sys.argv) > 1:
        response = list_single_device(sys.argv[1])
        print(json.dumps(response, indent=2))
    else:
        response = list_network_devices()

        vulnfile="vulnerabilities.json"
        file = open(vulnfile, 'r')
        jdata = json.load(file)
        for element in jdata:
            for device in response['response']:
                if any(device['series'] in s for s in element['product_names']) or any(device['softwareVersion'] in p for p in element['product_names']):
                    append_to_json('vulnerability.json', element)
                    with open("vulnerability.json", "r") as sample:
                        data = json.load(sample)
                        unique = { each['advisory_id'] : each for each in data}.values()
        with open('vulnerability.csv', 'w', newline='') as csvfile:
            csvwriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
            csvwriter.writerow([ADVISORY_ID, ADVISORY_TITLE, BUG_IDS, CVES, CVRF_URL, CVSS_BASE_SCORE, CWE, FIRST_PUBLISHED, IPS_SIGNATURES, LAST_UPDATED, PRODUCT_NAMES, PUBLICATION_URL, SIR, SUMMARY]) 
            for item in unique: 
                bug_ids = "" 
                cves = "" 
                cwe = "" 
                ips_sig = "" 
                prod_names = "" 
                for i in item[BUG_IDS]:
                    bug_ids += "{}; ".format(i)
                for i in item[CVES]:
                    cves += "{}; ".format(i)
                for i in item[CWE]:
                    cwe += "{}; ".format(i)
                for i in item[IPS_SIGNATURES]:
                    ips_sig += "{}; ".format(i)
                for i in item[PRODUCT_NAMES]:
                    prod_names += "{}; ".format(i)
                csvwriter.writerow([item[ADVISORY_ID], item[ADVISORY_TITLE], bug_ids, cves, item[CVRF_URL], item[CVSS_BASE_SCORE], cwe, item[FIRST_PUBLISHED], ips_sig, item[LAST_UPDATED], prod_names, item[PUBLICATION_URL], item[SIR], item[SUMMARY]])
