Last active
October 13, 2023 12:01
-
-
Save salgo60/eae5986297ad88a801549d0a37731817 to your computer and use it in GitHub Desktop.
Länkröta Naturkartan
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Naturkartan check | |
# https://gist.github.com/salgo60/eae5986297ad88a801549d0a37731817 | |
# | |
from datetime import datetime | |
import urllib3 | |
import sys | |
from SPARQLWrapper import SPARQLWrapper, JSON | |
endpoint_url = "https://query.wikidata.org/sparql" | |
http = urllib3.PoolManager() | |
# Check linkroot https://w.wiki/7mZ4 | |
# SPARQL | |
query = """SELECT ?wd ?wdLabel ?N ?oldURL WHERE { | |
?wd wdt:P10467 ?N | |
BIND (URI(CONCAT("https://naturkartan.se/",?N)) AS ?oldURL) | |
SERVICE wikibase:label { bd:serviceParam wikibase:language "sv". } | |
} | |
""" | |
def get_results(endpoint_url, query): | |
user_agent = "user:salgo60/%s.%s" % (sys.version_info[0], sys.version_info[1]) | |
sparql = SPARQLWrapper(endpoint_url, agent=user_agent) | |
sparql.setQuery(query) | |
sparql.setReturnFormat(JSON) | |
return sparql.query().convert() | |
''' Check if URLS is ok''' | |
def check(url,wd, wdLabel): | |
try: | |
r = http.request('GET', url) | |
except Exception as e: | |
print("\t\tError\t",str(e)) | |
return False | |
if r.status != 200: | |
print("Status: ",r.status, " \t", url, "\tWikidata: ", wd," - ", wdLabel) | |
return False | |
return True | |
start_time = datetime.now() | |
print("Last run: ", start_time) | |
results = get_results(endpoint_url, query) | |
print ("Number records i Wikidata: " + str(len(results["results"]["bindings"]))) | |
ok = 0 | |
notok = 0 | |
for result in results["results"]["bindings"]: | |
#print(result) | |
try: | |
currentURL = result["oldURL"]["value"] | |
wd = result["wd"]["value"] | |
wdLabel = result["wdLabel"]["value"] | |
if check(currentURL,wd, wdLabel): | |
ok += 1 | |
else: | |
notok += 1 | |
except Exception as error: | |
print("An error occurred: ", wd, " - ", type(error).__name__) # An error occurred: NameError | |
print("OK: ",ok,"\t not ok",notok) | |
end = datetime.now() | |
print("Ended: ", end) | |
print('Time |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wohoo!