import shutil
import os
import json
import glob
import yaml
import sys
import urllib
import ssl
import csv
import time
import requests
import json
import csv
from rdflib import URIRef, BNode, Literal, Graph
from rdflib.namespace import RDF, RDFS, FOAF, XSD
from rdflib import Namespace



all = Graph()

with open("data/dict.json") as f:
    ln_map = json.load(f)

st_path = "../data/index.json"

with open(st_path) as f:
    result = json.load(f)

    uris = []

    for obj in result:

        fields = ["spatial", "agential"]

        for field in fields:
            values = obj[field]
            for value in values:
                uri = "chname:"+value
                if field == "spatial":
                    uri = "place:"+value

        if uri not in uris:
            uris.append(uri)

    for uri in uris:

        print(uri)

        tmp = uri.split(":")
        prefix = tmp[0]
        suffix = tmp[1]

        ln = suffix
        ln_org = ""

        if ln in ln_map:
            ln_org = ln
            ln = ln_map[ln]

        if len(ln) > 20:
            continue

        


        # ln = obj["uri"].split(":")[1]

        '''
        wiki_path = "data/wikidata/"+ln+".json"

        wiki = {}

        if os.path.exists(wiki_path):
            with open(wiki_path) as f:
                wiki = json.load(f)

        # sameAs
        stmt = (subject, URIRef("http://www.w3.org/2002/07/owl#sameAs"), URIRef(wiki_url))
        all.add(stmt)

        obj = wiki["entities"][wiki_url.split("/")[-1]]

        # description
        if "descriptions" in obj and "ja" in obj["descriptions"]:
            stmt = (subject, URIRef("http://schema.org/description"), Literal(obj["descriptions"]["ja"]["value"], lang="ja"))
            all.add(stmt)

        # label
        if "labels" in obj and "ja" in obj["labels"]:
            stmt = (subject, RDFS.label, Literal(obj["labels"]["ja"]["value"]))
            all.add(stmt)


            
        ln = wiki_url.split("/")[-1]
        '''

        db_path = "data/dbpedia_ja/"+ln+".json"
        wiki_path = "data/wikidata/"+ln+".json"

        db = {}
        wiki = {}

        if os.path.exists(db_path):
            with open(db_path) as f:
                db = json.load(f)

        if os.path.exists(wiki_path):
            with open(wiki_path) as f:
                wiki = json.load(f)

        db_uri = "http://ja.dbpedia.org/resource/"+ln

        if db_uri not in db:
            print("not" , db_uri)
            continue
        
        # ######
        subject = URIRef("https://shibusawa-dlab.github.io/lab1/api/"+prefix+"/"+ln)

        if prefix == "chname":
            stmt = (subject, RDF.type, URIRef("https://jpsearch.go.jp/term/type/Agent"))
            all.add(stmt)
        elif prefix == "time":
            stmt = (subject, RDF.type, URIRef("https://jpsearch.go.jp/term/type/Time"))
            all.add(stmt)
        elif prefix == "place":
            stmt = (subject, RDF.type, URIRef("https://jpsearch.go.jp/term/type/Place"))
            all.add(stmt)
        elif prefix == "event":
            stmt = (subject, RDF.type, URIRef("https://jpsearch.go.jp/term/type/Event"))
            all.add(stmt)
        elif prefix == "org":
            stmt = (subject, RDF.type, URIRef("https://jpsearch.go.jp/term/type/Organization"))
            all.add(stmt)
        elif prefix == "keyword":
            stmt = (subject, RDF.type, URIRef("https://jpsearch.go.jp/term/type/Keyword"))
            all.add(stmt)
        elif prefix == "type":
            stmt = (subject, RDF.type, URIRef("https://jpsearch.go.jp/term/type/Type"))
            all.add(stmt)
        # ######

        obj = db[db_uri]

        stmt = (subject, URIRef("http://www.w3.org/2002/07/owl#sameAs"), URIRef(db_uri))
        all.add(stmt)

        if "http://dbpedia.org/ontology/thumbnail" in obj:
            stmt = (subject, URIRef("http://schema.org/image"), URIRef(obj["http://dbpedia.org/ontology/thumbnail"][0]["value"]))
            all.add(stmt)

        if "http://www.w3.org/2000/01/rdf-schema#label" in obj:
            labels = obj["http://www.w3.org/2000/01/rdf-schema#label"]
            for label in labels:
                if label["lang"] == "ja":
                    stmt = (subject, RDFS.label, Literal(label["value"]))
                    all.add(stmt)

        if "http://www.w3.org/2000/01/rdf-schema#comment" in obj:
            labels = obj["http://www.w3.org/2000/01/rdf-schema#comment"]
            for label in labels:
                stmt = (subject, URIRef("http://schema.org/description"), Literal(label["value"], lang=label["lang"]))
                all.add(stmt)

        if "http://www.w3.org/2002/07/owl#sameAs" in obj:
            labels = obj["http://www.w3.org/2002/07/owl#sameAs"]
            for label in labels:
                value = label["value"]
                if "http://dbpedia.org" in value or "http://ja.dbpedia.org" in value or "www.wikidata.org" in value:
                    stmt = (subject, URIRef("http://www.w3.org/2002/07/owl#sameAs"), URIRef(value))
                    all.add(stmt)

        # 位置情報
        '''
        if "point" in obj and prefix == "place":
            value = obj["point"]["value"].split(" ")

            # addGeo関数
            geoUri = addGeo({
                "lat" : float(value[0]),
                "long": float(value[1])
            })
            stmt = (subject, URIRef("http://schema.org/geo"), geoUri)

            if suffix not in places:
                places[suffix] = {
                    "lat" : float(value[0]),
                    "long": float(value[1])
                }

            all.add(stmt)
        '''

        # 正規化前
        if ln_org != "" and ln != ln_org:
            stmt = (subject, URIRef("http://schema.org/name"), Literal(ln_org))
            all.add(stmt)

