Skip to content

Instantly share code, notes, and snippets.

@adierebel
Created December 9, 2021 17:33
Show Gist options
  • Save adierebel/d58beb552bb897210cb9994b43b78d49 to your computer and use it in GitHub Desktop.
Save adierebel/d58beb552bb897210cb9994b43b78d49 to your computer and use it in GitHub Desktop.

Download Data

Download from http://www.linux-usb.org/usb.ids and save as data.txt

Run Script

Save this code below as parser.py Run with python parser.py Result -> usb.json

from json import dumps

file = open("data.txt", "r")
content = file.read()
file.close()

vendor_id = ""
vendors = {}
is_start = False
is_end = False
version = "-"

for line in content.splitlines():
    if len(line.strip()) > 0:
        if line.startswith("#"):
            if not is_start and line.startswith("# Date:"):
                version = line.replace("# Date:", "").strip()
            if line.startswith("#		interface  interface_name"):
                is_start = True
            elif line.startswith("# List of known device classes"):
                is_end = True
        else:
            if is_start and not is_end:
                if line.startswith("\t"):
                    if vendors.get(vendor_id):
                        splitted = line.split("  ")
                        product_id = splitted[0].strip()
                        del splitted[0]
                        product_name = "  ".join(splitted).strip()
                        vendors[vendor_id]["products"][product_id] = product_name
                else:
                    splitted = line.split("  ")
                    vendor_id = splitted[0]
                    del splitted[0]
                    vendor_name = "  ".join(splitted).strip()
                    if not vendors.get(vendor_id):
                        vendors[vendor_id] = {
                            "name": vendor_name,
                            "products": {}
                        }

data = {
    "source": "http://www.linux-usb.org/usb.ids",
    "version": version,
    "vendors": vendors
}

file_save = open("usb.json", "w")
file_save.write(dumps(data, indent=2))
file_save.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment