|
| 1 | +import re |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import requests |
| 5 | + |
| 6 | +try: |
| 7 | + import config |
| 8 | +except: |
| 9 | + print("create a config.py based on template.config.py and set your Malpedia API token!") |
| 10 | + sys.exit() |
| 11 | + |
| 12 | + |
| 13 | +def delete_existing_dbs(): |
| 14 | + """ delete potentially existing old apivector db files """ |
| 15 | + for filename in os.listdir("dbs"): |
| 16 | + if re.search(r"\d{4}-\d\d-\d\d-apivectors-v\d+\.csv", filename): |
| 17 | + os.remove("dbs" + os.sep + filename) |
| 18 | + |
| 19 | + |
| 20 | +def get_newest_db_version(): |
| 21 | + """ find ApiVector DB files and return newest version number found """ |
| 22 | + max_version = 0 |
| 23 | + for filename in os.listdir("dbs"): |
| 24 | + version = re.search(r"\d{4}-\d\d-\d\d-apivectors-v(?P<version_number>\d+)\.csv", filename) |
| 25 | + if version: |
| 26 | + max_version = max(max_version, int(version.group("version_number"))) |
| 27 | + return max_version |
| 28 | + |
| 29 | + |
| 30 | +def download_apivector_db(): |
| 31 | + result = { |
| 32 | + "filename": "", |
| 33 | + "content": "", |
| 34 | + "version": 0 |
| 35 | + } |
| 36 | + response = requests.get( |
| 37 | + 'https://malpedia.caad.fkie.fraunhofer.de/api/list/apiscout/csv', |
| 38 | + headers={'Authorization': 'apitoken ' + config.APITOKEN}, |
| 39 | + ) |
| 40 | + if response.status_code == 200: |
| 41 | + result["filename"] = response.headers['Content-Disposition'].split("=")[1].strip() |
| 42 | + result["content"] = response.text |
| 43 | + version = re.search(r"\d{4}-\d\d-\d\d-apivectors-v(?P<version_number>\d+)\.csv", result["filename"]) |
| 44 | + result["version"] = version |
| 45 | + else: |
| 46 | + print("Failed to download ApiVector DB, response code: ", response.status_code) |
| 47 | + return result |
| 48 | + |
| 49 | + |
| 50 | +def check_malpedia_version(): |
| 51 | + remote_version = 0 |
| 52 | + response = requests.get( |
| 53 | + 'https://malpedia.caad.fkie.fraunhofer.de/api/get/version' |
| 54 | + ) |
| 55 | + if response.status_code == 200: |
| 56 | + response_json = response.json() |
| 57 | + remote_version =response_json["version"] |
| 58 | + else: |
| 59 | + print("Failed to check Malpedia version, response code: ", response.status_code) |
| 60 | + return remote_version |
| 61 | + |
| 62 | + |
| 63 | +def main(): |
| 64 | + db_version = get_newest_db_version() |
| 65 | + malpedia_version = check_malpedia_version() |
| 66 | + if db_version < malpedia_version: |
| 67 | + apivector_update = download_apivector_db() |
| 68 | + if apivector_update["version"]: |
| 69 | + delete_existing_dbs() |
| 70 | + update_db_path = "dbs" + os.sep + apivector_update["filename"] |
| 71 | + with open(update_db_path, "w") as fout: |
| 72 | + fout.write(apivector_update["content"]) |
| 73 | + print("Downloaded and stored ApiVector DB file: ", update_db_path) |
| 74 | + else: |
| 75 | + print("ApiVector update download failed.") |
| 76 | + else: |
| 77 | + print("Your ApiVector DB is the most recent ({})".format(malpedia_version)) |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + sys.exit(main()) |
0 commit comments