import json

import attr
import lxml.html


@attr.s
class Child:
    title = attr.ib(default=None)
    link = attr.ib(default=None)
    subtree = attr.ib(default=[])


def first(iterable, default=None):
    try:
        return next(iter(iterable))
    except StopIteration:
        return None


def get_tree(node):
    childs = []
    for child_node in node.iterchildren():
        child = get_child(child_node)
        childs.append(child)
    return childs


def get_child(child_node):
    child = Child()
    tag = child_node.tag
    if tag == 'ul':
        child.subtree = get_tree(child_node)
    elif tag == 'li':
        child.title = child_node.find('span').text_content()
        child.link = first(child_node.xpath('span/a/@href'))
        ul = child_node.find('ul')
        if ul is not None:
            child.subtree = get_tree(ul)
    else:
        raise Exception("Unexpected tag: {}".format(tag))
    return attr.asdict(child)


def main():
    with open('./rutracker_cr_forum_map.html') as fin:
        html = lxml.html.fromstring(fin.read())
    fmap = html.get_element_by_id('f-map')
    tree = get_tree(fmap)
    with open('rutracker_cr_forum_map.json', 'w') as fout:
        json.dump(tree, fout, ensure_ascii=False, indent=4)


if __name__ == "__main__":
    main()
