|
11 | 11 | import csv
|
12 | 12 | import requests
|
13 | 13 | import time
|
| 14 | +import argparse |
14 | 15 |
|
15 |
| -def fill(): |
16 |
| - reports = [] |
17 |
| - with open('data.csv', 'r', newline='', encoding='utf-8') as file: |
| 16 | +def create_argument_parser(): |
| 17 | + argparser = argparse.ArgumentParser() |
| 18 | + argparser.add_argument( |
| 19 | + '--update-all', |
| 20 | + action='store_true', |
| 21 | + help='Update all reports', |
| 22 | + default=False |
| 23 | + ) |
| 24 | + argparser.add_argument( |
| 25 | + '--input-data-file', |
| 26 | + type=str, |
| 27 | + help='Path to input data file', |
| 28 | + default='data.csv' |
| 29 | + ) |
| 30 | + argparser.add_argument( |
| 31 | + '--output-data-file', |
| 32 | + type=str, |
| 33 | + help='Path to output data file', |
| 34 | + default='data.csv' |
| 35 | + ) |
| 36 | + return argparser |
| 37 | + |
| 38 | +def fill(commandline_args): |
| 39 | + fetched_reports = [] |
| 40 | + new_reports = [] |
| 41 | + with open(commandline_args.input_data_file, 'r', newline='', encoding='utf-8') as file: |
18 | 42 | reader = csv.DictReader(file)
|
19 | 43 | for row in reader:
|
20 |
| - reports.append(dict(row)) |
21 |
| - count_of_reports = len(reports) |
| 44 | + if row['title'] == '' or commandline_args.update_all: |
| 45 | + new_reports.append(dict(row)) |
| 46 | + else: |
| 47 | + fetched_reports.append(dict(row)) |
| 48 | + count_of_reports = len(new_reports) |
22 | 49 | for i in range(count_of_reports):
|
23 | 50 | time.sleep(0.5)
|
24 | 51 | print('Fetching report ' + str(i + 1) + ' out of ' + str(count_of_reports))
|
25 |
| - report_url = 'https://' + reports[i]['link'] + '.json' |
| 52 | + report_url = 'https://' + new_reports[i]['link'] + '.json' |
26 | 53 | try:
|
27 | 54 | json_info = requests.get(report_url).json()
|
28 |
| - reports[i]['title'] = json_info['title'] |
29 |
| - reports[i]['program'] = json_info['team']['profile']['name'] |
30 |
| - reports[i]['upvotes'] = int(json_info['vote_count']) |
31 |
| - reports[i]['bounty'] = float(json_info['bounty_amount'] if 'bounty_amount' in json_info else "0") if json_info['has_bounty?'] else 0.0 |
32 |
| - reports[i]['vuln_type'] = json_info['weakness']['name'] if 'weakness' in json_info else '' |
| 55 | + new_reports[i]['title'] = json_info['title'] |
| 56 | + new_reports[i]['program'] = json_info['team']['profile']['name'] |
| 57 | + new_reports[i]['upvotes'] = int(json_info['vote_count']) |
| 58 | + new_reports[i]['bounty'] = float(json_info['bounty_amount'] if 'bounty_amount' in json_info else "0") if json_info['has_bounty?'] else 0.0 |
| 59 | + new_reports[i]['vuln_type'] = json_info['weakness']['name'] if 'weakness' in json_info else '' |
33 | 60 | except Exception as err:
|
34 | 61 | print('error at report ' + str(i + 1), err)
|
35 | 62 | continue
|
36 | 63 |
|
37 |
| - print(reports[i]) |
| 64 | + print(new_reports[i]) |
38 | 65 |
|
39 |
| - with open('data.csv', 'w', newline='', encoding='utf-8') as file: |
| 66 | + with open(commandline_args.output_data_file, 'w', newline='', encoding='utf-8') as file: |
| 67 | + reports = new_reports + fetched_reports |
40 | 68 | keys = reports[0].keys()
|
41 | 69 | writer = csv.DictWriter(file, fieldnames=keys)
|
42 | 70 | writer.writeheader()
|
43 | 71 | writer.writerows(reports)
|
44 | 72 |
|
45 | 73 |
|
46 | 74 | if __name__ == '__main__':
|
47 |
| - fill() |
| 75 | + parser = create_argument_parser() |
| 76 | + args = parser.parse_args() |
| 77 | + fill(args) |
0 commit comments