#!/usr/bin/env python3 

import errno
import os
import sys
import xml.etree.ElementTree as ET
import requests
import rdflib
import re

def escape_string (a_string):
    if not isinstance (a_string, str):
        a_string = ""
    
    return "\"" + a_string \
            .replace ('"', '\\"') \
            .replace ('\r', '\\r') \
            .replace ('\n', '\\n') \
            .replace ('\\', '\\\\') + "\""

# This is a temporary fix because h-node has some ill-formatted IRIs
# The <permalink> XML tag should become available in a month or so.
def clean_iri (a_uri):
    if len(a_uri) < 1:
        return ""
    
    return re.sub (r'(view/[a-z]+/[0-9]+)(/.*)$', r'\1', a_uri)
    
# h-node XML file
hnode_xml_path = "https://h-node.org/download/all/en"

# Download database and parse XML
# https://web.archive.org/web/20170415073249/https://docs.python.org/3/library/xml.etree.elementtree.html
try:
    tree_root = ET.fromstring (requests.get(hnode_xml_path).text)
except Exception as e:
    print("Cannot download database from h-node")
    print(e)
    exit()

hnode_turtle =  "PREFIX dcterms: <http://purl.org/dc/terms/>\n" \
                "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" \
                "PREFIX schema: <http://schema.org/>\n"

# Convert XML to Turtle
for tree_el in tree_root: # Loop each element in the XML
    if tree_el.tag != "device":
        continue
    
    hnode_turtle += "\n<" + clean_iri (tree_el.find("link").text) + ">\n"
    hnode_turtle += "  rdf:type schema:IndividualProduct ;\n"
    
    for property in tree_el:
        if property.tag == "link":
            pass
        elif property.tag == "id":
            hnode_turtle += "  dcterms:identifier \"" + property.text + "\" ;\n"
        elif property.tag == "model_name":
            hnode_turtle += "  schema:name " + escape_string (property.text) + " ;\n"
        elif property.tag == "other_names":
            hnode_turtle += "  schema:alternateName " + escape_string (property.text) + " ;\n"
        elif property.tag == "description":
            hnode_turtle += "  schema:description " + escape_string (property.text) + " ;\n"
        elif property.tag == "vendor":
            hnode_turtle +=  "  schema:manufacturer [\n" \
                             "    rdf:type schema:Organization ;\n" \
                             "    schema:name " + escape_string (property.text) + "\n" \
                             "  ] ;\n"
        elif property.tag == "vendorid_productid":
            hnode_turtle += "  schema:productID " + escape_string (property.text) + " ;\n"
        elif property.tag == "year":
            hnode_turtle += "  schema:releaseDate \"" + property.text + "\" ;\n"
        else:
            hnode_turtle +=  "  schema:PropertyValue [\n" \
                             "    schema:name " + escape_string (property.tag) + " ;\n" \
                             "    schema:value " + escape_string (property.text) + "\n" \
                             "  ] ;\n"

    hnode_turtle += "  .\n"

# Write the output we've just created
with open ("data/h-node.ttl", 'w') as nqFile:
    nqFile.write (hnode_turtle)