path = "data/all.json"

all.serialize(destination=path, format='json-ld')
all.serialize(destination=path.replace(".json", ".rdf"), format='pretty-xml')
[((20, 6, 20, 13), 'rdflib.Graph', 'Graph', ({}, {}), '()', False, 'from rdflib import URIRef, BNode, Literal, Graph\n'), ((23, 13, 23, 25), 'json.load', 'json.load', ({(23, 23, 23, 24): 'f'}, {}), '(f)', False, 'import json\n'), ((28, 13, 28, 25), 'json.load', 'json.load', ({(28, 23, 28, 24): 'f'}, {}), '(f)', False, 'import json\n'), ((105, 11, 105, 34), 'os.path.exists', 'os.path.exists', ({(105, 26, 105, 33): 'db_path'}, {}), '(db_path)', False, 'import os\n'), ((109, 11, 109, 36), 'os.path.exists', 'os.path.exists', ({(109, 26, 109, 35): 'wiki_path'}, {}), '(wiki_path)', False, 'import os\n'), ((120, 18, 120, 84), 'rdflib.URIRef', 'URIRef', ({(120, 25, 120, 83): "'https://shibusawa-dlab.github.io/lab1/api/' + prefix + '/' + ln"}, {}), "('https://shibusawa-dlab.github.io/lab1/api/' + prefix + '/' + ln)", False, 'from rdflib import URIRef, BNode, Literal, Graph\n'), ((147, 25, 147, 71), 'rdflib.URIRef', 'URIRef', ({(147, 32, 147, 70): '"""http://www.w3.org/2002/07/owl#sameAs"""'}, {}), "('http://www.w3.org/2002/07/owl#sameAs')", False, 'from rdflib import URIRef, BNode, Literal, Graph\n'), ((147, 73, 147, 87), 'rdflib.URIRef', 'URIRef', ({(147, 80, 147, 86): 'db_uri'}, {}), '(db_uri)', False, 'from rdflib import URIRef, BNode, Literal, Graph\n'), ((107, 21, 107, 33), 'json.load', 'json.load', ({(107, 31, 107, 32): 'f'}, {}), '(f)', False, 'import json\n'), ((111, 23, 111, 35), 'json.load', 'json.load', ({(111, 33, 111, 34): 'f'}, {}), '(f)', False, 'import json\n'), ((123, 39, 123, 87), 'rdflib.URIRef', 'URIRef', ({(123, 46, 123, 86): '"""https://jpsearch.go.jp/term/type/Agent"""'}, {}), "('https://jpsearch.go.jp/term/type/Agent')", False, 'from rdflib import URIRef, BNode, Literal, Graph\n'), ((151, 29, 151, 62), 'rdflib.URIRef', 'URIRef', ({(151, 36, 151, 61): '"""http://schema.org/image"""'}, {}), "('http://schema.org/image')", False, 'from rdflib import URIRef, BNode, Literal, Graph\n'), ((151, 64, 151, 128), 'rdflib.URIRef', 'URIRef', ({(151, 71, 151, 127): "obj['http://dbpedia.org/ontology/thumbnail'][0]['value']"}, {}), "(obj['http://dbpedia.org/ontology/thumbnail'][0]['value'])", False, 'from rdflib import URIRef, BNode, Literal, Graph\n'), ((198, 29, 198, 61), 'rdflib.URIRef', 'URIRef', ({(198, 36, 198, 60): '"""http://schema.org/name"""'}, {}), "('http://schema.org/name')", False, 'from rdflib import URIRef, BNode, Literal, Graph\n'), ((198, 63, 198, 78), 'rdflib.Literal', 'Literal', ({(198, 71, 198, 77): 'ln_org'}, {}), '(ln_org)', False, 'from rdflib import URIRef, BNode, Literal, Graph\n'), ((126, 39, 126, 86), 'rdflib.URIRef', 'URIRef', ({(126, 46, 126, 85): '"""https://jpsearch.go.jp/term/type/Time"""'}, {}), "('https://jpsearch.go.jp/term/type/Time')", False, 'from rdflib import URIRef, BNode, Literal, Graph\n'), ((164, 33, 164, 72), 'rdflib.URIRef', 'URIRef', ({(164, 40, 164, 71): '"""http://schema.org/description"""'}, {}), "('http://schema.org/description')", False, 'from rdflib import URIRef, BNode, Literal, Graph\n'), ((164, 74, 164, 117), 'rdflib.Literal', 'Literal', (), '', False, 'from rdflib import URIRef, BNode, Literal, Graph\n'), ((129, 39, 129, 87), 'rdflib.URIRef', 'URIRef', ({(129, 46, 129, 86): '"""https://jpsearch.go.jp/term/type/Place"""'}, {}), "('https://jpsearch.go.jp/term/type/Place')", False, 'from rdflib import URIRef, BNode, Literal, Graph\n'), ((158, 49, 158, 72), 'rdflib.Literal', 'Literal', ({(158, 57, 158, 71): "label['value']"}, {}), "(label['value'])", False, 'from rdflib import URIRef, BNode, Literal, Graph\n'), ((172, 37, 172, 83), 'rdflib.URIRef', 'URIRef', ({(172, 44, 172, 82): '"""http://www.w3.org/2002/07/owl#sameAs"""'}, {}), "('http://www.w3.org/2002/07/owl#sameAs')", False, 'from rdflib import URIRef, BNode, Literal, Graph\n'), ((172, 85, 172, 98), 'rdflib.URIRef', 'URIRef', ({(172, 92, 172, 97): 'value'}, {}), '(value)', False, 'from rdflib import URIRef, BNode, Literal, Graph\n'), ((132, 39, 132, 87), 'rdflib.URIRef', 'URIRef', ({(132, 46, 132, 86): '"""https://jpsearch.go.jp/term/type/Event"""'}, {}), "('https://jpsearch.go.jp/term/type/Event')", False, 'from rdflib import URIRef, BNode, Literal, Graph\n'), ((135, 39, 135, 94), 'rdflib.URIRef', 'URIRef', ({(135, 46, 135, 93): '"""https://jpsearch.go.jp/term/type/Organization"""'}, {}), "('https://jpsearch.go.jp/term/type/Organization')", False, 'from rdflib import URIRef, BNode, Literal, Graph\n'), ((138, 39, 138, 89), 'rdflib.URIRef', 'URIRef', ({(138, 46, 138, 88): '"""https://jpsearch.go.jp/term/type/Keyword"""'}, {}), "('https://jpsearch.go.jp/term/type/Keyword')", False, 'from rdflib import URIRef, BNode, Literal, Graph\n'), ((141, 39, 141, 86), 'rdflib.URIRef', 'URIRef', ({(141, 46, 141, 85): '"""https://jpsearch.go.jp/term/type/Type"""'}, {}), "('https://jpsearch.go.jp/term/type/Type')", False, 'from rdflib import URIRef, BNode, Literal, Graph\n')]