|
| 1 | +import typer |
| 2 | +from typing_extensions import Annotated |
1 | 3 | from scan import scan_url, get_url_report, get_api_key
|
2 | 4 | from workbook import save_to_excel
|
3 | 5 | from generate import generate_html_report
|
4 |
| -import argparse |
5 | 6 |
|
| 7 | +app = typer.Typer() |
6 | 8 |
|
7 |
| -def main(): |
8 |
| - api_key = get_api_key() # Get the API key from user input |
9 | 9 |
|
10 |
| - parser = argparse.ArgumentParser(description="Scan URLs with VirusTotal.") |
11 |
| - parser.add_argument( |
12 |
| - "urls", |
13 |
| - metavar="URL", |
14 |
| - type=str, |
15 |
| - nargs="+", |
16 |
| - help="URLs to scan (space-separated)", |
17 |
| - ) |
18 |
| - args = parser.parse_args() |
19 |
| - urls = args.urls |
| 10 | +@app.command() |
| 11 | +def main( |
| 12 | + urls: Annotated[ |
| 13 | + list[str], |
| 14 | + typer.Argument(help="URLs to scan (space-separated)"), |
| 15 | + ], |
| 16 | + api_key: Annotated[ |
| 17 | + str, |
| 18 | + typer.Option( |
| 19 | + "--api-key", |
| 20 | + "-k", |
| 21 | + help="Your VirusTotal API key. If not provided, you will be prompted.", |
| 22 | + ), |
| 23 | + ] = None, |
| 24 | +): |
| 25 | + """ |
| 26 | + Scan URLs with VirusTotal and generate reports. |
| 27 | + """ |
| 28 | + if not api_key: |
| 29 | + api_key = get_api_key() |
20 | 30 |
|
21 | 31 | results = []
|
22 | 32 | for url in urls:
|
23 |
| - scan_id = scan_url(url, api_key) # Pass the API key to scan_url |
| 33 | + scan_id = scan_url(url, api_key) |
24 | 34 | if scan_id:
|
25 |
| - # Pass the API key to get_url_report |
26 | 35 | report = get_url_report(scan_id, api_key)
|
27 | 36 | if report:
|
28 |
| - stats = report.results.get('attributes', {}).get('stats', {}) |
29 |
| - results.append({ |
30 |
| - 'url': url, |
31 |
| - 'malicious': stats.get('malicious', 0), |
32 |
| - 'harmless': stats.get('harmless', 0), |
33 |
| - 'undetected': stats.get('undetected', 0) |
34 |
| - }) |
| 37 | + attributes = report.get("attributes", {}) |
| 38 | + stats = attributes.get("stats", {}) |
| 39 | + results.append( |
| 40 | + { |
| 41 | + "url": url, |
| 42 | + "malicious": stats.get("malicious", 0), |
| 43 | + "harmless": stats.get("harmless", 0), |
| 44 | + "undetected": stats.get("undetected", 0), |
| 45 | + } |
| 46 | + ) |
| 47 | + else: |
| 48 | + print(f"No report found for {url}") |
| 49 | + else: |
| 50 | + print(f"Scan failed for {url}") |
35 | 51 |
|
36 |
| - save_to_excel(results) |
37 |
| - generate_html_report(results) |
| 52 | + if results: |
| 53 | + save_to_excel(results) |
| 54 | + generate_html_report(results) |
| 55 | + else: |
| 56 | + print("No results to report.") |
38 | 57 |
|
39 | 58 |
|
40 | 59 | if __name__ == "__main__":
|
41 |
| - main() |
| 60 | + app() |
0 commit comments