-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoc.py
executable file
·1197 lines (997 loc) · 40.6 KB
/
oc.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2016, Silvio Peroni <[email protected]>
#
# Permission to use, copy, modify, and/or distribute this software for any purpose
# with or without fee is hereby granted, provided that the above copyright notice
# and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
# DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
# SOFTWARE.
__author__ = 'essepuntato'
import web
import json
from src.wl import WebLogger
from src.rrh import RewriteRuleHandler
from src.ldd import LinkedDataDirector
from src.ved import VirtualEntityDirector
from src.ramose import APIManager, Operation, HTMLDocumentationHandler
from src.oci import OCIManager
from src.intrepid import InTRePIDManager
import requests
import urllib.parse as urlparse
import urllib.request as urllib
import re
import csv
from datetime import datetime
from os import path
from os import listdir
from os import urandom
from io import StringIO
from hashlib import sha1
from urllib.parse import unquote, parse_qs
from redis import Redis
import uuid
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from prometheus_client import Counter, CollectorRegistry, generate_latest, Gauge, Info
from prometheus_client.parser import text_fd_to_metric_families
# Load the configuration file
with open("conf.json") as f:
c = json.load(f)
with open(c["auth_file"]) as f:
c_auth = json.load(f)
c_smtp = c_auth["smtp"]
c_captcha = c_auth["captcha"]
pages = [
{"name": "", "label": "Home"},
{"name": "about", "label": "About"},
{"name": "membership", "label": "Help us"},
{"name": "model", "label": "Data Model"},
{"name": "datasets", "label": "Datasets"},
{"name": "querying", "label": "Querying Data"},
{"name": "tools", "label": "Tools"},
{"name": "download", "label": "Download"},
{"name": "publications", "label": "Publications"}
]
active = {
"corpus": "datasets",
"index": "datasets",
"meta": "datasets",
"coci": "datasets",
"doci": "datasets",
"poci": "datasets",
"croci": "datasets",
"ccc": "datasets",
"oci": "tools",
"intrepid": "tools",
"api": "querying",
"sparql": "querying",
"search": "querying"
}
urls = (
# Generic URLs
"/", "Home",
"/(wikidata)(/api/.+)", "Api",
"/(index)(/api/.+)", "Api",
"/index/([^/]+)(/api/.+)", "Api",
"/index/sparql", "SparqlIndex",
"/index/search", "SearchIndex",
"/index/browser/(.+)", "BrowserIndex",
"/index/coci", "Coci",
"/index/doci", "Doci",
"/index/poci", "Poci",
"/index/croci", "Croci",
"/index/(.*)?", "IndexContentNegotiation",
# META related urls
"/(meta)(/api/.+)", "Api",
"/meta/sparql", "SparqlMeta",
"/meta/(../.+)", "MetaContentNegotiation",
"/meta", "Meta",
# CCC related urls
"/(ccc)(/api/.+)", "Api",
"/ccc", "CCC",
"/ccc/sparql", "SparqlCCC",
"/ccc/search", "SearchCCC",
"/ccc/browser/(.+)", "BrowserCCC",
"/ccc/(../.+)", "CCCContentNegotiation",
"/(about)", "About",
"/(model)", "Model",
"/(datasets)", "Datasets",
"/corpus", "Corpus",
"/corpus/(.+)", "CorpusContentNegotiation",
"/corpus/", "CorpusContentNegotiation",
"/virtual/(.+)", "Virtual",
"/oci(/.+)?", "OCI",
"/intrepid(/.+)?", "InTRePID",
"/(download)", "Download",
"/(policy)", "Policy",
# Legacy pages
"/(download)_legacy", "DownloadLegacy",
# OCC SPARQL-related urls > all redirected to Index
"/sparql", "SparqlIndex",
"/search", "SearchIndex",
"/browser/(.+)", "BrowserIndex",
"()(/api/.+)", "Api",
# Other generic URLs
"/(tools)", "Tools",
"/(membership)", "Membership",
"/(querying)", "Querying",
"/(publications)", "Publications",
"/(licenses)", "Licenses",
"(/paper/.+)", "RawGit",
"/index", "Index",
"/robots.txt", "Robots",
# Statistics
"/(statistics)", "StatisticsIndex",
"/statistics/(.+)", "Statistics",
# Token
"/accesstoken", "AuthCode",
"/accesstoken/(.+)", "AuthCodeConfirm",
)
render = web.template.render(c["html"])
#set auto reload of html template
web.config["TEMPLATES_AUTO_RELOAD"] = True
rewrite = RewriteRuleHandler(
"Redirect",
[
("^/corpus/context.json$",
"https://raw.githubusercontent.com/opencitations/corpus/master/context.json",
True),
("^/ontology/?$",
"https://w3id.org/oc/ontology",
True),
("^/ontology.html$",
"https://w3id.org/oc/ontology.html",
True),
("^/ontology.json",
"https://w3id.org/oc/ontology.json",
True),
("^/ontology.rdf",
"https://w3id.org/oc/ontology.xml",
True),
("^/ontology.ttl",
"https://w3id.org/oc/ontology.ttl",
True),
("^/bcite",
"http://212.47.249.17",
True),
("^/index/coci/sparql",
"/index/sparql",
True),
("^/index/doci/sparql",
"/index/sparql",
True),
("^/index/poci/sparql",
"/index/sparql",
True),
("^/index/croci/sparql",
"/index/sparql",
True),
("^/index/coci/search",
"/index/search",
True),
("^/index/doci/search",
"/index/search",
True),
("^/index/poci/search",
"/index/search",
True),
("^/index/croci/search",
"/index/search",
True),
("^/contacts",
"/about",
True),
("^/donate",
"/membership",
True)
],
urls
)
# Set the web logger
web_logger = WebLogger("opencitations.net", c["log_dir"], [
"REMOTE_ADDR", # The IP address of the visitor
"HTTP_USER_AGENT", # The browser type of the visitor
"HTTP_REFERER", # The URL of the page that called your program
"HTTP_HOST", # The hostname of the page being attempted
"REQUEST_URI", # The interpreted pathname of the requested document
# or CGI (relative to the document root)
"HTTP_AUTHORIZATION", # Access token
],
# comment this line only for test purposes
{"REMOTE_ADDR": ["130.136.130.1", "130.136.2.47", "127.0.0.1"]}
)
meta_api_manager = APIManager(c["api_meta"])
meta_doc_manager = HTMLDocumentationHandler(meta_api_manager)
doci_api_manager = APIManager(c["api_doci"])
doci_doc_manager = HTMLDocumentationHandler(doci_api_manager)
poci_api_manager = APIManager(c["api_poci"])
poci_doc_manager = HTMLDocumentationHandler(poci_api_manager)
coci_api_manager = APIManager(c["api_coci"])
coci_doc_manager = HTMLDocumentationHandler(coci_api_manager)
#coci_api_manager_v2 = APIManager(c["api_coci_v2"])
#coci_doc_manager_v2 = HTMLDocumentationHandler(coci_api_manager_v2)
croci_api_manager = APIManager(c["api_croci"])
croci_doc_manager = HTMLDocumentationHandler(croci_api_manager)
index_api_manager = APIManager(c["api_index"])
index_doc_manager = HTMLDocumentationHandler(index_api_manager)
index_api_manager_v2 = APIManager(c["api_index_v2"])
index_doc_manager_v2 = HTMLDocumentationHandler(index_api_manager_v2)
occ_api_manager = APIManager(c["api_occ"])
occ_doc_manager = HTMLDocumentationHandler(occ_api_manager)
wikidata_api_manager = APIManager(c["api_wikidata"])
wikidata_doc_manager = HTMLDocumentationHandler(wikidata_api_manager)
ccc_api_manager = APIManager(c["api_ccc"])
ccc_doc_manager = HTMLDocumentationHandler(ccc_api_manager)
rconn = Redis(host=c_auth["redis"]["host"],
port=c_auth["redis"]["port"], db=c_auth["redis"]["db"])
app = web.application(rewrite.get_urls(), globals())
session = web.session.Session(app, web.session.DiskStore(
"sessions"), initializer={"csrf": None})
def refreshCSRF():
session.csrf = sha1(urandom(64)).hexdigest()
def validateAccessToken():
auth_code = web.ctx.env.get('HTTP_AUTHORIZATION')
if not auth_code is None:
val = rconn.get(auth_code)
if val is None or val != b'1':
raise web.HTTPError(
"403 ",
{
"Content-Type": "text/plain"
},
"The access token provided is not valid."
)
def sendEmail(recipient, subject, body):
sender = c_smtp["email"]
if not isinstance(recipient, list):
recipient = [recipient]
message = MIMEMultipart('alternative')
message['Subject'] = subject
message['From'] = sender
message['To'] = ", ".join(recipient)
html_body = MIMEText(body, 'html')
message.attach(html_body)
#Define SMTP
server = smtplib.SMTP(c_smtp["address"], c_smtp["port"])
server.starttls()
server.login(sender, c_smtp["password"])
#Send email
server.sendmail(sender, recipient, message.as_string())
server.close()
class AuthCodeConfirm:
def GET(self, token):
check = rconn.get(token)
if check is None or check != b'2':
auth_code = None
else:
rconn.delete(token)
rconn.set(token, 1)
auth_code = token
return render.accesstokenconfirm(pages, active, auth_code, c_auth["messages"]["accesstokenconfirm"])
class AuthCode:
def __init__(self):
self.__rgx = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
def GET(self):
refreshCSRF()
web_logger.mes()
return render.accesstoken(pages, active, c_captcha["PUBKEY"], None, session.csrf, c_auth["messages"]["accesstoken"])
def POST(self):
data = web.input()
session_csrf = session.csrf
refreshCSRF()
# Validate email
email = data.tokenEmail
if not re.fullmatch(self.__rgx, email):
return render.accesstoken(pages, active, c_captcha["PUBKEY"], c_auth["messages"]["accesstoken"]["invalid_email"], session.csrf, c_auth["messages"]["accesstoken"])
# Validate recaptcha
recaptcha = data['g-recaptcha-response']
params = urlparse.urlencode({
'secret': c_captcha["PRVKEY"],
'response': recaptcha,
})
data_res = urllib.urlopen(
'https://www.google.com/recaptcha/api/siteverify', params.encode('utf-8')).read()
result = json.loads(data_res.decode('utf-8'))
success = result.get('success', None)
if not success == True:
return render.accesstoken(pages, active, c_captcha["PUBKEY"], c_auth["messages"]["accesstoken"]["invalid_captcha"], session.csrf, c_auth["messages"]["accesstoken"])
# CSFR Attack
csrf = data.csrf
#if csrf != session_csrf:
# return render.accesstoken(pages, active, c_captcha["PUBKEY"], c_auth["messages"]["accesstoken"]["invalid_form"], session.csrf, c_auth["messages"]["accesstoken"])
# Generate temporary token
token = str(uuid.uuid4())
while not rconn.get(token) is None:
token = str(uuid.uuid4())
rconn.set(token, 2, ex=3600)
email_msg = c_auth["messages"]["email"]
email_link = c["oc_base_url"]+"/accesstoken/" + token
sendEmail(email, email_msg["title"], """\
<html>
<head></head>
<body>
<div style="text-align: center">
<img width=100 src='%s'>
<h2><font color="#AA53FD">Access-Token</font> <font color="#3C40E5">Request</font></h2>
<br>
%s<br>
<h3>%s <a href='%s' style="background-color:#AA53FD;color:white;padding:8px;border-radius:3px;">%s</a></h3>
%s<br><br>
</div>
%s
<br><br>
%s %s</body>
</html>
""" % (
"https://raw.githubusercontent.com/opencitations/logo/master/logo-transparent.png",
email_msg["description"],
email_msg["token"],
email_link,
email_msg["token_button"],
email_msg["ignore"],
email_msg["signature"],
email_msg["html_message"],
email_link
))
return render.accesstokensuccess(pages, active, c_auth["messages"]["accesstokensuccess"])
class RawGit:
def GET(self, u):
web_logger.mes()
raise web.seeother(
"http://rawgit.com/essepuntato/opencitations/master" + u)
class Robots:
def GET(self):
web.header('Access-Control-Allow-Origin', '*')
web.header('Access-Control-Allow-Credentials', 'true')
web.header('Content-Type', "text/plain")
return "user-agent: %s\n" \
"disallow: /corpus/\ndisallow: /virtual/\ndisallow: /index/coci/\n\n" \
"user-agent: %s\n" \
"disallow: /" % ("\nuser-agent: ".join(
c["robots"]), "\nuser-agent: ".join(c["robots-all"]))
class Redirect:
def GET(self, u):
web_logger.mes()
raise web.seeother(rewrite.rewrite(u))
class WorkInProgress:
def GET(self, active):
web_logger.mes()
return render.wip(pages, active)
class Querying:
def GET(self, active):
web_logger.mes()
return render.querying(pages, active)
class Tools:
def GET(self, active):
web_logger.mes()
return render.tools(pages, active)
class Membership:
def GET(self, active):
web_logger.mes()
return render.membership(pages, active)
class Home:
def GET(self):
web_logger.mes()
return render.home(pages, "")
class Api:
def OPTIONS(self, dataset, call):
# remember to remove the slash at the end
org_ref = web.ctx.env.get('HTTP_REFERER')
if org_ref is not None:
org_ref = org_ref[:-1]
else:
org_ref = "*"
web.header('Access-Control-Allow-Origin', org_ref)
web.header('Access-Control-Allow-Credentials', 'true')
web.header('Access-Control-Allow-Methods', '*')
web.header('Access-Control-Allow-Headers', 'Authorization')
def GET(self, dataset, call):
validateAccessToken()
man = None
if dataset == "":
dataset = "index"
#man = occ_api_manager
#doc = occ_doc_manager
if dataset == "coci":
man = coci_api_manager
doc = coci_doc_manager
#if "v2" in call:
# man = coci_api_manager_v2
# doc = coci_doc_manager_v2
elif dataset == "doci":
man = doci_api_manager
doc = doci_doc_manager
elif dataset == "poci":
man = poci_api_manager
doc = poci_doc_manager
elif dataset == "croci":
man = croci_api_manager
doc = croci_doc_manager
elif dataset == "index":
man = index_api_manager
doc = index_doc_manager
if "v2" in call:
man = index_api_manager_v2
doc = index_doc_manager_v2
elif dataset == "wikidata":
man = wikidata_api_manager
doc = wikidata_doc_manager
elif dataset == "ccc":
man = ccc_api_manager
doc = ccc_doc_manager
elif dataset == "meta":
man = meta_api_manager
doc = meta_doc_manager
if man is None:
raise web.notfound()
else:
if re.match("^/api/v[1-9][0-9]*/?$", call):
# remember to remove the slash at the end
org_ref = web.ctx.env.get('HTTP_REFERER')
if org_ref is not None:
org_ref = org_ref[:-1]
else:
org_ref = "*"
web.header('Access-Control-Allow-Origin', org_ref)
web.header('Access-Control-Allow-Credentials', 'true')
web.header('Content-Type', "text/html")
web.header('Access-Control-Allow-Methods', '*')
web.header('Access-Control-Allow-Headers', 'Authorization')
web_logger.mes()
return doc.get_documentation()[1]
else:
content_type = web.ctx.env.get('HTTP_ACCEPT')
if content_type is not None and "text/csv" in content_type:
content_type = "text/csv"
else:
content_type = "application/json"
operation_url = call + unquote(web.ctx.query)
op = man.get_op(operation_url)
if type(op) is Operation:
status_code, res, c_type = op.exec(
content_type=content_type)
if status_code == 200:
# remember to remove the slash at the end
org_ref = web.ctx.env.get('HTTP_REFERER')
if org_ref is not None:
org_ref = org_ref[:-1]
else:
org_ref = "*"
web.header('Access-Control-Allow-Origin', org_ref)
web.header('Access-Control-Allow-Credentials', 'true')
web.header('Content-Type', c_type)
web.header('Access-Control-Allow-Methods', '*')
web.header('Access-Control-Allow-Headers',
'Authorization')
web_logger.mes()
return res
else:
try:
with StringIO(res) as f:
if content_type == "text/csv":
mes = next(csv.reader(f))[0]
else:
mes = json.dumps(
next(csv.DictReader(f)), ensure_ascii=False)
raise web.HTTPError(
str(status_code)+" ", {"Content-Type": content_type}, mes)
except:
raise web.HTTPError(
str(status_code)+" ", {"Content-Type": content_type}, str(res))
else:
raise web.HTTPError(
"404 ", {"Content-Type": content_type}, "No API operation found at URL '%s'" % call)
class Policy:
def GET(self, active):
web_logger.mes()
return render.policy(pages, active)
class About:
def GET(self, active):
web_logger.mes()
return render.about(pages, active)
class Corpus:
def GET(self):
web_logger.mes()
cur_date = "January 1, 1970"
cur_tot = "0"
cur_cit = "0"
cur_cited = "0"
if path.exists(c["statistics"]):
with open(c["statistics"]) as f:
lastrow = None
for lastrow in csv.reader(f):
pass
cur_date = datetime.strptime(
lastrow[0], "%Y-%m-%dT%H:%M:%S").strftime("%B %d, %Y")
cur_tot = lastrow[5]
cur_cit = lastrow[2]
cur_cited = lastrow[6]
return render.corpus(pages, active["corpus"], cur_date, cur_tot, cur_cit, cur_cited)
class OCI:
def GET(self, oci):
data = web.input()
if "oci" in data:
clean_oci = re.sub("\s+", "", re.sub("^oci:", "",
data.oci.strip(), flags=re.IGNORECASE))
cur_format = ".rdf"
if "format" in data:
cur_format = "." + data.format.strip().lower()
raise web.seeother(c["oc_base_url"]
+ "/oci/" + clean_oci + cur_format)
elif oci is None or oci.strip() == "":
web_logger.mes()
return render.oci(pages, active["oci"])
else:
web_logger.mes()
clean_oci, ex = re.findall(
"^([^\.]+)(\.[a-z]+)?$", oci.strip().lower())[0]
exs = (".csv", ".json", ".scholix",
".jsonld", ".ttl", ".nt", ".xml")
if ex in exs:
cur_format = ex[1:]
om_conf = c["ved_conf"]
om = OCIManager(
"oci:" + clean_oci[1:], om_conf["lookup"], om_conf["oci_conf"])
cit = om.get_citation_data(cur_format)
if cit:
if cur_format == "csv":
ct_header = "text/csv"
elif cur_format == "jsonld":
ct_header = "application/ld+json"
elif cur_format == "ttl":
ct_header = "text/turtle"
elif cur_format == "nt":
ct_header = "application/n-triples"
elif cur_format == "xml":
ct_header = "application/rdf+xml"
else:
ct_header = "application/json"
web.header('Access-Control-Allow-Origin', '*')
web.header('Access-Control-Allow-Credentials', 'true')
web.header('Content-Type', ct_header)
return cit
else:
raise web.seeother(c["oc_base_url"]
+ c["virtual_local_url"] + "ci" + clean_oci)
class InTRePID:
def GET(self, intrepid):
data = web.input()
if "intrepid" in data:
clean_intrepid = re.sub(
"\s+", "", re.sub("^intrepid:", "", data.intrepid.strip(), flags=re.IGNORECASE))
om_conf = c["ved_conf"]
im = InTRePIDManager("intrepid:" + clean_intrepid,
om_conf["lookup"], om_conf["intrepid_conf"])
rp = im.execute_query()
raise web.seeother(c["oc_base_url"]
+ c["ccc_local_url"] + rp.split("/ccc/")[1])
elif intrepid is None or intrepid.strip() == "":
web_logger.mes()
return render.intrepid(pages, active["intrepid"])
else:
web_logger.mes()
clean_intrepid, ex = re.findall(
"^([^\.]+)(\.[a-z]+)?$", intrepid.strip().lower())[0]
exs = (".jsonld", ".ttl", ".nt", ".xml")
if ex in exs:
cur_format = ex[1:]
om_conf = c["ved_conf"]
im = InTRePIDManager(
"intrepid:" + clean_intrepid, om_conf["lookup"], om_conf["intrepid_conf"])
rp = im.get_rp_data(cur_format)
if rp:
if cur_format == "jsonld":
ct_header = "application/ld+json"
elif cur_format == "ttl":
ct_header = "text/turtle"
elif cur_format == "nt":
ct_header = "application/n-triples"
else:
ct_header = "application/rdf+xml"
web.header('Access-Control-Allow-Origin', '*')
web.header('Access-Control-Allow-Credentials', 'true')
web.header('Content-Type', ct_header)
return rp
else:
om_conf = c["ved_conf"]
im = InTRePIDManager(
"intrepid:" + clean_intrepid[1:], om_conf["lookup"], om_conf["intrepid_conf"])
rp = im.execute_query()
raise web.seeother(c["oc_base_url"]
+ c["ccc_local_url"] + rp.split("/ccc/")[1])
class Index:
def GET(self):
web_logger.mes()
return render.index(pages, active["index"])
class CCC:
def GET(self):
web_logger.mes()
return render.ccc(pages, active["ccc"])
class Meta:
def GET(self):
web_logger.mes()
return render.meta(pages, active["meta"])
class Coci:
def GET(self):
web_logger.mes()
return render.coci(pages, active["coci"])
class Doci:
def GET(self):
web_logger.mes()
return render.doci(pages, active["doci"])
class Poci:
def GET(self):
web_logger.mes()
return render.poci(pages, active["poci"])
class Croci:
def GET(self):
web_logger.mes()
return render.croci(pages, active["croci"])
class Download:
def GET(self, active):
web_logger.mes()
return render.download(pages, active)
class DownloadLegacy:
def GET(self, active):
web_logger.mes()
return render.download_legacy(pages, active + " (legacy)")
class Search:
def __init__(self, render_page):
self.render_page = render_page
def GET(self):
web_logger.mes()
query_string = web.ctx.env.get("QUERY_STRING")
return self.render_page(pages, active["search"], query_string)
class SearchIndex(Search):
def __init__(self):
Search.__init__(self, render.search_index)
class SearchOC(Search):
def __init__(self):
Search.__init__(self, render.search)
class SearchCCC(Search):
def __init__(self):
Search.__init__(self, render.search_ccc)
class Browser:
def __init__(self, render_page):
self.render_page = render_page
def GET(self, res_id):
web_logger.mes()
return self.render_page(res_id)
class BrowserIndex(Browser):
def __init__(self):
Browser.__init__(self, render.browser_index)
class BrowserOC(Browser):
def __init__(self):
Browser.__init__(self, render.browser)
class BrowserCCC(Browser):
def __init__(self):
Browser.__init__(self, render.browser_ccc)
class Model:
def GET(self, active):
web_logger.mes()
return render.model(pages, active)
class Datasets:
def GET(self, active):
web_logger.mes()
return render.datasets(pages, active)
class Publications:
def GET(self, active):
web_logger.mes()
return render.publications(pages, active)
class Licenses:
def GET(self, active):
web_logger.mes()
return render.licenses(pages, active)
class Contacts:
def GET(self, active):
web_logger.mes()
return render.contacts(pages, active)
class Sparql:
def __init__(self, sparql_endpoint, sparql_endpoint_title, yasqe_sparql_endpoint):
self.sparql_endpoint = sparql_endpoint
self.sparql_endpoint_title = sparql_endpoint_title
self.yasqe_sparql_endpoint = yasqe_sparql_endpoint
def GET(self):
content_type = web.ctx.env.get('CONTENT_TYPE')
return self.__run_query_string(active["sparql"], web.ctx.env.get("QUERY_STRING"), content_type)
def POST(self):
content_type = web.ctx.env.get('CONTENT_TYPE')
cur_data = web.data().decode("utf-8")
if "application/x-www-form-urlencoded" in content_type:
return self.__run_query_string(active["sparql"], cur_data, True, content_type)
elif "application/sparql-query" in content_type:
return self.__contact_tp(cur_data, True, content_type)
else:
raise web.redirect("/sparql")
def __contact_tp(self, data, is_post, content_type):
accept = web.ctx.env.get('HTTP_ACCEPT')
if accept is None or accept == "*/*" or accept == "":
accept = "application/sparql-results+xml"
if is_post:
req = requests.post(self.sparql_endpoint, data=data,
headers={'content-type': content_type, "accept": accept})
else:
req = requests.get("%s?%s" % (self.sparql_endpoint, data),
headers={'content-type': content_type, "accept": accept})
if req.status_code == 200:
web.header('Access-Control-Allow-Origin', '*')
web.header('Access-Control-Allow-Credentials', 'true')
web.header('Content-Type', req.headers["content-type"])
web_logger.mes()
req.encoding = "utf-8"
return req.text
else:
raise web.HTTPError(
str(req.status_code)+" ", {"Content-Type": req.headers["content-type"]}, req.text)
def __run_query_string(self, active, query_string, is_post=False,
content_type="application/x-www-form-urlencoded"):
parsed_query = urlparse.parse_qs(query_string)
if query_string is None or query_string.strip() == "":
web_logger.mes()
return render.sparql(pages, active, self.sparql_endpoint_title, self.yasqe_sparql_endpoint)
if re.search("updates?", query_string, re.IGNORECASE) is None:
if "query" in parsed_query:
return self.__contact_tp(query_string, is_post, content_type)
else:
raise web.redirect("/sparql")
else:
raise web.HTTPError(
"403 ", {"Content-Type": "text/plain"}, "SPARQL Update queries are not permitted.")
class SparqlOC(Sparql):
def __init__(self):
Sparql.__init__(self, c["sparql_endpoint"],
"Corpus", c["oc_base_url"]+"/sparql")
class SparqlIndex(Sparql):
def __init__(self):
Sparql.__init__(self, c["sparql_endpoint_index"],
"Index", c["oc_base_url"]+"/index/sparql")
class SparqlMeta(Sparql):
def __init__(self):
Sparql.__init__(self, c["sparql_endpoint_meta"],
"Meta", c["oc_base_url"]+"/meta/sparql")
class SparqlCCC(Sparql):
def __init__(self):
Sparql.__init__(self, c["sparql_endpoint_ccc"],
"CCC", c["oc_base_url"]+"/ccc/sparql")
class Virtual:
def GET(self, file_path=None):
ldd = LinkedDataDirector(
c["index_base_path"], c["html"], c["oc_base_url"],
c["json_context_path"], c["corpus_local_url"],
label_conf=c["label_conf"], tmp_dir=c["tmp_dir"],
dir_split_number=int(c["dir_split_number"]),
file_split_number=int(c["file_split_number"]),
default_dir=c["default_dir"])
ved = VirtualEntityDirector(ldd, c["virtual_local_url"], c["ved_conf"])
cur_page = ved.redirect(file_path)
if cur_page is None:
raise web.notfound()
else:
web_logger.mes()
return cur_page
class ContentNegotiation:
def __init__(self, base_url, local_url, context_path=None, from_triplestore=None, label_func=None):
self.base_url = base_url
self.local_url = local_url
self.from_triplestore = from_triplestore
self.label_func = label_func
self.context_path = context_path
def GET(self, file_path=None):
ldd = LinkedDataDirector(
c["index_base_path"], c["html"], self.base_url,
self.context_path, self.local_url,
label_conf=c["label_conf"], tmp_dir=c["tmp_dir"],
dir_split_number=int(c["dir_split_number"]),
file_split_number=int(c["file_split_number"]),
default_dir=c["default_dir"], from_triplestore=self.from_triplestore,
label_func=self.label_func)
cur_page = ldd.redirect(file_path)
if cur_page is None:
raise web.notfound()
else:
web_logger.mes()
return cur_page
class IndexContentNegotiation(ContentNegotiation):
def __init__(self):
ContentNegotiation.__init__(self, c["index_base_url"], c["index_local_url"],
context_path=c["ocdm_json_context_path"],
from_triplestore=c["sparql_endpoint_index"],
label_func=lambda u: "oci:%s" % re.findall(
"^.+/ci/(.+)$", u)[0]
if "/ci/" in u else "provenance agent 1" if "/pa/1" in u
else "INDEX")
class CorpusContentNegotiation(ContentNegotiation):
def __init__(self):
ContentNegotiation.__init__(self, c["oc_base_url"], c["corpus_local_url"],
context_path=c["json_context_path"],
from_triplestore=c["sparql_endpoint"])
class CCCContentNegotiation(ContentNegotiation):
def __init__(self):
ContentNegotiation.__init__(self, c["oc_base_url"], c["ccc_local_url"],
context_path=c["ocdm_json_context_path"],
from_triplestore=c["sparql_endpoint_ccc"],
label_func=lambda u: "%s %s" % re.findall("^.+/ccc/(..)/070(.+)$", u)[0])
class MetaContentNegotiation(ContentNegotiation):
def __init__(self):
ContentNegotiation.__init__(self, c["index_base_url"], c["meta_local_url"],
context_path=c["ocdm_json_context_path"],