import bz2
import io
import sys
import urllib.request

import networkx as nx
import arpy


def read_wot(keysfile, namesfile, sigsfile):
    G = nx.MultiDiGraph()

    keys = list()
    for name in namesfile:
        keyid = int.from_bytes(keysfile.read(4), byteorder="big")
        keys.append(keyid)
        G.add_node(keyid, name=name.decode("utf-8", errors="replace"))

    for owner in keys:
        numsigs = int.from_bytes(sigsfile.read(4), byteorder="big")
        for i in range(numsigs):
            signer_index = int.from_bytes(sigsfile.read(4), byteorder="big")
            signer_index = signer_index & 0x0FFFFFFF
            signer = keys[signer_index]
            G.add_edge(signer, owner)

    return G


def latest_wot():
    url = "http://wot.christoph-egger.org/download/latest.wot"
    return urllib.request.urlopen(url)


def get_files(wot_file):
    wot_files = {}

    with bz2.BZ2File(wot_file) as wot_ar:
        wot_archive = arpy.Archive(fileobj=wot_ar)
        for f in wot_archive:
            filename = f.header.name.decode()
            contents = io.BytesIO(f.read())
            wot_files[filename] = contents

    return wot_files

if __name__ == "__main__":

    if len(sys.argv) < 2:
        wot_file = latest_wot()
    else:
        filename = sys.argv[1]
        wot_file = open(filename, "rb")

    files = get_files(wot_file)

    G = read_wot(files["keys"], files["names"], files["signatures"])
    nx.write_gexf(G, "wot.gexf")

@@ -1,12 +1,28 @@
+import argparse%0A
 import bz2%0Ai
@@ -1212,29 +1212,131 @@
     
-if len(sys.argv) %3C 2:
+parser = argparse.ArgumentParser(description=%22Generate web of trust graph%22)%0A    parser.add_argument(%0A        %22-w%22, %22--wot%22,
 %0A   
@@ -1340,24 +1340,30 @@
         
+dest=%22
 wot_file
  = lates
@@ -1358,64 +1358,323 @@
 file
- = latest_wot()%0A    else:%0A        filename = sys.argv%5B1%5D
+name%22,%0A        help=%22use local .wot file%22,%0A    )%0A    parser.add_argument(%0A        %22-f%22, %22--format%22,%0A        choices=%5B%22gexf%22, %22graphml%22, %22yaml%22%5D,%0A        dest=%22file_format%22,%0A        default=%22gexf%22,%0A        help=%22specify file format of outputted graph%22,%0A    )%0A    args = parser.parse_args()%0A%0A    if args.wot_filename:
 %0A   
@@ -1694,16 +1694,25 @@
  = open(
+args.wot_
 filename
@@ -1718,16 +1718,58 @@
 e, %22rb%22)
+%0A    else:%0A        wot_file = latest_wot()
 %0A%0A    fi
@@ -1868,37 +1868,234 @@
 %22%5D)%0A
-    nx.write_gexf(G, %22wot.gexf
+%0A    if args.file_format == %22gexf%22:%0A        nx.write_gexf(G, %22wot.gexf%22)%0A    elif args.file_format == %22graphml%22:%0A        nx.write_graphml(G, %22wot.graphml%22)%0A    elif args.file_format == %22yaml%22:%0A        nx.write_yaml(G, %22wot.yaml
 %22)%0A

Allow user to export to different formats