-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiiif.rb
1440 lines (1358 loc) · 57 KB
/
iiif.rb
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
require 'net/https'
require 'open-uri'
require 'nokogiri'
require 'json'
require 'typhoeus'
# require 'fastimage'
# @uuid = ARGV[0]
if ARGV.length == 1
@link = ARGV[0]
@library = @link.split("/")[3]
object = @link.split("/")[5]
@uuid = object.split("?")[0]
else
@library = ARGV[0]
@uuid = ARGV[1]
full = ARGV[2]
if (full == 'full')
@full = true
else
@full = false
end
end
@registrkrameriu = "https://api.registr.digitalniknihovna.cz/api/libraries"
@url_manifest = "https://iiif.digitalniknihovna.cz"
@mods
@canvasIndex = 0
@languages = {"cze" => "čeština", "ger" => "němčina", "eng" => "angličtina", "lat" => "latina", "fre" => "francouzština",
"rus" => "ruština", "pol" => "polština", "slv" => "slovinština", "slo" => "slovenština",
"ita" => "italština", "dut" => "nizozemština", "hun" => "maďarština", "ukr" => "ukrajinština",
"rum" => "rumunština", "sla" => "slovanské jazyky", "grc" => "starořečtina", "spa" => "španělština", "scr" => "chorvatština",
"und" => "neurčený jazyk", "zxx" => "žádný lingvistický obsah", "---" => "žádný lingvistický obsah", }
@mapping = {
"k5" => {
"solr" => "/search/api/v5.0/search?",
"items" => "/search/api/v5.0/item/",
"mods_url" => "/streams/BIBLIO_MODS",
"thumb" => "/thumb",
"pid" => "PID",
"fedora_model" => "fedora.model",
"root_title_url" => "root_title",
"details" => "details",
"title" => "title",
"parent_pid" => "parent_pid",
"rels_ext_index" => "rels_ext_index",
"alto_url" => "/streams/ALTO",
"root_pid" => "root_pid",
"mp3_url" => "/streams/MP3",
"pdf" => "img_full_mime"
},
"k7" => {
"solr" => "/search/api/client/v7.0/search?",
"items" => "/search/api/client/v7.0/items/",
"mods_url" => "/metadata/mods",
"thumb" => "/image/thumb",
"pid" => "pid",
"fedora_model" => "model",
"root_title_url" => "root.title",
"title" => "title.search",
"parent_pid" => "own_parent.pid",
"rels_ext_index" => "rels_ext_index.sort",
"details" => "date.str,part.number.str,part.name,issue.type.code,page.number,page.type",
"alto_url" => "/ocr/alto",
"root_pid" => "root.pid",
"mp3_url" => "/audio/mp3",
"pdf" => "ds.img_full.mime"
}
}
def get_xml(url)
uri = URI(url)
# uri = URI(URI.encode(url))
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_PEER
https.open_timeout = 100
https.read_timeout = 100
request = Net::HTTP::Get.new(uri)
request["Accept"] = 'application/xml'
response = https.request(request).read_body
end
def get_json(url)
uri = URI(url) #uri = URI(URI.encode(url))
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
request = Net::HTTP::Get.new(uri)
request.add_field "Content-Type", "application/json; charset=utf-8"
request.add_field "Accept", "application/json"
response = https.request(request)
result = JSON.parse(response.read_body)
return result
end
def control_404(url)
response = Net::HTTP.get_response(URI(url))
error = response.is_a?(Net::HTTPNotFound)
code = response.code
if code.to_i > 400
result = true
end
return !result
end
def create_provider(library)
object = get_json(@registrkrameriu)
# K7 TEST
if library == "mzkk7"
id = "#{@registrkrameriu}/#{@library}"
label_cz = object.find {|h1| h1['code'] == "mzk"}["name"]
label_en = object.find {|h1| h1['code'] == "mzk"}["name_en"]
homepage = "https://k7-test.mzk.cz"
version = "7"
logo = object.find {|h1| h1['code'] == "mzk"}["logo"]
provider = [{"id" => id, "type" => "Agent", "label" => {"cz" => [label_cz]}, "homepage" => [{ "id" => homepage, "type" => "Text", "label" => {"cz" => [label_cz]}, "format" => "text/html"}], "logo" => [{"id" => logo, "type" => "Image", "format" => "image/png"}]}]
else
id = "#{@registrkrameriu}/#{@library}"
label_cz = object.find {|h1| h1['code'] == library}["name"]
label_en = object.find {|h1| h1['code'] == library}["name_en"]
homepage = object.find {|h1| h1['code'] == library}["url"]
version = object.find {|h1| h1['code'] == library}["version"]
logo = object.find {|h1| h1['code'] == library}["logo"]
provider = [{"id" => id, "type" => "Agent", "label" => {"cz" => [label_cz]}, "homepage" => [{ "id" => homepage, "type" => "Text", "label" => {"cz" => [label_cz]}, "format" => "text/html"}], "logo" => [{"id" => logo, "type" => "Image", "format" => "image/png"}]}]
end
return [provider, homepage, version]
end
def find_document_properties(pid)
document = {}
uuid = "#{@uuid}".to_json
@solr_url = @mapping["#{@v}"]["solr"]
@pid = @mapping["#{@v}"]["pid"]
@parent_pid = @mapping["#{@v}"]["parent_pid"]
@fedora_model = @mapping["#{@v}"]["fedora_model"]
@root_title_url = @mapping["#{@v}"]["root_title_url"]
@details = @mapping["#{@v}"]["details"]
@title_url = @mapping["#{@v}"]["title"]
@rels_ext_index = @mapping["#{@v}"]["rels_ext_index"]
@items = @mapping["#{@v}"]["items"]
@alto_url = @mapping["#{@v}"]["alto_url"]
@mods_url = @mapping["#{@v}"]["mods_url"]
@thumb_url = @mapping["#{@v}"]["thumb"]
@mp3_url = @mapping["#{@v}"]["mp3_url"]
@pdf = @mapping["#{@v}"]["pdf"]
solr_request = "#{@kramerius}#{@solr_url}fl=#{@fedora_model},#{@root_title_url},#{@details},#{@title_url},#{@pdf}&q=#{@pid}:#{uuid}&rows=1500&start=0"
object = get_json(solr_request)
response_body = object["response"]["docs"][0]
if !response_body.nil?
document["model"] = response_body["#{@fedora_model}"]
document["root_title"] = response_body["#{@root_title_url}"].split(":")[0]
document["title"] = response_body["#{@title_url}"]
document["pdf"] = response_body["#{@pdf}"]
if @v == "k5"
if document["model"].to_s == "periodicalvolume"
if !response_body["details"][0].split("##")[0].nil?
document["date"] = response_body["details"][0].split("##")[0].strip.sub(" ", "")
end
if !response_body["details"][0].split("##")[1].nil?
document["number"] = response_body["details"][0].split("##")[1].strip.sub(" ", "")
end
elsif document["model"].to_s == "periodicalitem"
if !response_body["details"][0].split("##")[2].nil?
document["date"] = response_body["details"][0].split("##")[2].strip.sub(" ", "")
end
if !response_body["details"][0].split("##")[3].nil?
document["number"] = response_body["details"][0].split("##")[3].strip.sub(" ", "")
end
elsif document["model"].to_s == "monograph"
object = get_json("#{@kramerius}/search/api/v5.0/search?fl=PID,details,rels_ext_index,fedora.model&q=parent_pid:#{uuid} AND fedora.model:monographunit&rows=1500&start=0")
response_body = object["response"]["docs"]
if !response_body[0].nil?
document["model"] = 'monographcollection'
end
end
if document["pdf"] == "application/pdf"
document["model"] = 'pdf'
end
elsif @v == "k7"
if document["model"].to_s == "periodicalvolume" || document["model"].to_s == "periodicalitem"
if !response_body["date.str"].nil?
document["date"] = response_body["date.str"]
end
if !response_body["part.number.str"].nil?
document["number"] = response_body["part.number.str"]
end
if !response_body["issue.type.code"].nil?
document["issue_type"] = response_body["issue.type.code"]
end
elsif document["model"].to_s == "monograph"
object = get_json("#{@kramerius}#{@solr_url}fl=#{@fedora_model}&q=own_parent.pid:#{uuid} AND #{@fedora_model}:monographunit&rows=1500&start=0")
response_body = object["response"]["docs"]
if !response_body[0].nil?
document["model"] = 'monographcollection'
end
end
if document["pdf"] == "application/pdf"
document["model"] = 'pdf'
end
end
end
return document
end
def mods_extractor
errors = []
mods = {}
@mods_url = "#{@kramerius}#{@items}#{@uuid}#{@mods_url}"
xmldoc = Nokogiri::XML get_xml(@mods_url)
if xmldoc.nil?
errors << uuid
continue
end
xmldoc.remove_namespaces!
# sysno a sigla
xmldoc.xpath("modsCollection/mods/recordInfo/recordIdentifier").each do |sys|
sysno = "#{sys.text}"
sigla = "MZK01" #TODO
if sysno.length == 9
mods["sysno"] = sysno
end
mods["sigla"] = sigla
end
# uuid
xmldoc.xpath("modsCollection/mods/identifier").each do |id|
if id.at("@type").to_s == "uuid"
uuid = id.text
if uuid.length == 36
uuid = "uuid:#{id.text}"
else
uuid = "#{id.text}"
end
uuid2 = uuid.to_s
mods["uuid"] = uuid2
end
end
# title
xmldoc.xpath("modsCollection/mods/titleInfo[1]").each do |titleInfo|
if !titleInfo.at("title").nil?
title = titleInfo.at("title").text
if !titleInfo.at("nonSort").nil?
nonsort = titleInfo.at("nonSort").text
mods["title"] = "#{nonsort}#{title}"
else
mods["title"] = title
end
end
if !titleInfo.at("subTitle").nil?
subtitle = titleInfo.at("subTitle").text
mods["subtitle"] = subtitle
end
if !titleInfo.at("partNumber").nil?
partNumber = titleInfo.at("partNumber").text
mods["partNumber"] = partNumber
end
if !titleInfo.at("partName").nil?
partName = titleInfo.at("partName").text
mods["partName"] = partName
end
end
# authors
if xmldoc.at("modsCollection/mods/name")
authors = []
xmldoc.xpath("modsCollection/mods/name").each do |name|
author = ""
if name.at("//namePart/@type").to_s == "family"
family = ""
given = ""
termOfAddress = ""
date = ""
name.xpath("namePart").each do |namePart|
if namePart.at("@type").to_s == "family"
family = namePart.text
end
if namePart.at("@type").to_s == "given"
given = ", #{namePart.text}"
end
if namePart.at("@type").to_s == "termsOfAddress"
termOfAddress = ", #{namePart.text}"
end
if namePart.at("@type").to_s == "date"
date = ", #{namePart.text}"
end
end
author = "#{family}#{given}#{termOfAddress}#{date}"
end
if name.at("//namePart[not(@type)]")
name2 = ""
termOfAddress = ""
date = ""
name.xpath("namePart").each do |namePart|
if namePart.at("@type").to_s == "termsOfAddress"
termOfAddress = ", #{namePart.text}"
elsif namePart.at("@type").to_s == "date"
date = ", #{namePart.text}"
# elsif namePart.at("[not(@type)]")
else
name2 = namePart.text
end
end
author = "#{name2}#{termOfAddress}#{date}"
end
authors.push(author)
mods["authors"] = authors
end
end
# nakladatelske udaje
xmldoc.xpath("modsCollection/mods/originInfo").each do |originInfo|
published = ""
# place
originInfo.xpath("place").each do |place|
if place.xpath("placeTerm/@authority").to_s != "marccountry"
places = place.at("placeTerm").text
published = "#{published}#{places}"
# else
# places = place.at("placeTerm").text
# published = "#{published}#{places}"
end
end
# publisher
if !originInfo.xpath("publisher").nil?
originInfo.xpath("publisher").each do |pub|
publisher = pub.text
published = "#{published}: #{publisher}"
end
end
# date
if !originInfo.xpath("dateIssued").nil?
dates = {}
originInfo.xpath("dateIssued").each do |dat|
if dat.at("@point").to_s == "start"
dates["date_start"] = dat.text
elsif dat.at("@point").to_s == "end"
dates["date_end"] = dat.text
else
dates["date"] = dat.text
mods["date"] = dat.text
end
end
if published.length > 0
published = "#{published}, #{dates["date"]}"
else
published = "#{dates["date"]}"
end
end
mods["published"] = published
end
# jazyk
langs = []
xmldoc.xpath("modsCollection/mods/language/languageTerm").each do |lang|
if !@languages[lang.text].nil?
langs.push(@languages[lang.text])
else
langs.push(lang.text)
end
mods["languages"] = langs
end
# coordinates
xmldoc.xpath("modsCollection/mods/subject/cartographics/coordinates").each do |c|
mods["coordinates"] = c.text
end
# monograph volume part
xmldoc.xpath("modsCollection/mods/part").each do |part|
if part.xpath("@type").to_s == "Volume"
if !part.xpath("detail/number").nil?
part.xpath("detail/number").each do |detail|
partNumber = detail.text
mods["partNumber"] = partNumber
end
end
end
end
return mods
end
def create_label(uuid)
label = {}
if @document_model == "periodicalvolume" || @document_model == "periodicalitem"
partDate = @mods["date"]
label = {"none" => ["#{@root_title} (#{@document["date"]})"]}
elsif @document_model == "soundunit"
label = {"none" => ["#{@root_title} (#{@mods["partName"]})"]}
else label = {"none" => [@mods["title"]]}
end
return label
end
def create_metadata
metadata = []
if !@mods["title"].nil?
title = {"label" => {"cz" => ["Název"]}, "value" => {"none" => [@mods["title"]]}}
metadata.push(title)
end
if !@mods["subtitle"].nil?
subtitle = {"label" => {"cz" => ["Podnázev"]}, "value" => {"none" => [@mods["subtitle"]]}}
metadata.push(subtitle)
end
if !@type_of_resource.nil?
type_of_resource = {"label" => {"cz" => ["Typ dokumentu"]}, "value" => {"cz" => [@type_of_resource]}}
metadata.push(type_of_resource)
end
# if !@mods["partNumber"].nil?
# title = {"label" => {"cz" => ["Číslo části"]}, "value" => {"none" => [@mods["partNumber"]]}}
# metadata.push(title)
# end
if !@mods["authors"].nil?
author = {"label" => {"cz" => ["Autor"]}, "value" => {"none" => @mods["authors"]}}
metadata.push(author)
end
if @document_model == "periodicalvolume" || @document_model == "periodicalitem"
number = {"label" => {"cz" => ["Číslo části"]}, "value" => {"none" => [@document["number"]]}}
date = {"label" => {"cz" => ["Vydáno"]}, "value" => {"none" => [@document["date"]]}}
metadata.push(number)
metadata.push(date)
elsif @document_model == "soundunit" || @document_model == "monographunit"
if !@mods["partNumber"].nil?
number = {"label" => {"cz" => ["Číslo části"]}, "value" => {"none" => [@mods["partNumber"]]}}
metadata.push(number)
end
if !@mods["partName"].nil?
name = {"label" => {"cz" => ["Název části"]}, "value" => {"none" => [@mods["partName"]]}}
metadata.push(name)
end
elsif !@mods["published"].nil?
if @mods["published"].length > 0
published = {"label" => {"cz" => ["Nakladatelské údaje"]}, "value" => {"none" => [@mods["published"]]}}
metadata.push(published)
end
end
if !@mods["languages"].nil?
subtitle = {"label" => {"cz" => ["Jazyk"]}, "value" => {"none" => @mods["languages"]}}
metadata.push(subtitle)
end
# return JSON.pretty_generate(metadata)
return metadata
end
def create_homepage
uuid = @uuid
sysno = @mods["sysno"]
sigla = @mods["sigla"]
homepage = []
if !uuid.nil?
dk = {"id" => "https://www.digitalniknihovna.cz/#{@library}/uuid/#{uuid}", "type" => "Text", "label" => { "cz" => ["Odkaz do Digitální knihovny"]}}
homepage.push(dk)
end
if @library == "mzk"
if !sysno.nil?
vufind = {"id" => "https://vufind.mzk.cz/Record/#{sigla}-#{sysno}", "type" => "Text", "label" => { "cz" => ["Odkaz do Vufindu"]}}
homepage.push(vufind)
end
end
return homepage
end
def create_homepage_periodical_volume_issue
uuid = @uuid
homepage = []
if !uuid.nil?
dk = {"id" => "https://www.digitalniknihovna.cz/mzk/uuid/#{uuid}", "type" => "Text", "label" => { "cz" => ["Odkaz do Digitální knihovny"]}}
homepage.push(dk)
end
return homepage
end
def create_behavior
behavior = ["paged"]
return behavior
end
def create_thumbnail(uuid)
# K5 https://kramerius.mzk.cz/search/api/v5.0/item/uuid:bdc28360-3fc8-11e7-b3c8-005056825209/thumb
# K7 https://k7-test.mzk.cz/search/api/client/v7.0/items/uuid:4a6d8f19-13ab-4804-a26f-d877f1940970/image/thumb
thumbnail = {"id" => "#{@kramerius}" + @mapping["#{@v}"]["items"] + "#{@uuid}" + @mapping["#{@v}"]["thumb"],
"type" => "Image",
"format" => "image/jpeg",
# "service" => [{"@id" => "#{@kramerius}/search/iiif/#{@uuid}",
# "@type" => "ImageService2",
# "profile" => "http://iiif.io/api/image/2/level2.json"
# }]
}
return [thumbnail]
end
def create_navPlace
coordinates = parse_coordinates
type = coordinates[0]
features = {
"id" => "#{@url_manifest}/#{@library}/#{@uuid}/feature/1",
"type" => "Feature",
"properties" => {
"label" => {"en" => [@title]}
},
"geometry" => {
"type" => type,
"coordinates" => [coordinates[1]]
}
}
navPlace = {
"id" => "#{@url_manifest}/#{@library}/#{@uuid}/feature",
"type" => "FeatureCollection",
"features" => [features]
}
return navPlace
end
def parse_coordinates
input = @mods["coordinates"]
reg1 = "^\\((\\d{1,3})°(\\d{1,2})\´(\\d{1,2})\"\\s([v,z]{1})\.d\.--(\\d{1,3})°(\\d{1,2})\´(\\d{1,2})\"\\s([v,z]{1})\.d\.\/(\\d{1,3})°(\\d{1,2})\´(\\d{1,2})\"\\s([s,j]{1})\.š\.--(\\d{1,3})°(\\d{1,2})\´(\\d{1,2})\"\\s([s,j]{1})\.š\.\\)$"
reg2 = "^\\(([E,W]{1})\\s(\\d{1,3})°(\\d{1,2})'(\\d{1,2})\"--([E,W]{1})\\s(\\d{1,3})°(\\d{1,2})'(\\d{1,2})\"\/([S,N]{1})\\s(\\d{1,3})°(\\d{1,2})'(\\d{1,2})\"--([S,N]{1})\\s(\\d{1,3})°(\\d{1,2})'(\\d{1,2})\"\\)$"
output = input.match(reg1)
output2 = input.match(reg2)
# d1s1 d2s1
# d1s2 d2s2
if !output.nil?
d1x = output[4]
d1 = (output[1].to_f + (output[2].to_f/60) + (output[3].to_f/3600))
d2x = output[8]
d2 = (output[5].to_f + (output[6].to_f/60) + (output[7].to_f/3600))
s1x = output[12]
s1 = (output[9].to_f + (output[10].to_f/60) + (output[11].to_f/3600))
s2x = output[16]
s2 = (output[13].to_f + (output[14].to_f/60) + (output[15].to_f/3600))
if d1x == "z"
d1 = d1*-1
end
if d2x == "z"
d2 = d2*-1
end
if s1x == "j"
s1 = s1*-1
end
if s2x == "j"
s2 = s2*-1
end
if (d1 == d2 && s1 == s2)
coordinates = ["Point", [d1, s1]]
else
coordinates = ["Polygon", [[d1, s1], [d2, s1], [d1, s2], [d2, s2]]]
end
elsif !output2.nil?
d1x = output2[1]
d1 = (output2[2].to_f + (output2[3].to_f/60) + (output2[4].to_f/3600))
d2x = output2[5]
d2 = (output2[6].to_f + (output2[7].to_f/60) + (output2[8].to_f/3600))
s1x = output2[9]
s1 = (output2[10].to_f + (output2[11].to_f/60) + (output2[12].to_f/3600))
s2x = output2[13]
s2 = (output2[14].to_f + (output2[15].to_f/60) + (output2[16].to_f/3600))
if d1x == "W"
d1 = d1*-1
elsif d2x == "W"
d2 = d2*-1
elsif s1x == "S"
s1 = s1*-1
elsif s2x == "S"
s2 = s2*-1
end
if (d1 == d2 && s1 == s2)
coordinates = ["Point", [d1, s1]]
else
coordinates = ["Polygon", [[d1, s1], [d2, s1], [d1, s2], [d2, s2]]]
end
end
return coordinates
end
def create_list_of_pages(uuid)
# nactu a seradim si stranky
solr_request = "#{@kramerius}#{@solr_url}fl=#{@pid},#{@details},#{@rels_ext_index},#{@fedora_model},model_path&q=#{@parent_pid}:#{uuid} AND #{@fedora_model}:page&rows=1500&start=0"
object = get_json(solr_request)
response_body = object["response"]["docs"]
# specialni pripad, kdyz je v rels_ext_index vice hodnot (v K5)
if response_body[0][@rels_ext_index].is_a?(Array)
sorted_object = response_body.sort { |a, b| a[@rels_ext_index][ a["model_path"].index(a["model_path"].min_by{ |s| s.length })] <=> b[@rels_ext_index][b["model_path"].index(b["model_path"].min_by{ |s| s.length })]}
else
sorted_object = response_body.sort { |a, b| a[@rels_ext_index] <=> b[@rels_ext_index]}
end
pids = []
pages = []
# pro kazdou stranku:
sorted_object.each do |page|
page_properties = {}
uuid_page = page[@pid]
pids.push(uuid_page)
page_properties["pid"] = uuid_page
index = @canvasIndex
# K7 ALTO https://k7-test.mzk.cz/search/api/client/v7.0/items/uuid:52d55705-435f-11dd-b505-00145e5790ea/ocr/alto
# K7 THUMB https://k7-test.mzk.cz/search/api/client/v7.0/items/uuid:52d55705-435f-11dd-b505-00145e5790ea/image/thumb
# K7 IMAGE https://k7-test.mzk.cz/search/api/client/v7.0/items/uuid:52d55705-435f-11dd-b505-00145e5790ea/image/
# id pro vsechny urovne
page_properties["canvas_id"] = "#{@url_manifest}/#{@library}/#{@uuid}/canvases/#{index}"
page_properties["annotationPage_id"] = "#{@url_manifest}/#{@library}/#{@uuid}/canvases/#{index}/ap"
page_properties["annotation_id"] = "#{@url_manifest}/#{@library}/#{@uuid}/canvases/#{index}/ap/a"
if @v == "k5"
page_properties["body_id_iiif"] = "#{@kramerius}/search/iiif/#{uuid_page}/full/full/0/default.jpg"
page_properties["body_id_imgfull"] = "#{@kramerius}#{@items}#{uuid_page}/streams/IMG_FULL"
page_properties["alto_id"] = "#{@kramerius}#{@items}#{uuid_page}#{@alto_url}"
page_properties["thumb_id"] = "#{@kramerius}#{@items}#{uuid_page}/thumb"
elsif @v == "k7"
# PROVERIT!
page_properties["body_id_iiif"] = "#{@kramerius}/search/iiif/#{uuid_page}/full/full/0/default.jpg"
page_properties["body_id_imgfull"] = "#{@kramerius}#{@items}#{uuid_page}/image"
page_properties["alto_id"] = "#{@kramerius}#{@items}#{uuid_page}#{@alto_url}"
page_properties["thumb_id"] = "#{@kramerius}#{@items}#{uuid_page}/image/thumb"
end
# cislo strany # typ strany
if @v == "k5"
if !page["details"][0].split("##")[0].nil?
page_properties["page_number"] = page["details"][0].split("##")[0].strip.sub(" ", "")
page_properties["label"] = page_properties["page_number"]
end
if !page["details"][0].split("##")[1].nil?
page_properties["page_type"] = page["details"][0].split("##")[1].strip.sub(" ", "")
page_properties["label"] = page_properties["label"] + ' (' + page_properties["page_type"] + ')'
end
elsif @v == "k7"
if !page["page.number"].nil?
page_properties["page_number"] = page["page.number"]
page_properties["label"] = page_properties["page_number"]
end
if !page["page.type"].nil?
page_properties["page_type"] = page["page.type"]
page_properties["label"] = page_properties["label"] + ' (' + page_properties["page_type"] + ')'
end
end
pages.push(page_properties)
@canvasIndex += 1
end
if !pages[0].nil?
# PROVERIT!
image_iiif_control = control_404("#{@kramerius}/search/iiif/#{pages[0]['pid']}/info.json")
iiif_image_body_control = !(get_xml("#{@kramerius}/search/iiif/#{pages[0]['pid']}/info.json") == '')
@image_iiif = image_iiif_control && iiif_image_body_control
# @first_page_width = get_json("#{@kramerius}/search/iiif/#{pages[0]['pid']}/info.json")["width"]
# @first_page_height = get_json("#{@kramerius}/search/iiif/#{pages[0]['pid']}/info.json")["height"]
end
# typhoeus test
if @full
if @image_iiif
hydra = Typhoeus::Hydra.new(max_concurrency: 10)
requests = pids.map{ |pid|
request = Typhoeus::Request.new(
"#{@kramerius}/search/iiif/#{pid}/info.json",
method: :get,
headers: {
"Content-Type" => "application/json"
},
followlocation: true
)
hydra.queue(request)
request
}
# puts 'before hydra run', Time.new
hydra.run
# puts 'after hydra run', Time.new
responses = requests.map { |request|
JSON(request.response.body) if request.response.code === 200
}
# puts responses
pages.each do |page|
pid2 = "#{@kramerius}/search/iiif/#{page["pid"]}"
if !responses.nil?
responses.each do |item|
if !item.nil?
if pid2 === item["@id"]
page["width"] = item["width"]
page["height"] = item["height"]
# page["thumb_width"] = item["sizes"][0]["width"]
# page["thumb_height"] = item["sizes"][0]["height"]
# page["width"] = item["sizes"][item["sizes"].length - 1]["width"]
# page["height"] = item["sizes"][item["sizes"].length - 1]["height"]
end
end
end
end
end
else
pages.each do |page|
page["width"] = 1000
page["height"] = 1000
end
end
else
pages.each do |page|
# if @image_iiif
# page["width"] = @first_page_width
# page["height"] = @first_page_height
# else
page["width"] = 1000
page["height"] = 1000
# end
end
end
return pages
end
def create_items_pages(uuid)
uuid = uuid.to_json
itemsCanvas = []
pages = create_list_of_pages(uuid)
if !pages[0].nil?
alto = control_404("#{@kramerius}#{@items}#{pages[0]['pid']}#{@alto_url}")
end
pages.each do |page|
itemsAnnotationPage = []
itemsAnnotation = []
seeAlso = {"id" => page["alto_id"],
"type" => "Alto",
"profile" => "https://www.loc.gov/standards/alto/v3/alto.xsd",
"label" => { "none" => ["ALTO XML"] },
"format" => "text/xml"}
body_service = {"@id" => "#{@kramerius}/search/iiif/#{page["pid"]}",
"@type" => "ImageService2",
"profile" => "http://iiif.io/api/image/2/level1.json",
}
# TODO thumb_service = {}
canvas_thumbnail = {"id" => page["thumb_id"],
"type" => "Image",
# TODO "service" => [thumb_service]
}
if @image_iiif
# PROVERIT
body = {"id" => page["body_id_iiif"],
"type" => "Image",
"format" => "image/jpeg",
"width" => page["width"],
"height" => page["height"],
"service" => [body_service]
}
else
body = {"id" => page["body_id_imgfull"],
"type" => "Image",
"format" => "image/jpeg",
}
end
annotation = {"id" => page["annotation_id"],
"type" => "Annotation",
"motivation" => "painting",
"body" => body,
"target" => page["canvas_id"]
}
annotationPage = {"id" => page["annotationPage_id"],
"type" => "AnnotationPage",
"items" => itemsAnnotation
}
if alto
canvas = {"id" => page["canvas_id"],
"type" => "Canvas",
"label" => { "none" => [page["label"]]},
"width" => page["width"],
"height" => page["height"],
"thumbnail" => [canvas_thumbnail],
"seeAlso" => [seeAlso],
"items" => itemsAnnotationPage
}
else
canvas = {"id" => page["canvas_id"],
"type" => "Canvas",
"label" => { "none" => [page["label"]]},
"width" => page["width"],
"height" => page["height"],
"thumbnail" => [canvas_thumbnail],
"items" => itemsAnnotationPage
}
end
itemsAnnotation.push(annotation)
itemsAnnotationPage.push(annotationPage)
itemsCanvas.push(canvas)
end
return itemsCanvas
end
def part_of
uuid = "#{@uuid}".to_json
solr_request = "#{@kramerius}#{@solr_url}fl=#{@parent_pid}&q=#{@pid}:#{uuid}&rows=1500&start=0"
object = get_json(solr_request)
response_body = object["response"]["docs"]
partOf = []
response_body.each do |a|
if @v == "k5"
a["#{@parent_pid}"].each do |pid|
item = {}
uuid = pid.to_json
solr_request = "#{@kramerius}#{@solr_url}fl=#{@pid},#{@title_url},#{@root_title_url},#{@fedora_model}&q=#{@pid}:#{uuid}&rows=1500&start=0"
object = get_json(solr_request)
response_body = object["response"]["docs"][0]
if response_body["#{@fedora_model}"] == "periodical" || response_body["#{@fedora_model}"] == "periodicalvolume" || response_body["#{@fedora_model}"] == "soundrecording" || response_body["#{@fedora_model}"] == "monograph"
item["id"] = "#{@url_manifest}/#{@library}/#{pid}"
item["type"] = "Collection"
item["label"] = { "none": [response_body["#{@root_title_url}"]]}
end
partOf.push(item)
end
elsif @v == "k7"
item = {}
pid = a["#{@parent_pid}"]
uuid = pid.to_json
solr_request = "#{@kramerius}#{@solr_url}fl=#{@pid},#{@title_url},#{@root_title_url},#{@fedora_model}&q=#{@pid}:#{uuid}&rows=1500&start=0"
object = get_json(solr_request)
response_body = object["response"]["docs"][0]
if response_body["#{@fedora_model}"] == "periodical" || response_body["#{@fedora_model}"] == "periodicalvolume" || response_body["#{@fedora_model}"] == "soundrecording" || response_body["#{@fedora_model}"] == "monograph"
item["id"] = "#{@url_manifest}/#{@library}/#{pid}"
item["type"] = "Collection"
item["label"] = { "none": [response_body["#{@root_title_url}"]]}
end
partOf.push(item)
end
end
return partOf
end
# ---------- MONOGRAFIE -----------
def create_iiif_monograph
if !@mods["coordinates"].nil?
context = ["http://iiif.io/api/extension/navplace/context.json", "http://iiif.io/api/presentation/3/context.json"]
iiif = {"@context" => context,
"id" => "#{@url_manifest}/#{@library}/#{@uuid}",
"type" => "Manifest",
"label" => create_label(@uuid),
"metadata" => create_metadata,
# "behavior" => create_behavior,
"provider" => create_provider(@library)[0],
"homepage" => create_homepage,
"thumbnail" => create_thumbnail(@uuid),
"navPlace" => create_navPlace,
"items" => create_items_pages(@uuid)
}
elsif @document_model == 'monographunit'
context = "http://iiif.io/api/presentation/3/context.json"
iiif = {"@context" => context,
"id" => "#{@url_manifest}/#{@library}/#{@uuid}",
"type" => "Manifest",
"label" => create_label(@uuid),
"metadata" => create_metadata,
# "behavior" => create_behavior,
"provider" => create_provider(@library)[0],
"homepage" => create_homepage,
"thumbnail" => create_thumbnail(@uuid),
"items" => create_items_pages(@uuid),
"partOf" => part_of
}
else
context = "http://iiif.io/api/presentation/3/context.json"
iiif = {"@context" => context,
"id" => "#{@url_manifest}/#{@library}/#{@uuid}",
"type" => "Manifest",
"label" => create_label(@uuid),
"metadata" => create_metadata,
# "behavior" => create_behavior,
"provider" => create_provider(@library)[0],
"homepage" => create_homepage,
"thumbnail" => create_thumbnail(@uuid),
"items" => create_items_pages(@uuid)
}
end
return JSON.pretty_generate(iiif)
end
# ---------- VICESVAZKY -----------
def create_iiif_monograph_collection
context = "http://iiif.io/api/presentation/3/context.json"
iiif = {"@context" => context,
"id" => "#{@url_manifest}/#{@library}/#{@uuid}",
"type" => "Collection",
"label" => create_label(@uuid),
"metadata" => create_metadata,
# "behavior" => create_behavior,
"provider" => create_provider(@library)[0],
"homepage" => create_homepage,
"thumbnail" => create_thumbnail(@uuid),
"items" => create_items_monographunits
}
return JSON.pretty_generate(iiif)
end
def create_items_monographunits
itemsMonographunits= []
monographunits = create_list_of_monographunits
monographunits.each do |monographunit|
item = {"id" => "#{@url_manifest}/#{@library}/#{monographunit["pid"]}",
"type" => "Manifest",
# "label" => {"non": ["#{monographunit["label"]}"]}
}
if !monographunit["label"].nil?
item["label"] = {"none": ["#{monographunit["label"]}"]}
elsif !monographunit["pid"].nil?
item["label"] = {"none": ["#{monographunit["title"]}"]}
end
itemsMonographunits.push(item)
end
return itemsMonographunits
end
def create_list_of_monographunits
uuid = "#{@uuid}".to_json
object = get_json("#{@kramerius}#{@solr_url}fl=#{@pid},#{@details},#{@rels_ext_index},#{@fedora_model},#{@title_url}&q=#{@parent_pid}:#{uuid} AND #{@fedora_model}:monographunit&rows=1500&start=0")
response_body = object["response"]["docs"]
sorted_object = response_body.sort { |a, b| a["#{@rels_ext_index}"] <=> b["#{@rels_ext_index}"]}
monographunits = []
sorted_object.each do |monographunit|
monographunit_properties = {}
monographunit_properties["index"] = monographunit["#{@rels_ext_index}"][0]
monographunit_properties["pid"] = monographunit["#{@pid}"]
if @v == "k5"
if !monographunit["details"][0].split("##")[0].nil?
monographunit_properties["number"] = monographunit["details"][0].split("##")[0].strip.sub(" ", "")
monographunit_properties["label"] = monographunit_properties["number"]
end
if !monographunit["details"][0].split("##")[1].nil?
monographunit_properties["title"] = monographunit["details"][0].split("##")[1].strip.sub(" ", "")
monographunit_properties["label"] = monographunit_properties["label"] + '. ' + monographunit_properties["title"]
end
elsif @v == "k7"
if !monographunit["part.number.str"].nil?
monographunit_properties["number"] = monographunit["part.number.str"]
end
if !monographunit["part.name"].nil?
monographunit_properties["title"] = monographunit["part.name"]
monographunit_properties["label"] = monographunit["part.name"]
else
monographunit_properties["label"] = monographunit["title.search"]
end
end
monographunits.push(monographunit_properties)
end
return monographunits
end
# ---------- CISLO PERIODIKA -----------
def create_iiif_periodicalissue
iiif = {"@context" => "http://iiif.io/api/presentation/3/context.json",
"id" => "#{@url_manifest}/#{@library}/#{@uuid}",
"type" => "Manifest",
"label" => create_label(@uuid),
"metadata" => create_metadata,
"provider" => create_provider(@library)[0],
"homepage" => create_homepage_periodical_volume_issue,
"thumbnail" => create_thumbnail(@uuid),
"items" => create_items_pages(@uuid),
"partOf" => part_of
}
return JSON.pretty_generate(iiif)
end
# ---------- ROCNIK PERIODIKA -----------
def create_iiif_periodicalvolume
iiif = {"@context" => "http://iiif.io/api/presentation/3/context.json",
"id" => "#{@url_manifest}/#{@library}/#{@uuid}",
"type" => "Collection",
"label" => create_label(@uuid),
"metadata" => create_metadata,
"provider" => create_provider(@library)[0],
"homepage" => create_homepage_periodical_volume_issue,
"thumbnail" => create_thumbnail(@uuid),
"items" => create_items_periodical_issues,
"partOf" => part_of
}
return JSON.pretty_generate(iiif)
end
def create_list_of_periodical_issues
uuid = "#{@uuid}".to_json
# najdu si cisla (items) a seradim
object = get_json("#{@kramerius}#{@solr_url}fl=#{@pid},#{@details},#{@rels_ext_index}&q=#{@parent_pid}:#{uuid} AND #{@fedora_model}:periodicalitem&rows=1500&start=0")
response_body = object["response"]["docs"]
sorted_object = response_body.sort { |a, b| a["#{@rels_ext_index}"] <=> b["#{@rels_ext_index}"]}