-
Notifications
You must be signed in to change notification settings - Fork 30
/
APTnotes_sync_download.py
65 lines (51 loc) · 2.3 KB
/
APTnotes_sync_download.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/python3
import os
import hashlib
import json
import requests
from utilities import get_download_url, load_notes, report_already_downloaded, verify_report_filetype
if __name__ == '__main__':
# Load APT report metadata into JSON container
APT_reports = load_notes()
# Process each report based on obtained metadata
for report in APT_reports:
report_date = report['Date']
report_title = report['Title']
report_year = report['Year']
report_source = report['Source']
report_link = report['Link']
report_filename = report['Filename']
report_sha1 = report['SHA-1']
try:
# Download Box Splash/Preview page for file
report_splash = requests.get(report_link).text
file_url = get_download_url(report_splash)
# Ensure directory exists
os.makedirs(report_year, exist_ok=True)
# Set hash check
hash_check = hashlib.sha1()
# Set download path
download_path = os.path.join(report_year, report_filename)
if report_already_downloaded(download_path):
print("[+] File {} already exists".format(report_filename))
continue
else:
# Stream download the file
report_file = requests.get(file_url, stream=True)
# Download file to desired path in chunks
with open(download_path, 'wb') as f:
for chunk in report_file.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
hash_check.update(chunk)
# Verify file contents based on expected hash value
if hash_check.hexdigest() != report_sha1:
os.remove(download_path)
raise ValueError("File integrity check failed")
except Exception as unexpected_error:
message = "[!] Download failure for {}".format(report_filename)
print(message, unexpected_error)
else:
# Verify report filetype and add extension
download_path = verify_report_filetype(download_path)
print("[+] Successfully downloaded {}".format(download_path))