from urllib.request import urlopen

from rdflib import Graph, Literal
from rdflib.namespace import DC, RDF
from util import str2id
from ns import ns_division, ns_property, ns_type

def main():
    graph = Graph()
    URL = 'http://www.electionsquebec.qc.ca/documents/donnees-ouvertes/Liste_circonscriptions.txt'
    with urlopen(URL) as fp:
        contents = fp.read().decode('utf-8')
    lines = contents.splitlines()
    for line in lines:
        div_id, div_name = line.split(';')
        current_division = ns_division[str2id(div_name)]
        graph.add((current_division, RDF.type, ns_type.ProvincialDivision))
        graph.add((current_division, DC.description, Literal(div_name)))
        graph.add((current_division, ns_property.dgeqDivisionId, Literal(int(div_id))))
    graph.serialize('division.rdf')

if __name__ == '__main__':
    main()
