Remove boilerplate code
import json
from urllib.request import urlopen
-ids = []
-cvss = []
-summary = []
-
-def output_cve_id(type=None, name=None, version=None):
+def data_cve_circl(name, version=None):
"""
Outputs cve-ids, if any, related to a package.
Take as input, a package name, type, package version and
query cve-search' dataset for any reported vulnerabilities
"""
- if not name:
- return
+ url = f'https://cve.circl.lu/api/search/{name}'
if version:
- url = f'https://cve.circl.lu/api/search/{name}/{version}'
- else:
- url = f'https://cve.circl.lu/api/search/{name}'
+ url += f'/{version}'
raw_data = urlopen(url).read()
data = json.loads(raw_data)
+ return data
+
+def extract_fields(data, fields_names, version=False):
"""
- Extract CVE-IDs & CVSS scores associated with
- a package.
+ Extracts requested data fields using data generated by
+ cve-search' api. Takes as input data, fields requested
"""
- if data and name and not version:
- for item in data['data']:
- try:
- ids.append(item['id'])
- cvss.append(item['cvss'])
- summary.append(item['summary'])
- except TypeError:
- cvss.append(None)
+ extracted_data = []
- return ids, cvss, summary
+ if version:
+ data = data
+ else:
+ data = data['data']
- if data and version:
for item in data:
- try:
- ids.append(item['id'])
- cvss.append(item['cvss'])
- summary.append(item['summary'])
- except TypeError:
- cvss.append(None)
+ results = {}
+ for name in fields_names:
+ results[name] = item[name]
+
+ extracted_data.append(results)
- return ids, cvss, summary
+ return extracted_data
