forked from NFDI4Culture/collection-publishing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
funktionen.py
178 lines (152 loc) · 6.86 KB
/
funktionen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from datetime import datetime
import sys
import time
from SPARQLWrapper import SPARQLWrapper, JSON
import requests
from PIL import Image
import html
import re
from markdownify import markdownify
endpoint_url = "https://computational-publishing-service.wikibase.cloud/query/sparql"
#where the sparql queries come from
query_txt = """PREFIX cps: <https://computational-publishing-service.wikibase.cloud/entity/>
PREFIX cpss: <https://computational-publishing-service.wikibase.cloud/entity/statement/>
PREFIX cpsv: <https://computational-publishing-service.wikibase.cloud/value/>
PREFIX cpspt: <https://computational-publishing-service.wikibase.cloud/prop/direct/>
PREFIX cpsp: <https://computational-publishing-service.wikibase.cloud/prop/>
PREFIX cpsps: <https://computational-publishing-service.wikibase.cloud/prop/statement/>
PREFIX cpspq: <https://computational-publishing-service.wikibase.cloud/prop/qualifier/>
SELECT ?textItem ?kuratorLabel ?textUrl
WHERE
{
<placeholder>
?textItem cpsp:P46 ?kuratorStatement.
?kuratorStatement cpsps:P46 ?kuratorItem.
?kuratorItem rdfs:label ?kuratorLabel.
?textItem cpsp:P57 ?urlstatement.
?urlstatement cpsps:P57 ?textUrl.
}"""
query_img ="""PREFIX cps: <https://computational-publishing-service.wikibase.cloud/entity/>
PREFIX cpss: <https://computational-publishing-service.wikibase.cloud/entity/statement/>
PREFIX cpsv: <https://computational-publishing-service.wikibase.cloud/value/>
PREFIX cpspt: <https://computational-publishing-service.wikibase.cloud/prop/direct/>
PREFIX cpsp: <https://computational-publishing-service.wikibase.cloud/prop/>
PREFIX cpsps: <https://computational-publishing-service.wikibase.cloud/prop/statement/>
PREFIX cpspq: <https://computational-publishing-service.wikibase.cloud/prop/qualifier/>
SELECT DISTINCT ?itemLabel ?itemDescr ?imgItem ?imgUrl ?publishDate
WHERE
{
?imgItem cpsp:P107 ?urlStatement.
?urlStatement cpsps:P107 ?imgUrl.
?imgItem cpsp:P60 ?dateStatement.
?dateStatement cpsps:P60 ?publishDate.
?imgItem cpsp:P6 ?partOfStatement.
?partOfStatement cpsps:P6 ?partOfItem.
<placeholder>
FILTER (datatype(?publishDate) = xsd:edtf)
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en,de".
?imgItem rdfs:label ?itemLabel.
?imgItem schema:description ?itemDescr.
}
}"""
query_graph = """PREFIX cps: <https://computational-publishing-service.wikibase.cloud/entity/>
PREFIX cpss: <https://computational-publishing-service.wikibase.cloud/entity/statement/>
PREFIX cpsv: <https://computational-publishing-service.wikibase.cloud/value/>
PREFIX cpspt: <https://computational-publishing-service.wikibase.cloud/prop/direct/>
PREFIX cpsp: <https://computational-publishing-service.wikibase.cloud/prop/>
PREFIX cpsps: <https://computational-publishing-service.wikibase.cloud/prop/statement/>
PREFIX cpspq: <https://computational-publishing-service.wikibase.cloud/prop/qualifier/>
SELECT ?x ?y
WHERE
{
?a cpsp:P2 ?c.
?c cpsps:P2 ?d.
?a rdfs:label ?x.
?d rdfs:label ?y.
}LIMIT 1"""
query_graph2 = """
SELECT ?a ?b ?c
WHERE
{
?a rdfs:label ?c
}LIMIT 100"""
def run_query(endpoint_url, query):
user_agent = "WDQS-example Python/%s.%s" % (sys.version_info[0], sys.version_info[1])
# TODO adjust user agent; see https://w.wiki/CX6
sparql = SPARQLWrapper(endpoint_url, agent=user_agent)
sparql.setQuery(query)
sparql.setMethod("POST") #this NEEDS to be added to get results (not included in the wikibase python example code)
sparql.setReturnFormat(JSON)
return sparql.query().convert()
def get_text(textitem_id):
q = ""
if textitem_id:
q = query_txt.replace("<placeholder>", "cps:"+textitem_id+" cpsp:P46 ?kuratorStatement.")
else:
q = query_txt.replace("<placeholder>","")
results_txt = run_query(endpoint_url, q)
for item in results_txt["results"]["bindings"]:
print('Wikibase link: ' + '[' + item['textItem']['value'] + ']' + '(' + item['textItem']['value'] + ')' + '\n')
print('Kurator: ' + item['kuratorLabel']['value'] + '\n')
headers = {'User-Agent': 'Ex_Books_conference_bot/0.0 (https://github.com/SimonXIX/Experimental_Books_workshop; [email protected])'}
r = requests.get(item['textUrl']['value'], headers=headers, stream=True)
text = str(r.text) ### CHANGED from r.content
text = text.removeprefix("<!DOCTYPE html>") ### changed from "b'<!DOCTYPE html>"
# Konvertiere HTML zu Markdown
markdown_text = markdownify(text)
# Fußnoten im Fließtext umwandeln
# markdown_text = re.sub(r'\[(\d+)\]', r'[^\1]', markdown_text)
# Fußnotenliste am Ende des Dokuments umwandeln und Leerzeichen entfernen
# markdown_text = re.sub(r'\n \[\^(\d+)\]', r'\n[^\1]:', markdown_text)
print(markdown_text)
def get_delay(date):
try:
date = datetime.datetime.strptime(date, '%a, %d %b %Y %H:%M:%S GMT')
timeout = int((date - datetime.datetime.now()).total_seconds())
except ValueError:
timeout = int(date)
return timeout
def fetch_image_by_url(url, headers):
r = requests.get(url, headers=headers, stream=True)
if r.status_code == 200:
im = Image.open(r.raw)
return im
if r.status_code == 500:
return None
if r.status_code == 403:
return None
if r.status_code == 429:
timeout = get_delay(r.headers['retry-after'])
print('Timeout {} m {} s'.format(timeout // 60, timeout % 60))
time.sleep(timeout)
fetch_image_by_url(url, headers)
def get_img(partOfItem_id):
q = ""
if partOfItem_id:
q = query_img.replace("<placeholder>", "?partOfStatement cpsps:P6 cps:"+partOfItem_id+".")
else:
q = query_img.replace("<placeholder>","")
results_img = run_query(endpoint_url, q)
for item in results_img["results"]["bindings"]:
#print(item)
print('Wikibase link: ' + '[' + item['imgItem']['value'] + ']' + '(' + item['imgItem']['value'] + ')' + '\n')
print('Title: ' + item['itemLabel']['value'] + '\n')
print('Year: ' + item['publishDate']['value'] + '\n')
print('Description: ' + html.unescape(item['itemDescr']['value']) + '\n')
# get image from image URL and display resized version
image_url=item['imgUrl']['value']
headers = {'User-Agent': 'Ex_Books_conference_bot/0.0 (https://github.com/SimonXIX/Experimental_Books_workshop; [email protected])'}
im = fetch_image_by_url(image_url, headers)
im.thumbnail((500, 500), Image.Resampling.LANCZOS)
display(im)
print('\n\n')
def get_graph():
import VizKG.visualize as vkg
results_graph1 = run_query(endpoint_url, query_graph)
#print(results_graph1)
#print('---')
results_graph2 = run_query(endpoint_url, query_graph2)
#print(results_graph2)
chart = vkg(sparql_query=query_graph2, sparql_service_url=endpoint_url, chart='wordcloud')
chart.plot()