aboutsummaryrefslogtreecommitdiffstats
path: root/examples/advanced_queries.py
diff options
context:
space:
mode:
authorLouis Burda <dev@sinitax.com>2026-01-30 03:04:01 +0100
committerLouis Burda <dev@sinitax.com>2026-01-30 03:04:01 +0100
commitf6487c615cff023db1574e2c23db78bf02a43709 (patch)
tree8a0e793a8ea28b2a5eef5dcd509b6c6a2466ee1c /examples/advanced_queries.py
downloadnvdb-py-main.tar.gz
nvdb-py-main.zip
Add initial versionHEADmain
Diffstat (limited to 'examples/advanced_queries.py')
-rw-r--r--examples/advanced_queries.py120
1 files changed, 120 insertions, 0 deletions
diff --git a/examples/advanced_queries.py b/examples/advanced_queries.py
new file mode 100644
index 0000000..46035c7
--- /dev/null
+++ b/examples/advanced_queries.py
@@ -0,0 +1,120 @@
+"""Advanced query examples for nvdb-py."""
+
+import asyncio
+from datetime import datetime, timedelta
+
+from nvd import NVDClient
+
+
+async def main() -> None:
+ """Run advanced examples."""
+ async with NVDClient() as client:
+ print("=" * 80)
+ print("Example 1: CVEs in CISA KEV catalog with HIGH severity")
+ print("=" * 80)
+
+ count = 0
+ async for cve in client.cve.search_cves(
+ has_kev=True, cvss_v3_severity="HIGH"
+ ):
+ print(f"{cve.id} - Score: {cve.cvss_v3_score}")
+ count += 1
+ if count >= 5:
+ break
+ print(f"Showing {count} results\n")
+
+ print("=" * 80)
+ print("Example 2: CVEs for specific CPE (Apache Log4j)")
+ print("=" * 80)
+
+ count = 0
+ async for cve in client.cve.search_cves(
+ cpe_name="cpe:2.3:a:apache:log4j:2.14.1:*:*:*:*:*:*:*"
+ ):
+ print(f"{cve.id}: {cve.description[:100]}...")
+ count += 1
+ if count >= 5:
+ break
+ print(f"Showing {count} results\n")
+
+ print("=" * 80)
+ print("Example 3: Recent CVEs (last 30 days)")
+ print("=" * 80)
+
+ # Calculate date range
+ end_date = datetime.utcnow()
+ start_date = end_date - timedelta(days=30)
+
+ # Format for NVD API
+ start_str = start_date.strftime("%Y-%m-%dT%H:%M:%S.000")
+ end_str = end_date.strftime("%Y-%m-%dT%H:%M:%S.000")
+
+ count = 0
+ async for cve in client.cve.search_cves(
+ pub_start_date=start_str, pub_end_date=end_str
+ ):
+ print(f"{cve.id} - Published: {cve.published.date()}")
+ count += 1
+ if count >= 10:
+ break
+ print(f"Showing {count} results\n")
+
+ print("=" * 80)
+ print("Example 4: CVEs with specific CWE (Cross-Site Scripting)")
+ print("=" * 80)
+
+ count = 0
+ async for cve in client.cve.search_cves(cwe_id="CWE-79"):
+ print(f"{cve.id}: {cve.description[:100]}...")
+ count += 1
+ if count >= 5:
+ break
+ print(f"Showing {count} results\n")
+
+ print("=" * 80)
+ print("Example 5: CPE match criteria for a CVE")
+ print("=" * 80)
+
+ matches = await client.cpematch.get_cve_match_criteria("CVE-2021-44228")
+ print(f"Found {len(matches)} match criteria for CVE-2021-44228")
+ for match in matches[:5]:
+ print(f" {match.criteria}")
+ if match.versionEndExcluding:
+ print(f" Versions: < {match.versionEndExcluding}")
+ print()
+
+ print("=" * 80)
+ print("Example 6: Search with multiple filters")
+ print("=" * 80)
+
+ count = 0
+ async for cve in client.cve.search_cves(
+ keyword="remote",
+ cvss_v3_severity="CRITICAL",
+ has_cert_alerts=True,
+ ):
+ print(
+ f"{cve.id} - Score: {cve.cvss_v3_score} - {cve.description[:80]}..."
+ )
+ count += 1
+ if count >= 5:
+ break
+ print(f"Showing {count} results\n")
+
+ print("=" * 80)
+ print("Example 7: List data sources")
+ print("=" * 80)
+
+ count = 0
+ async for source in client.source.list_sources():
+ print(f"{source.name}")
+ print(f" Contact: {source.contactEmail}")
+ print(f" Identifiers: {', '.join(source.sourceIdentifiers[:3])}")
+ count += 1
+ if count >= 5:
+ break
+ print(f"Showing {count} results\n")
+
+
+if __name__ == "__main__":
+ asyncio.run(main())