<commit_before>__author__ = "<NAME>"

import argparse
import datetime
import requests
from sparql.general import *
from sparql.languages import workflow as languages
from sparql.__init__ import CONSTRUCT_GRAPH_PRE_URI
from sparql.__init__ import CONSTRUCT_GRAPH_POST_URI

def execute_queries(queries, url):
    for i, sparql in enumerate(queries):
        print("{}.".format(i+1), end="")
        print(sparql[1:sparql.find('\n')])
        result = requests.post(
            url,
            data={"update": sparql})
        
        if result.status_code > 399:
            print("..ERROR with {} for {}\n{}".format(
                sparql,
                url,
                result.text))

#The below test query was to test direct links one at a time for a service call to dbpedia            
def test_queries(queries, url):
   result = requests.post(
        url,
        data={"query": """PREFIX bf: <http://bibframe.org/vocab/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT ?ref WHERE { ?bfLangId bf:dbpRef ?ref .}""",
        'format':'json'})
   uriItems = result.json().get('results').get('bindings')
   for uri in uriItems:
       qryStr = """PREFIX bf: <http://bibframe.org/vocab/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT * WHERE 
{ 
  SERVICE <http://DBpedia.org/sparql>
    { 
       <"""+ uri.get('ref').get('value') + """> rdfs:label ?langLabel.
    }.
}"""
       testUri = requests.post(
           url,
           data={"query": qryStr,
           'format':'json'})
       print(uri.get('ref').get('value') ," | " ,testUri.status_code)
       print(qryStr)
       if testUri.status_code > 399:    	
           print("Error")

def pull_graph(uriString, url):
   QSTR = CONSTRUCT_GRAPH_PRE_URI+" <"+uriString.strip()+"> "+CONSTRUCT_GRAPH_POST_URI
   print(QSTR)
   result = requests.post(
        url,
        data={"query": QSTR}
        )
   print(result.text)
   if result.status_code > 399:    	
       print("Error")

 

def main(args):
    start = datetime.datetime.now()
    print("Starting {} Workflow at {}".format(
        args.workflow,
        start.isoformat()))
    if args.workflow.startswith("languages"):
        execute_queries(languages, args.triplestore)
    if args.workflow.startswith("test"):
        test_queries(languages, args.triplestore)
    if args.workflow.startswith("graph"):<commit_msg>Fixed format call. Need to figure out how to read the content<commit_after>__author__ = "<NAME>"

import argparse
import datetime
import requests
from sparql.general import *
from sparql.languages import workflow as languages
from sparql.__init__ import CONSTRUCT_GRAPH_PRE_URI
from sparql.__init__ import CONSTRUCT_GRAPH_POST_URI

def execute_queries(queries, url):
    for i, sparql in enumerate(queries):
        print("{}.".format(i+1), end="")
        print(sparql[1:sparql.find('\n')])
        result = requests.post(
            url,
            data={"update": sparql})
        
        if result.status_code > 399:
            print("..ERROR with {} for {}\n{}".format(
                sparql,
                url,
                result.text))

#The below test query was to test direct links one at a time for a service call to dbpedia            
def test_queries(queries, url):
   result = requests.post(
        url,
        data={"query": """PREFIX bf: <http://bibframe.org/vocab/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT ?ref WHERE { ?bfLangId bf:dbpRef ?ref .}""",
        'format':'json'})
   uriItems = result.json().get('results').get('bindings')
   for uri in uriItems:
       qryStr = """PREFIX bf: <http://bibframe.org/vocab/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT * WHERE 
{ 
  SERVICE <http://DBpedia.org/sparql>
    { 
       <"""+ uri.get('ref').get('value') + """> rdfs:label ?langLabel.
    }.
}"""
       testUri = requests.post(
           url,
           data={"query": qryStr},
           headers={"Accept":"application/json"})
       print(uri.get('ref').get('value') ," | " ,testUri.status_code)
       print(qryStr)
       if testUri.status_code > 399:    	
           print("Error")

def pull_graph(uriString, url):
   QSTR = CONSTRUCT_GRAPH_PRE_URI+" <"+uriString.strip()+"> "+CONSTRUCT_GRAPH_POST_URI
   print(QSTR)
   result = requests.post(
        url,
        data={"query": QSTR},
        headers={"Accept":"application/x-turtle"}
        )
   print(result.headers['content-type'])
   print(result.content)
   if result.status_code > 399:    	
       print("Error")

 

def main(args):
    start = datetime.datetime.now()
    print("Starting {} Workflow at {}".format(
        args.workflow,
        start.isoformat()))
    if args.workflow.startswith("languages"):
        execute_queries(languages, args.triplestore)
    if args.workflow.startswith("test"):
        test_queries(languages, args.triplestore)
    if args.workflow.startswith("graph"):
{'fraction_non_alphanumeric': 0.1516023795816542, 'fraction_numerical': 0.00422183841872961, 'mean_word_length': 2.746944644140906, 'pattern_counts': {'":': 10, '<': 27, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1226835', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1723, 'n_words': 358}