API Documentation
Core modules
pyreslib.koha
explicit_abbreviations_from_marc(record, abbreviations_dir=os.path.join('data', 'mappings', 'abbreviations'), abbreviation_codes={'relationships': {'fields': ['100$4', '700$4']}, 'languages': {'fields': ['041$a']}, 'item_types': {'fields': ['942$c']}, 'musical_instruments': {'fields': ['059$a']}})
The method returns a MARC-in-JSON record where MARC abbreviation codes are made explicit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
dict
|
MARC-in-JSON record from Koha API. |
required |
abbreviations_dir
|
str
|
Directory path (in data/mappings) containing a series of JSON mappings for standard MARC abbreviation codes. |
join('data', 'mappings', 'abbreviations')
|
abbreviation_codes
|
dict
|
Dictionary of abbreviation codes tied to each type. |
{'relationships': {'fields': ['100$4', '700$4']}, 'languages': {'fields': ['041$a']}, 'item_types': {'fields': ['942$c']}, 'musical_instruments': {'fields': ['059$a']}}
|
Returns: explicit_record (dict): MARC-in-JSON record with explicit abbreviations.
Source code in pyreslib/koha.py
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 | |
fetch_and_process_authority(auth_id, session, base_url)
Fetch a single authority from API and extract metadata. This function runs in parallel worker processes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
oauth2
|
Oauth2 session provided by |
required |
auth_id
|
int
|
Authority ID for the requested record. |
required |
base_url
|
str
|
Koha API url from credentials. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
result |
dict
|
Dictionary containing authority ID, Wikidata ID and MARC-in-JSON record. |
Source code in pyreslib/koha.py
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 | |
fetch_and_process_biblio(biblio_id, session, base_url)
Fetch a single bibliographic record from API and extract metadata. This function runs in parallel worker processes.
Args:
session (oauth2): Oauth2 session provided by pyreslib.koha.oauth2_session method.
biblio_id (int): Biblio ID for the requested record.
base_url (str): Koha API url from credentials.
Returns: result (dict): Dictionary containing biblio ID, and MARC-in-JSON record.
Source code in pyreslib/koha.py
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 | |
get_alternative_headings_from_authority(record, headings={'PERSO_NAME': '400', 'CORPO_NAME': '410', 'CHRON_TERM': '448', 'TOPIC_TERM': '450', 'GEOGR_NAME': '451', 'KOOPM_KEYW': '450'}, heading_subfield='a')
This function returns a list of alternative headings (equivalent terms, pseudonyms,...) of a given record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
dict
|
MARC-in-JSON record of the authority, conform with Koha API. |
required |
headings
|
dict
|
Dictionary of main heading fields according to authority type. Default is {"PERSO_NAME": "100","CORPO_NAME": "110","CHRON_TERM": "148","TOPIC_TERM": "150","GEOGR_NAME": "151"} |
{'PERSO_NAME': '400', 'CORPO_NAME': '410', 'CHRON_TERM': '448', 'TOPIC_TERM': '450', 'GEOGR_NAME': '451', 'KOOPM_KEYW': '450'}
|
heading_subfield
|
str
|
Subfield of the main heading. Default is "a". |
'a'
|
Returns:
| Name | Type | Description |
|---|---|---|
alternative_headings |
list
|
List of alternative headings. |
Examples:
>>> pyreslib.koha.get_alternative_headings_from_authority(record=marc_json_authority)
>>> ["Example Person", "Example Pseudonym", "Example Equivalent Term"]
Source code in pyreslib/koha.py
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 | |
get_auth_list_from_csv_report(auth_id_csv_filepath='./data/mappings/koha/authority_list.csv', auth_id_field='authid')
Returns a list of all authorities in the catalogue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
auth_id_csv_filepath
|
str
|
File path of the csv file coming from report. By default this is |
'./data/mappings/koha/authority_list.csv'
|
auth_id_field
|
str
|
Koha report field name for authority ID. Default is |
'authid'
|
Returns:
| Name | Type | Description |
|---|---|---|
authority_list |
list
|
List of authority IDs. |
Source code in pyreslib/koha.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | |
get_authority_json(session, auth_id, base_url)
Get JSON data for authority from Koha API. The amount of metadata is very limited compared to the MARC-in-JSON response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
oauth2
|
Oauth2 session provided by |
required |
auth_id
|
int
|
Authority ID for the requested record. |
required |
base_url
|
str
|
Koha API url from credentials. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
response |
dict
|
JSON serialization of the record. |
Examples:
>>> my_session = pyreslib.koha.oauth2_session(client_id="{CLIENT_ID}"", client_secret="{SECRET_KEY}" , base_url="https://{KOHA_STAFF_URL}/api/v1")
>>> json_authority = pyreslib.koha.get_authority_json(my_session,407,base_url="https://{KOHA_STAFF_URL}/api/v1")
>>> {'authority_id': 11721, 'created_date': '2021-01-12', 'framework_id': 'PERSO_NAME', 'heading': 'Bach, Anna Magdalena 1701-1760', 'modified_date': '2026-03-30T09:54:31+02:00'}
Source code in pyreslib/koha.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
get_authority_marc(session, auth_id, base_url)
Get MARC in JSON data for authority from Koha API
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
oauth2
|
Oauth2 session provided by |
required |
auth_id
|
int
|
Authority ID for the requested record. |
required |
base_url
|
str
|
Koha API url from credentials. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
response |
dict
|
MARC in JSON serialization of the record. |
Examples:
>>> my_session = pyreslib.koha.oauth2_session(client_id="{CLIENT_ID}"", client_secret="{SECRET_KEY}" , base_url="https://{KOHA_STAFF_URL}/api/v1")
>>> marc_json_authority = pyreslib.koha.get_authority_marc(my_session,407,base_url="https://{KOHA_STAFF_URL}/api/v1")
>>> marc_json_authority[0:145] # returns the first 145 characters of the JSON dictionary
>>>{'leader': '00744nam a2200217Ia 4500', 'fields': [{'000': '00373cz a2200157n 4500'}, {'001': '407'}, {'003': 'OI'}, {'005': '20260330105335.0'} ...
Source code in pyreslib/koha.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
get_authority_type(record, auth_type_field=['942', 'a'])
This function returns the authority type code of a given record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
dict
|
MARC-in-JSON record of the authority, conform with Koha API. |
required |
auth_type_field
|
list
|
MARC field holding the authority type code. Default is 942$a. |
['942', 'a']
|
Returns:
| Name | Type | Description |
|---|---|---|
auth_type |
str
|
Authority type code or None if not found. |
Examples:
>>> pyreslib.koha.get_authority_type(record=marc_json_authority)
>>> "PERSO_NAME"
Source code in pyreslib/koha.py
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 | |
get_biblio_json(session, biblio_id, base_url)
Get JSON data for biblio from Koha API. The amount of metadata is very limited compared to the MARC-in-JSON response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
oauth2
|
Oauth2 session provided by |
required |
biblio_id
|
int
|
Biblio ID for the requested record. |
required |
base_url
|
str
|
Koha API url from credentials. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
response |
dict
|
JSON serialization of the record. |
Examples:
>>> my_session = pyreslib.koha.oauth2_session(client_id="{CLIENT_ID}"", client_secret="{SECRET_KEY}" , base_url="https://{KOHA_STAFF_URL}/api/v1")
>>> json_record = pyreslib.koha.get_biblio_json(my_session,12,base_url="https://{KOHA_STAFF_URL}/api/v1")
>>>{'abstract': None, 'age_restriction': None, 'author': 'Playford, Henry', 'biblio_id': 1637, ... }
Source code in pyreslib/koha.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
get_biblio_list_from_csv_report(biblio_id_csv_filepath='./data/mappings/koha/biblio_list.csv', biblio_id_field='biblionumber')
Returns a list of all bibliographic records in the catalogue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
biblio_id_csv_filepath
|
str
|
File path of the csv file coming from report. By default this is |
'./data/mappings/koha/biblio_list.csv'
|
biblio_id_field
|
str
|
Koha report field name for biblio ID. Default is |
'biblionumber'
|
Returns:
| Name | Type | Description |
|---|---|---|
biblio_list |
list
|
List of biblio IDs. |
Source code in pyreslib/koha.py
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | |
get_biblio_marc(session, biblio_id, base_url)
Get MARC in JSON data for biblio from Koha API. Note: this method does not return items metadata, use pyreslib.koha.get_items_from_biblio_json in order to extract metadata at item level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
oauth2
|
Oauth2 session provided by |
required |
biblio_id
|
int
|
Biblio ID for the requested record. |
required |
base_url
|
str
|
Koha API url from credentials. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
response |
dict
|
MARC in JSON serialization of the record. |
Examples:
>>> my_session = pyreslib.koha.oauth2_session(client_id="{CLIENT_ID}"", client_secret="{SECRET_KEY}" , base_url="https://{KOHA_STAFF_URL}/api/v1")
>>> marc_json_record = pyreslib.koha.get_biblio_marc(my_session,12,base_url="https://{KOHA_STAFF_URL}/api/v1")
>>> {"leader": "01574acm a2200301 4500","fields": [{"001": "1"},{"005": "20251104114244.0"},{"008": "210318s1730 ||le| |||| 00| 0 fre d"},{"041": {"ind1": " ","ind2": " ","subfields": [{"a": "fre"}]}}, ...] }
Source code in pyreslib/koha.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
get_biblio_type(record, biblio_type_field=['942', 'c'])
This function returns the biblio type code of a given record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
dict
|
MARC-in-JSON record of the biblio, conform with Koha API. |
required |
biblio_type_field
|
list
|
MARC field holding the biblio type code. Default is 942$c. |
['942', 'c']
|
Returns:
| Name | Type | Description |
|---|---|---|
biblio_type |
str
|
Biblio type code. |
Examples:
>>> pyreslib.koha.get_biblio_type(record=marc_json_biblio)
>>> "BOO"
Source code in pyreslib/koha.py
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 | |
get_framework_id_authority(session, auth_id, base_url)
Get the authority record framework ID .
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
oauth2
|
Oauth2 session provided by |
required |
auth_id
|
int
|
Authority ID for the requested record. |
required |
base_url
|
str
|
Koha API url from credentials. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
framework |
str
|
Koha Framework ID for the record. |
Examples:
>>> my_session = pyreslib.koha.oauth2_session(client_id="{CLIENT_ID}"", client_secret="{SECRET_KEY}" , user_agent="{USER_AGENT}", base_url="https://{KOHA_STAFF_URL}/api/v1")
>>> pyres;lib.koha.get_framework_id_authority(my_session,1,base_url="https://{KOHA_STAFF_URL}/api/v1")
>>> "1"
Source code in pyreslib/koha.py
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
get_framework_id_biblioitem(session, biblio_id, base_url)
Get the biblio record framework ID .
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
oauth2
|
Oauth2 session provided by |
required |
biblio_id
|
int
|
Biblio ID for the requested record. |
required |
base_url
|
str
|
Koha API url from credentials. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
framework |
str
|
Koha Framework ID for the record. |
Examples:
>>> my_session = pyreslib.koha.oauth2_session(client_id="{CLIENT_ID}"", client_secret="{SECRET_KEY}" , user_agent="{USER_AGENT}", base_url="https://{KOHA_STAFF_URL}/api/v1")
>>> pyres;lib.koha.get_framework_id_biblioitem(my_session,12,base_url="https://{KOHA_STAFF_URL}/api/v1")
>>> "1"
Source code in pyreslib/koha.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
get_items_from_biblio_json(session, biblio_id, base_url)
Get JSON item data for biblio from Koha API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
oauth2
|
Oauth2 session provided by |
required |
biblio_id
|
int
|
Biblio ID for the requested record. |
required |
base_url
|
str
|
Koha API url from credentials. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
response |
dict
|
JSON serialization of the record's items. |
Examples:
>>> my_session = pyreslib.koha.oauth2_session(client_id="{CLIENT_ID}"", client_secret="{SECRET_KEY}" , base_url="https://{KOHA_STAFF_URL}/api/v1")
>>> json_items = pyreslib.koha.get_items_from_biblio_json(my_session,12)
>>>
Source code in pyreslib/koha.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
get_main_heading_from_authority(record, headings={'PERSO_NAME': '100', 'CORPO_NAME': '110', 'CHRON_TERM': '148', 'TOPIC_TERM': '150', 'GEOGR_NAME': '151', 'KOOPM_KEYW': '150'}, heading_subfield='a')
This function returns the authority main heading of a given record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
dict
|
MARC-in-JSON record of the authority, conform with Koha API. |
required |
headings
|
dict
|
Dictionary of main heading fields according to authority type. Default is {"PERSO_NAME": "100","CORPO_NAME": "110","CHRON_TERM": "148","TOPIC_TERM": "150","GEOGR_NAME": "151"} |
{'PERSO_NAME': '100', 'CORPO_NAME': '110', 'CHRON_TERM': '148', 'TOPIC_TERM': '150', 'GEOGR_NAME': '151', 'KOOPM_KEYW': '150'}
|
heading_subfield
|
str
|
Subfield of the main heading. Default is "a". |
'a'
|
Returns:
| Name | Type | Description |
|---|---|---|
main_heading |
str
|
Main heading string. |
Examples:
>>> pyreslib.koha.get_main_heading_from_authority(record=marc_json_authority)
>>> "Example Person"
Source code in pyreslib/koha.py
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 | |
get_umbrella_terms_from_authority(record, umbrella_fields={'PERSO_NAME': '500', 'CORPO_NAME': '510', 'CHRON_TERM': '548', 'TOPIC_TERM': '550', 'GEOGR_NAME': '551'}, umbrella_subfields={'label': 'a', 'id': '9'})
This function returns a list of umbrella terms of a given record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
dict
|
MARC-in-JSON record of the authority, conform with Koha API. |
required |
umbrella_fields
|
dict
|
Dictionary of umbrella fields for the authority type, according to MARC21 guidelines. |
{'PERSO_NAME': '500', 'CORPO_NAME': '510', 'CHRON_TERM': '548', 'TOPIC_TERM': '550', 'GEOGR_NAME': '551'}
|
umbrella_subfields
|
str
|
label and authority id for each umbrella term. Default is {"label": "a","id": "9"}. |
{'label': 'a', 'id': '9'}
|
Returns:
| Name | Type | Description |
|---|---|---|
umbrella_terms |
list
|
List of umbrella terms. |
Examples:
>>> pyreslib.koha.get_umbrella_terms_from_authority(record=marc_json_authority)
>>> [{"type": "PERSO_NAME", "label": "Example Person", "id": "434"},{...}]
Source code in pyreslib/koha.py
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 | |
get_wd_authority_list(report_csv_file=os.path.join('data', 'mappings', 'wikidata', 'wd_authority_list.csv'), separator='|')
This method returns a list of authority IDs and their corresponding Wikidata entities from a CSV report exported from Koha.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
report_csv_file
|
str
|
File path to the CSV report exported from Koha. See Reports page for the SQL query. |
join('data', 'mappings', 'wikidata', 'wd_authority_list.csv')
|
separator
|
str
|
Separator for multiple values. Pipe is default. |
'|'
|
Returns:
| Name | Type | Description |
|---|---|---|
authority_wd_list |
list
|
A list of dictionaries, each containing an authority ID and its corresponding Wikidata entities. |
Examples:
>>> authority_wd_list = pyreslib.koha.get_authority_wd_list(report_csv_file="path/to/authorities_wd_list.csv")
>>> [{"auth_id": 1, "type": "GEOGR_NAME", "main_heading": "Venice", "qid": ["Q641"], "wd_uri": ["http://www.wikidata.org/entity/Q641"], "wd_label": ["Venice","Venezia"]}, ...]
Source code in pyreslib/koha.py
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 | |
get_wikidata_entities_from_authority(record, wikidata_field='024', wikidata_subfields={'uri': '1', 'label': '9'})
This function returns a list of wikidata entities linked with a given record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
dict
|
MARC-in-JSON record of the authority, conform with Koha API. |
required |
wikidata_field
|
str
|
Default is "024", according to MARC21 guidelines. |
'024'
|
wikidata_subfields
|
str
|
label and authority id for each umbrella term. Default is {"label": "a","id": "9"}. |
{'uri': '1', 'label': '9'}
|
Returns:
| Name | Type | Description |
|---|---|---|
wikidata_entities |
list
|
List of Wikidata entities. |
Examples:
>>> pyreslib.koha.get_wikidata_entities_from_authority(record=marc_json_authority)
>>> [{"uri": "http://www.wikidata.org/entity/Q42", "label": "Douglas Adams"}, {"uri": "http://www.wikidata.org/entity/Q12345", "label": "Example Entity"}]
Source code in pyreslib/koha.py
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 | |
import_koha_authorities_from_api(session, base_url, output_directory='./data/koha_auth/json', num_workers=None)
Generate authority dictionary using parallel processing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
oauth2
|
Oauth2 session provided by |
required |
base_url
|
str
|
Koha API url from credentials. |
required |
output_directory
|
str
|
Output directory for the generated JSON. The default filename is "auth_dict-{yyyy-mm-dd}.json". Default is |
'./data/koha_auth/json'
|
num_workers
|
int
|
Number of parallel workers (default: CPU count) |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
auth_dict |
list
|
List of authority dictionaries with auth_id, wd_id, and record |
Source code in pyreslib/koha.py
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 | |
import_koha_authorities_from_marc(marc_filepath, output_directory='./data/koha_auth/json')
Returns a MARC-in-JSON dictionaries from a MARC import of the authority catalogue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
marc_filepath
|
str
|
Location of the MARC file to be processed. Default directory is |
required |
output_directory
|
str
|
Output directory for the generated JSON. The default filename is "auth_dict-{yyyy-mm-dd}.json". Default is |
'./data/koha_auth/json'
|
Returns: auth_dict (list): List of authority dictionaries with auth_id, wd_id, and record.
Source code in pyreslib/koha.py
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 | |
import_koha_biblios_from_api(session, base_url, output_directory='./data/koha_biblio/json', num_workers=None)
Generate authority dictionary using parallel processing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
oauth2
|
Oauth2 session provided by |
required |
base_url
|
str
|
Koha API url from credentials. |
required |
output_directory
|
str
|
Output directory for the generated JSON. The default filename is "biblio_dict-{yyyy-mm-dd}.json". Default is |
'./data/koha_biblio/json'
|
num_workers
|
int
|
Number of parallel workers (default: CPU count) |
None
|
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List of bibliographic record dictionaries with biblio_id and record |
Source code in pyreslib/koha.py
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 | |
import_koha_biblios_from_marc(marc_filepath, output_directory='./data/koha_biblio/json')
Returns a MARC-in-JSON dictionaries from a MARC import of the bibliographic catalogue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
marc_filepath
|
str
|
Location of the MARC file to be processed. Default directory is |
required |
output_directory
|
str
|
Output directory for the generated JSON. The default filename is "biblio_dict-{yyyy-mm-dd}.json". Default directory is |
'./data/koha_biblio/json'
|
Retruns
biblio_dict (list): List of bibliopgraphic record dictionaries with biblio_id and record
Source code in pyreslib/koha.py
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 | |
koha_session(client_id, client_secret, user_agent, base_url, scope='all')
Returns an OAuth2 session, given client ID and secret key of your Koha account.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client_id
|
str
|
Client ID associated with your Koha Admin User. |
required |
client_secret
|
str
|
Secret key provided by the Koha Administration. |
required |
user_agent
|
str
|
User-Agent string. Default is None. |
required |
base_url
|
str
|
Base URL for your Koha Staff interface |
required |
Returns:
| Name | Type | Description |
|---|---|---|
session |
oauth2
|
OAuth2 session |
Examples:
>>> my_session = pyreslib.koha.oauth2_session(client_id="{CLIENT_ID}"", client_secret="{SECRET_KEY}" , base_url="https://{KOHA_STAFF_URL}/api/v1")
Source code in pyreslib/koha.py
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 | |
query_biblio_marc(session, base_url, q, limit=20, offset=0)
Returns a list of MARC-in-JSON biblio record matching a query according to Koha standards. This method exploits the ListBiblio operation.
Unfortunately the method does not yet work.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
oauth2
|
Oauth2 session provided by |
required |
base_url
|
str
|
Koha API url from credentials. |
required |
q
|
str
|
Query string according to Koha standards. For example 'title:"Orlando furioso" AND author:"Ariosto"' |
required |
Examples:
Source code in pyreslib/koha.py
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 | |
update_authority_marc(session, auth_id, marc_json, base_url)
Update authority record given a MARC-in-JSON payload via Koha API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
oauth2
|
Oauth2 session provided by |
required |
auth_id
|
int
|
Authority ID for the requested record. |
required |
marc_json
|
dict
|
JSON dictionary serialization of the record. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
|
Examples:
>>> my_session = pyreslib.koha.oauth2_session(client_id="{CLIENT_ID}"", client_secret="{SECRET_KEY}" , base_url="https://{KOHA_STAFF_URL}/api/v1")
>>> marc_json_authority = {'leader': '00744nam a2200217Ia 4500', 'fields': [{'000': '00373cz a2200157n 4500'}, {'001': '407'}, {'003': 'OI'}, {'005': '20260330105335.0'} ...
>>> update_authority_marc(my_session,407,marc_json_authority)
Source code in pyreslib/koha.py
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 | |
update_biblio_marc(session, biblio_id, marc_json, base_url)
Update biblio record given a MARC-in-JSON payload via Koha API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
oauth2
|
Oauth2 session provided by |
required |
biblio_id
|
int
|
Biblio ID for the requested record. |
required |
marc_json
|
dict
|
JSON dictionary serialization of the record. |
required |
base_url
|
str
|
Koha API url from credentials. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
|
Examples:
>>> my_session = pyreslib.koha.oauth2_session(client_id="{CLIENT_ID}"", client_secret="{SECRET_KEY}" , base_url="https://{KOHA_STAFF_URL}/api/v1")
>>> marc_json_record = {'leader': '00744nam a2200217Ia 4500', 'fields': [{'000': '00373cz a2200157n 4500'}, {'001': '12'}, {'003': 'OI'}, {'005': '20260330105335.0'} ...
>>> update_biblio_marc(my_session,12,marc_json_record)
Source code in pyreslib/koha.py
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 | |
Enhancing modules
pyreslib.wikidata
add_qids_to_authorities(koha_session, koha_base_url, auth_qid_csv='./data/wikidata/ingest_qid_to_auth/add_qids_to_authorities.csv', field='024', subfield='1')
Given a CSV file with fields authid, main_heading, wd_uri and type, generated from Koha report (see reports section)
and enriched via OpenRefine, the method updates the metadata authorities back to Koha via API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
koha_session
|
oauth2
|
Oauth2 session provided by |
required |
koha_base_url
|
str
|
Koha API url from credentials. |
required |
auth_qid_csv
|
str
|
filepath for the CSV file. Default is "./data/wikidata/ingest_qid_to_auth/add_qids_to_authorities.csv". |
'./data/wikidata/ingest_qid_to_auth/add_qids_to_authorities.csv'
|
field
|
str
|
Wikidata field. Default is "024", according to MARC21 framework. |
'024'
|
subfield
|
str
|
Wikidata URI subfield. Default is "1", according to MARC21 framework. |
'1'
|
Returns:
| Type | Description |
|---|---|
|
|
Source code in pyreslib/wikidata.py
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 | |
append_qid_to_qid_log(qid, qid_log, wb)
Appends metadata and occurrence information from a Wikidata entity which might become a candidate authority for the catalogue.
The user can evaluate the final result in data/wikidata/qid_log folder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
qid
|
str
|
Wikdiata QID value for entity. |
required |
qid_log
|
list
|
List of dictionaries holding metadata on Wikidata entities that could be part of the Koha thesaurus and their occurrence in statements. |
required |
wb
|
Wikibase Integrator session generated via pyreslib.wikidata.wikibase_integrator_session_basic or pyreslib.wikidata.wikibase_integrator_session_oauth2. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
qid_log |
list
|
Updated qid_log list. |
Source code in pyreslib/wikidata.py
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 | |
convert_point_in_time_to_date(point_in_time, date_format=f'%Y-%m-%d')
Returns a formatted date given a Wikibase [Point in time] string. The default format is %y-%m-%d according to the datetime syntaxt. For DD/MM/YYYY format use %d/%m/%y instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
point_in_time
|
str
|
Wikibase Point in Time string similar to ISO 8061 date, with the exception of + sign. The conversion only works for AD dates. |
required |
date_format
|
str
|
|
f'%Y-%m-%d'
|
Returns:
| Name | Type | Description |
|---|---|---|
date |
str
|
Formatted date. |
Examples:
>>> point_in_time = "+2016-01-01T00:00:00Z"
>>> date_value = convert_point_in_time_to_date(point_in_time)
>>> print(date_value)
>>> '2016-01-01'
Source code in pyreslib/wikidata.py
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 | |
convert_qid_to_URI(qid, base_uri='http://wikidata.org/')
Returns a QID string into the entity URI. By default the method uses the Wikidata Concept URI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
qid
|
str
|
Wikibase Point in Time string similar to ISO 8061 date, with the exception of + sign. The conversion only works for AD dates. |
required |
base_uri
|
str
|
By default set to |
'http://wikidata.org/'
|
Returns:
| Name | Type | Description |
|---|---|---|
URI |
str
|
URI of the entity. |
Examples:
>>> qid = "Q1296"
>>> print(convert_qid_to_URI(qid,base_uri="http://my.wikibase.org/"))
>>> 'http://my.wikibase.org/Q1296'
Source code in pyreslib/wikidata.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | |
enhance_authorities_via_wikidata(auth_dict, wb, auth_id_range={'min': 1, 'max': None}, backup_frequency=10, qid_log_dir=os.path.join('data', 'wikidata', 'qid_log'), wikidata_koha_mapping_filepath=os.path.join('data', 'mappings', 'wikidata', 'wikidata-koha-properties.csv'), wikibase_base_url='http://wwww.wikidata.org/entity/', backup_auth_dir=os.path.join('data', 'wikidata', 'backup_auth'), changed_auth_dir=os.path.join('data', 'wikidata', 'changed_auth'), in_progress=False, exclude_types=[], koha_fields={'PERSO_NAME': '500', 'CORPO_NAME': '510', 'CHRON_TERM': '548', 'TOPIC_TERM': '550', 'GEOGR_NAME': '551'}, headings={'PERSO_NAME': '100', 'CORPO_NAME': '110', 'CHRON_TERM': '148', 'TOPIC_TERM': '150', 'GEOGR_NAME': '151'})
Generates a list of dictionaries of enhanced authorities and their backups to be later submitted via the Koha API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
auth_dict
|
list
|
Authorities list of dictionaries formatted according to pyreslib.koha.import_koha_authorities_from_marc or pyreslib.koha.import_koha_authorities_from_api. |
required |
wb
|
Wikibase Integrator session generated via pyreslib.wikidata.wikibase_integrator_session_basic or pyreslib.wikidata.wikibase_integrator_session_oauth2. |
required | |
auth_id_range
|
dict
|
Minimal and maximal authority id number for enhancement, previous and subsequent ones will be ignored. Default is {"min": 1,"max": None}, meaning all catalogue is going to be exposed to enhancement. |
{'min': 1, 'max': None}
|
backup_frequency
|
int
|
Backup frequency. |
10
|
wikidata_koha_mapping_filepath
|
str
|
Path to Wikidata-Koha mapping CSV. |
join('data', 'mappings', 'wikidata', 'wikidata-koha-properties.csv')
|
wikibase_base_url
|
str
|
Base URL for Wikibase instance. Default is wikidata URI "http://wwww.wikidata.org/entity/". |
'http://wwww.wikidata.org/entity/'
|
backup_auth_dir
|
str
|
Destination path for backup enhanced authorities as JSON. |
join('data', 'wikidata', 'backup_auth')
|
changed_auth_dir
|
str
|
Destination path for changed enhanced authorities as JSON. |
join('data', 'wikidata', 'changed_auth')
|
in_progress
|
bool
|
Retrieves latest files in |
False
|
exclude_types
|
list
|
Authority type codes to be excluded from the process. Default is empty list. |
[]
|
koha_fields
|
dict
|
Fields for umbrella terms statements for authority record. Default list is definined according to the 5XX MARC21 framework. |
{'PERSO_NAME': '500', 'CORPO_NAME': '510', 'CHRON_TERM': '548', 'TOPIC_TERM': '550', 'GEOGR_NAME': '551'}
|
headings
|
dict
|
Fields for main heading statements for authority record. Default list is definined according to the 1XX MARC21 framework. |
{'PERSO_NAME': '100', 'CORPO_NAME': '110', 'CHRON_TERM': '148', 'TOPIC_TERM': '150', 'GEOGR_NAME': '151'}
|
Returns:
| Type | Description |
|---|---|
|
changed_authorities, backup_authorities (list): Lists of dictionaries for the enhanced authorities and their backups. You can locally store, or submit the changes to the catalogue via the API later on. |
Examples:
>>> auth_dict = pyreslib.koha.import_koha_authorities_from_api(session=koha_session,base_url=koha_base_url)
>>> changed_authorities, backup_authorities = pyreslib.wikidata.external_sources_metadata_authorities(auth_dict=auth_dict)
>>> changed_authorities
>>> [{{"auth_id": 34922,"wd_id": [254], "record": {... "fields": [...,{ ... } ...] }
>>> backup_authorities
>>> >>> [{{"auth_id": 34922,"wd_id": [254], "record": {... "fields": [..., { ... } ] }
Source code in pyreslib/wikidata.py
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 | |
external_sources_metadata_authorities(auth_dict, wb, backup_auth_dir=os.path.join('data', 'wikidata', 'backup_auth'), changed_auth_dir=os.path.join('data', 'wikidata', 'changed_auth'), external_ids_mapping_filepath=os.path.join('data', 'mappings', 'lod', 'external_identifiers.json'), uri_field=['024', '1'], source_subfield='2', label_subfield='9', id_subfield='a')
Given a list of External identifiers, by default stored in data/mappins/lod/external_identifiers.json, authorities' 024 fields are enhanced with extra metadata such as source, label and id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
auth_dict
|
list
|
Authorities list of dictionaries formatted according to pyreslib.koha.import_koha_authorities_from_marc or pyreslib.koha.import_koha_authorities_from_api. |
required |
wb
|
Wikibase Integrator session generated via pyreslib.wikidata.wikibase_integrator_session_basic or pyreslib.wikidata.wikibase_integrator_session_oauth2. |
required | |
external_ids_mapping_filepath
|
str
|
Path to external ids mapping JSON. |
join('data', 'mappings', 'lod', 'external_identifiers.json')
|
source_subfield
|
str
|
Koha subfield where the name of the external source is recorded. Default is "2", but you can change it according to your MARC framework. |
'2'
|
label_subfield
|
str
|
Koha subfield where the label of the external source is recorded. Default is "9", but you can change it according to your MARC framework. |
'9'
|
id_subfield
|
str
|
Koha subfield where the id of the external source is recorded. Default is "a", but you can change it according to your MARC framework. |
'a'
|
Returns:
| Type | Description |
|---|---|
|
changed_authorities, backup_authorities (list): Lists of dictionaries for the enhanced authorities and their backups. You can locally store, or submit the changes to the catalogue via the API later on. |
Examples:
>>> auth_dict = pyreslib.koha.import_koha_authorities_from_api(session=koha_session,base_url=koha_base_url)
>>> changed_authorities, backup_authorities = pyreslib.wikidata.external_sources_metadata_authorities(auth_dict=auth_dict)
>>> changed_authorities
>>> [{{"auth_id": 34922,"wd_id": [254], "record": {... "fields": {"024": {... "subfields": [{"1": "http://www.wikidata.org/entity/Q254"},{"9": "Wolfgang Amadeus Mozart"},{"a": "Q254"},{"2": "Wikidata"} } ... ]} ...]
>>> backup_authorities
>>> >>> [{{"auth_id": 34922,"wd_id": [254], "record": {... "fields": {"024": {... "subfields": [{"1": "http://www.wikidata.org/entity/Q254"}] ...} ]
Source code in pyreslib/wikidata.py
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 | |
generate_qid_sorted_dict_and_list(auth_dict, exclude_types=[])
Generates a list of dictionaries and a sorted list of IDs from Koha authorities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
auth_dict
|
list
|
Authorities list of dictionaries formatted according to pyreslib.koha.import_koha_authorities_from_marc or pyreslib.koha.import_koha_authorities_from_api. |
required |
exclude_types
|
list
|
Authority type codes to be excluded from the process. Default is empty list. |
[]
|
Returns:
| Type | Description |
|---|---|
list
|
auth_dict_sorted_qid, auth_qid_list (list): Sorted list of dictionaries for authorities and list of ids for bisect search. |
Examples:
>>> auth_dict = pyreslib.koha.import_koha_authorities_from_api(session=koha_session,base_url=koha_base_url)
>>> auth_dict_sorted_qid, auth_qid_list = pyreslib.wikidata.generate_qid_sorted_dict_and_list(auth_dict,exclude_types=["GEOGR_NAME","CHRON_TERM"])
>>> auth_dict_sorted_qid
>>> [{"auth_id": 20935, "wd_id": 6955, "record": {"leader": ... , "fields": [...]}}, ...]
>>> auth_qid_list
>>> [6955,...]
Source code in pyreslib/wikidata.py
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 | |
generate_statistics_authorities_wikidata_enhancement(changed_authorities, statistics_filepath=os.path.join('data', 'wikidata', 'statistics', f'statistics-{utilities.get_current_date()}.json'), wikidata_koha_mapping_filepath=os.path.join('data', 'mappings', 'wikidata', 'wikidata-koha-properties.csv'), koha_fields={'PERSO_NAME': '500', 'CORPO_NAME': '510', 'CHRON_TERM': '548', 'TOPIC_TERM': '550', 'GEOGR_NAME': '551'}, authority_types=['PERSO_NAME', 'CORPO_NAME', 'CHRON_TERM', 'TOPIC_TERM', 'GEOGR_NAME'], wd_property_subfield='i')
Generates a JSON file that records statistics for the Wikidata enhanchement, such as number of authorities modified and the type of statements ingested.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
changed_authorities
|
list
|
Lists of dictionaries for the enhanced authorities. |
required |
statistics_filepath
|
str
|
Path to output JSON file. Default is |
join('data', 'wikidata', 'statistics', f'statistics-{get_current_date()}.json')
|
wikidata_koha_mapping_filepath
|
str
|
CSV file with Wikidata-Koha mapping of properties and fields. Default is |
join('data', 'mappings', 'wikidata', 'wikidata-koha-properties.csv')
|
koha_fields
|
dict
|
Fields for umbrella terms statements for authority record. Default list is defined according to the 5XX MARC21 framework. |
{'PERSO_NAME': '500', 'CORPO_NAME': '510', 'CHRON_TERM': '548', 'TOPIC_TERM': '550', 'GEOGR_NAME': '551'}
|
authority_types
|
list
|
List of authority type codes, according to MARC21 framework. |
['PERSO_NAME', 'CORPO_NAME', 'CHRON_TERM', 'TOPIC_TERM', 'GEOGR_NAME']
|
wd_property_subfield
|
str
|
Subfield where the Wikidata property URI is stored. Default is "i", to be set in MARC framework for every authority. |
'i'
|
Returns:
| Name | Type | Description |
|---|---|---|
statistics |
dict
|
Dictiorary of the statistics. |
Source code in pyreslib/wikidata.py
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 | |
retrieve_authority_from_qid(qid, auth_qid_list, auth_dict_sorted_qid)
Retrieves Koha authority metadata given a Wikidata QID
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
qid
|
str
|
Wikdiata QID value for entity. |
required |
auth_qid_list
|
list
|
Sorted list of ids for bisect search generated from pyreslib.wikidata.generate_qid_sorted_dict_and_list. |
required |
auth_dict_sorted_qid
|
list
|
Sorted list of dictionaries for authorities for bisect search generated from pyreslib.wikidata.generate_qid_sorted_dict_and_list. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
auth |
dict
|
Authority metadata or None if QID not present in catalogue. |
Examples:
>>> auth_dict_sorted_qid, auth_qid_list = pyreslib.wikidata.generate_qid_sorted_dict_and_list(auth_dict,exclude_types=[])
>>> qid = "Q6927"
>>> pyreslib.wikidata.retrieve_authority_from_qid(qid,auth_qid_list,auth_dict_sorted_qid)
>>> {"auth_id": 20936, "wd_id": [6927], "record": {"leader": ... , "fields": [...]}}
Source code in pyreslib/wikidata.py
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 | |
update_enhanced_authorities(koha_session, koha_base_url, changed_auth_filepath=os.path.join('data', 'wikidata', 'changed_auth', f'changed_auth-{utilities.get_current_date()}.json'), ckeck_updates=5)
Submit enhanced authorities via Koha API from JSON list after review.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
changed_auth_filepath
|
str
|
Filepath of the |
join('data', 'wikidata', 'changed_auth', f'changed_auth-{get_current_date()}.json')
|
koha_session
|
oauth2
|
Oauth2 session provided by |
required |
koha_base_url
|
str
|
Koha API url from credentials. |
required |
check_updates
|
int
|
Number of checks before batch. To be manually done by user in Koha Staff interface. |
required |
Returns: None
Examples:
Source code in pyreslib/wikidata.py
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 | |
wb_get_property_data(wb_entity, pid, wikibase_URI=False, base_uri='http://wikidata.org/entity/', date_format=f'%Y-%m-%d')
Returns a dictionary with information related to statements, given a WikibaseIntegrator entity tied to a QID and a specific property PID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wb_entity
|
|
required | |
pid
|
str
|
String indicating the PID of the property. |
required |
wikibase_URI
|
bool
|
Returns full URI of Wikibase item. False by default. |
False
|
base_uri
|
str
|
Base Concept URI for the item. By default |
'http://wikidata.org/entity/'
|
date_format
|
str
|
|
f'%Y-%m-%d'
|
Returns:
| Name | Type | Description |
|---|---|---|
statements |
dict
|
List of dictionaries with statement values, qualifiers and references. |
Examples:
>>> qid = "Q1296"
>>> pid = "P1082"
>>> wb_entity = wb.item.get(qid)
>>> statements = wb_get_property_data(wb_entity,pid)
>>> print(statements)
>>> [{"value": "Q257029","references": [], "qualifiers": [{"pid": "P585","value": "2016-01-01"}]}, {"value": 230951,"references": [{"pid": "P143", "value": ""http://wikidata.org/"Q206855"}], "qualifiers": [{"pid": "P585","value": "2005-01-01"}]} ]
Source code in pyreslib/wikidata.py
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 | |
wikibase_integrator_session_basic(username, password, mediawiki_api_url='https://www.wikidata.org/w/api.php')
Returns an wikibase_integrator session, given user and password of your Wikidata account. We advice to create a Wikidata user in oder to prevent a "Too Many Requests HTTP 429" code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
username
|
str
|
Username associated with your Wikidata user. |
required |
password
|
str
|
Password associated with your Wikidata user. |
required |
mediawiki_api_url
|
str
|
Wikidata REST API by default. You can change it by replacing the |
'https://www.wikidata.org/w/api.php'
|
Returns:
| Name | Type | Description |
|---|---|---|
wb |
WikibaseIntegrator
|
WikibaseIntegrator session |
Examples:
>>> wb = pyreslib.koha.wikibase_integrator_session_basic(username="{USERNAME}"", password="{PASSWORD}" , mediawiki_api_url="https://www.wikidata.org/w/rest.php/wikibase/v1")
Source code in pyreslib/wikidata.py
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 | |
wikibase_integrator_session_oauth2(consumer_token, consumer_secret, mediawiki_api_url='https://www.wikidata.org/w/api.php')
Returns an wikibase_integrator session, given API consumer token and secret keys of your Wikidata account. You have to make a request to Wikimedia via this URL in order to have such credentials. For advanced use only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
consumer_token
|
str
|
API consumer key associated with your Wikidata user. |
required |
consumer_secret
|
str
|
API secret key associated with your Wikidata user. |
required |
mediawiki_api_url
|
str
|
Wikidata REST API by default. You can change it by replacing the |
'https://www.wikidata.org/w/api.php'
|
Returns:
| Name | Type | Description |
|---|---|---|
wb |
WikibaseIntegrator
|
Wikibase Integrator session |
Examples:
>>> wb = pyreslib.koha.wikibase_integrator_session_oauth2(consumer_token="{TOKEN}"", consumer_secret="{SECRET_TOKEN}" , mediawiki_api_url="https://www.wikidata.org/w/api.php")
Source code in pyreslib/wikidata.py
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 | |
pyreslib.google_books
create_marc_field(tag, subfields, ind1, ind2)
Create a MARC field in Koha API format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tag
|
str
|
The MARC field tag |
required |
ind1
|
str
|
First indicator |
required |
ind2
|
str
|
Second indicator |
required |
subfields
|
list
|
List of dictionaries with subfield code and value. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
MARC field dictionary |
Source code in pyreslib/google_books.py
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 | |
enhance_biblio_record_from_isbn(biblio_id, koha_session, base_url, google_api_key)
Enhance a Koha biblio record with metadata from Google Books API using the ISBN number.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
biblio_id
|
int
|
The biblio_id of the book in Koha. |
required |
koha_session
|
An authenticated Koha session object. |
required | |
base_url
|
str
|
The base URL of the Koha API. |
required |
google_api_key
|
str
|
Your Google API key for accessing the Books API. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if successful, False otherwise |
Source code in pyreslib/google_books.py
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 | |
get_google_api_koha_mapping(google_koha_mapping_filepath=os.path.join('data', 'mappings', 'google', 'google_books-koha_mapping.csv'))
Get the mapping between Google Books API metadata fields and Koha metadata fields from a CSV file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
google_koha_mapping_filepath
|
str
|
The file path to the CSV file containing the mapping. |
join('data', 'mappings', 'google', 'google_books-koha_mapping.csv')
|
Returns:
| Name | Type | Description |
|---|---|---|
mapping |
list
|
A list of dictionaries of the mappins. |
Source code in pyreslib/google_books.py
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 | |
get_isbn_from_biblio_id(biblio_id, koha_session, base_url)
Get the ISBN number from a Koha biblio_id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
biblio_id
|
int
|
The biblio_id of the book in Koha. |
required |
koha_session
|
An authenticated Koha session object. |
required | |
base_url
|
str
|
The base URL of the Koha API. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
isbn |
str
|
The ISBN number of the book as string. |
Source code in pyreslib/google_books.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
get_metadata_from_google_api(isbn, google_api_key)
Get book metadata from Google Books API. Args: isbn (str): The ISBN number of the book as string. google_api_key (str): Your Google API key for accessing the Books API from credentials.
Returns:
| Name | Type | Description |
|---|---|---|
response |
dict
|
JSON dictionary of the Google API metadata |
Examples:
>>> get_metadata_from_google_api(isbn="9780674970472",goole_api_key=credentals["google"]["api_key"])
>>> {'kind': 'books#volumes', 'totalItems': 1, 'items': [{'kind': 'books#volume', 'id': 'IXV-EAAAQBAJ', 'etag': 'J8cdkA9cXTg', 'selfLink': 'https://www.googleapis.com/books/v1/volumes/IXV-EAAAQBAJ', 'volumeInfo': {'title': 'In Praise of Failure', 'subtitle': 'Four Lessons in Humility', 'authors': ['Costica Bradatan'], 'publisher': 'Harvard University Press' ...}
Source code in pyreslib/google_books.py
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 | |
transform_value(value, format_type)
Apply format transformations to values based on the format field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
The value to transform |
required | |
format_type
|
str
|
The format type from the mapping (e.g., 'ymd_date2year', 'name_surname2surname,_name') |
required |
Returns:
| Type | Description |
|---|---|
|
Transformed value |
Source code in pyreslib/google_books.py
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 | |
pyreslib.transkribus
api_login(user, password)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user
|
user
|
Client Username |
required |
password
|
str
|
Client secret Password |
required |
Returns: session: requests.Session() for Transkribus API
Source code in pyreslib/transkribus.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
classify_region_two_columns(region, page_center, error_margin)
Classify a region based o page_center x-coordinates, returning left, center or right
Source code in pyreslib/transkribus.py
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 | |
classify_regions_two_colums(page_xml_path, page_center_method='reference_region', reference_type='page-number', PAGEXML_NS='http://schema.primaresearch.org/PAGE/gts/pagecontent/2013-07-15')
Classifies regions according to two-column layout. You can use a reference region to define the center. It returns a list of dictionaries, with XML element and classification tag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
page_xml_path
|
str
|
Path to PAGEXML file. |
required |
page_center_method
|
str
|
Reference method in order to determine page center. Options are "image_width" (get half of the whole image length) and "reference_region" (a specific region is used as center). |
'reference_region'
|
reference_type
|
str
|
Region tag used for "reference_region" as |
'page-number'
|
PAGEXML_NS
|
str
|
Namespace URI for PAGEXML schema. Default is "http://schema.primaresearch.org/PAGE/gts/pagecontent/2013-07-15". |
'http://schema.primaresearch.org/PAGE/gts/pagecontent/2013-07-15'
|
Returns: ordered_regions(list): List of blocks
Source code in pyreslib/transkribus.py
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 | |
extract_reading_order_index(text_region)
Extract the readingOrder index from the custom attribute.
Returns:
| Type | Description |
|---|---|
int
|
Index value, or float('inf') if not found |
Source code in pyreslib/transkribus.py
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 | |
extract_region_polygonal_coordinates(region)
Returns a list of coordinates given the region id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region
|
Element
|
ET XML element of the region. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
coordinates |
list
|
List of (x,y) tuples of coordinates. |
Source code in pyreslib/transkribus.py
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 | |
fit_text_line_in_region(text_line, root, TRANSKRIBUS_NS_URL='http://schema.primaresearch.org/PAGE/gts/pagecontent/2013-07-15')
Fit text_line into best region, according to area intersection
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text_line
|
Element
|
XML element of text line to be inserted. |
required |
root
|
Element
|
XML root of the page document to be enriched. |
required |
Returns:
| Type | Description |
|---|---|
Element
|
root |
Source code in pyreslib/transkribus.py
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 | |
get_document_metadata(session, collection_id, document_id)
Returns a document metadata from Transkribus API
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Transkribus session from |
required | |
collection_id
|
int
|
Collection ID identifier from Transkribus. |
required |
document_id
|
int
|
Document ID identifier from Transkribus. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
document |
dict
|
Document metadata |
Examples:
>>> session = pyreslib.transkribus.api_login(user,password)
>>> document = pyreslib.transkribus.get_document_metadata(session,collection_id=2792,document_id=16606356)
>>> {"md": {...}, "pageList": {"pages":[...]}, ... }
Source code in pyreslib/transkribus.py
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 | |
get_documents_metadata(session, collection_id)
Returns a full dictionary of documents, including pages metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Transkribus session from |
required | |
collection_id
|
int
|
Collection ID identifier from Transkribus. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
documents |
list
|
List of documents, with metadata and pagelist. |
Examples:
>>> session = pyreslib.transkribus.api_login(user,password)
>>> documents = pyreslib.transkribus.get_documents_metadata(session,collection_id=2792)
>>> [{"md": {...}, "pageList": {"pages":[...]},"collection": {...}, ... }]
Source code in pyreslib/transkribus.py
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 | |
get_jpg_image(session, collection_id, document_id, page_number, output_dir)
Extract the JPG image of a given page from Transkribus API
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Transkribus session from |
required | |
collection_id
|
int
|
Collection ID identifier from Transkribus. |
required |
document_id
|
int
|
Document ID identifier from Transkribus. |
required |
page_number
|
int
|
Internal page number identifier from Transkribus. |
required |
output_dir
|
str
|
local directory for download. The filename is equal to the one stored in Transkribus metadata. |
required |
Returns:
| Type | Description |
|---|---|
|
|
Examples:
>>> session = pyreslib.transkribus.api_login(user,password)
>>> output_filepath = f"./data/kraken/transcriptions/{str(document_id)}"
>>> get_jpg_image(session,collection_id=2792,document_id=145869,page_number=14,output_dir=output_dir)
Source code in pyreslib/transkribus.py
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 | |
get_page_status(session, collection_id, document_id, page_number)
Retrieves the PAGEXML transcription status of a given page of a document as string.
Source code in pyreslib/transkribus.py
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 | |
get_page_txt(session, collection_id, document_id, page_number)
Retrieves the TXT transcription of a given page of a document as string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Transkribus session from |
required | |
collection_id
|
int
|
Collection ID identifier from Transkribus. |
required |
collection_id
|
int
|
Document ID identifier from Transkribus. |
required |
page_number
|
int
|
Internal page number identifier from Transkribus. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
plain_text |
str
|
string of the plaintext transcription of the page by Transkribus. |
Examples:
>>> session = pyreslib.transkribus.api_login(user,password)
>>> plain_text = pyreslib.transkribus.get_page_txt(session,collection_id=2792,document_id=145869,page_number=14)
Source code in pyreslib/transkribus.py
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 | |
get_page_xml(session, collection_id, document_id, page_number)
Retrieves the PAGEXML transcription of a given page of a document as string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Transkribus session from |
required | |
collection_id
|
int
|
Collection ID identifier from Transkribus. |
required |
document_id
|
int
|
Document ID identifier from Transkribus. |
required |
page_number
|
int
|
Internal page number identifier from Transkribus. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
page_xml |
str
|
string serialization of the PAGEXML transcription of the page by Transkribus. You can parse it later by usin |
Examples:
>>> import xml.etree.ElementTree as ET
>>> session = pyreslib.transkribus.api_login(user,password)
>>> page_xml = pyreslib.transkribus.get_page_xml(session,collection_id=2792,document_id=145869,page_number=14)
>>> root = ET.fromstring(page_xml)
Source code in pyreslib/transkribus.py
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 | |
get_polygonal_centroids(coordinate_list)
Returns the centroids of a lsit of coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coordinate_list
|
list
|
List of coordinates [(x_1,y_1), ... (x_n,x_n)]. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
centroids |
Tuple of x and y centroids. |
Source code in pyreslib/transkribus.py
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 | |
import_jpg_from_document(session, collection_id, document_id, output_dir)
Downloads JPG images from transkribus, giving them the same basename. as the page filename.
Args:
session(requests.Session): Transkribus session from pyreslib.transkribus.api_login() method.
collection_id (int): Collection ID identifier from Transkribus.
document_id (int): Document ID identifier from Transkribus.
output_dir(str | Path): Directory path for JPG files.
Source code in pyreslib/transkribus.py
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 | |
import_pagexml_from_document(session, collection_id, document_id, output_dir)
Downloads PAGEXML transcriptions from transkribus, giving them the same basename. as the page filename.
Args:
session(requests.Session): Transkribus session from pyreslib.transkribus.api_login() method.
collection_id (int): Collection ID identifier from Transkribus.
document_id (int): Document ID identifier from Transkribus.
output_dir(str | Path): Directory path for PAGEXML files.
Source code in pyreslib/transkribus.py
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 | |
import_text_transcription_to_koha_field(text, biblio_id, koha_session, koha_base_url, field=['520', 'a'])
Creates a new field statement for a bibliographic record with the given Transkribus transcription.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
String to be ingested in Koha. |
required |
biblio_id
|
int
|
Biblio ID for the record. |
required |
koha_session
|
oauth2
|
Oauth2 session provided by |
required |
koha_base_url
|
str
|
Koha API url from credentials. |
required |
field
|
list
|
Field and subfield where the text has to be added to record. Default is Summary/Abstract (520$a) field = ["520","a"] |
['520', 'a']
|
Returns:
| Type | Description |
|---|---|
|
None |
Source code in pyreslib/transkribus.py
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 | |
import_txt_from_document(session, collection_id, document_id, output_dir)
Downloads TXT transcriptions from transkribus, giving them the same basename. as the page filename.
Args:
session(requests.Session): Transkribus session from pyreslib.transkribus.api_login() method.
collection_id (int): Collection ID identifier from Transkribus.
document_id (int): Document ID identifier from Transkribus.
output_dir(str | Path): Directory path for TXT files.
Source code in pyreslib/transkribus.py
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 | |
intersection_area(coords1, coords2)
Calcuate the intesection area between two 2D-coordinate lists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coords1
|
list
|
First list of float coordinates. |
required |
coords2
|
list
|
Second list of float coordinates. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
intersection_area |
float
|
Size of intersection between two polygonal shapes. |
Source code in pyreslib/transkribus.py
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 | |
post_page_xml(session, page_xml, collection_id, document_id, page_number, filepath=True)
Post a PAGEXML file back to Transkribus, either from string or local file.
Args:
session: Transkribus session from pyreslib.transkribus.api_login() method.
page_xml (str): Filepath or XML string. Set filepath=True if you wish to interpret it as a path.
collection_id (int): Collection ID identifier from Transkribus.
document_id (int): Document ID identifier from Transkribus.
page_number (int): Internal page number identifier from Transkribus.
filepath (bool): True if page_xml is a filepath, False for page_xml as string.
Returns: None
Examples:
session = pyreslib.transkribus.api_login(user,password)
Source code in pyreslib/transkribus.py
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 | |
reading_order_document(session, collection_id, document_id, n_columns=2, page_center_method='image_width', reference_type='page-number', min_page_status='FINAL')
Applies reading order to whole document, only for pages with status equal or better that given parameter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Transkribus session from pyreslib.transkribus.api_login method. |
required | |
collection_id
|
int
|
Collection ID identifier from Transkribus. |
required |
doocument_id
|
int
|
Document ID identifier from Transkribus. |
required |
n_columns
|
int
|
Number of columns. The method accepts only 1 or 2. Default is 2. |
2
|
page_center_method
|
str
|
Reference method in order to determine page center. Options are "image_width" (get half of the whole image length) and "reference_region" (a specific region is used as center). |
'image_width'
|
reference_type
|
str
|
Region tag used for "reference_region" as |
'page-number'
|
min_page_status
|
str
|
Minimal status in order to apply reordering to page. Default is |
'FINAL'
|
Retuns
None
Source code in pyreslib/transkribus.py
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 | |
reading_order_regions(session, collection_id, document_id, page_number, n_columns=2, page_center_method='image_width', reference_type=None)
Update region order via Transkribus API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Transkribus session from pyreslib.transkribus.api_login method. |
required | |
collection_id
|
int
|
Collection ID identifier from Transkribus. |
required |
doocument_id
|
int
|
Document ID identifier from Transkribus. |
required |
page_number
|
int
|
Internal page number identifier from Transkribus. |
required |
n_columns
|
int
|
Number of columns. The method accepts only 1 or 2. Default is 2. |
2
|
page_center_method
|
str
|
Reference method in order to determine page center. Options are "image_width" (get half of the whole image length) and "reference_region" (a specific region is used as center). |
'image_width'
|
reference_type
|
str
|
Region tag used for "reference_region" as |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
page_xml_string |
str
|
String serialization of the modified PAGEXML file. The new XML file is automatically updated back to Transkribus via pyreslib.transkribus.post_page_xml method. |
Examples:
>>> ordered_page_xml = reading_order_regions(
session=transkribus_session,
collection_id=2353709,document_id=14756063,page_number=29,
n_columns=2,page_center_method="reference_region",reference_type="page-number")
>>> Exporting back to Transkribus...
Source code in pyreslib/transkribus.py
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 | |
pyreslib.kraken
serialization_prediction(segmentation, file_path, image, export_dir, xml_namespace)
Serializes recognition results to PageXML and plain text files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
segmentation
|
Segmentation object with recognition results |
required | |
file_path
|
str
|
Path of the original image. |
required |
image
|
PIL.Image object |
required | |
export_dir
|
str
|
Export directory. |
required |
xml_namespace
|
str
|
XML namespace for PageXML output. |
required |
Returns:
| Type | Description |
|---|---|
|
None |
Source code in pyreslib/kraken.py
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 | |
transcribe_directory(dir_path='./data/kraken/transcriptions', segmentation_model_path=None, recognition_model_path='./data/kraken/models/catmus-large/catmus-print-fondue-large.mlmodel', export_dir='./data/kraken/transcriptions', xml_namespace='http://schema.primaresearch.org/PAGE/gts/pagecontent/2019-07-15')
Transcribe image files within directory (no subdirectories allowed) using Kraken.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dir_path
|
str
|
Directory path where images are stored. Default is |
'./data/kraken/transcriptions'
|
segmentation_model_path
|
str
|
Path to segmentation model. Default is |
None
|
recognition_model_path
|
str
|
Path to recognition model. Default is the CATMUS Print large "./data/kraken/models/catmus-large/catmus-print-fondue-large.mlmodel" |
'./data/kraken/models/catmus-large/catmus-print-fondue-large.mlmodel'
|
export_dir
|
str
|
Export directory for the PAGEXML files. Default is |
'./data/kraken/transcriptions'
|
xml_namespace
|
str
|
PAGEXML namespace used by Kraken 7: |
'http://schema.primaresearch.org/PAGE/gts/pagecontent/2019-07-15'
|
Returns:
| Type | Description |
|---|---|
|
|
Source code in pyreslib/kraken.py
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 | |
transcribe_image(image_filepath, export_dir, segmentation_model_path=None, recognition_model_path='./data/kraken/models/catmus-large/catmus-print-fondue-large.mlmodel', xml_namespace='http://schema.primaresearch.org/PAGE/gts/pagecontent/2019-07-15')
Transcribe image file using Kraken, returning a PAGEXML and TXT transcriptions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dir_path
|
str
|
Directory path where images are stored. Default is |
required |
segmentation_model_path
|
str
|
Path to segmentation model. Default is |
None
|
recognition_model_path
|
str
|
Path to recognition model. Default is the CATMUS Print large "./data/kraken/models/catmus-large/catmus-print-fondue-large.mlmodel" |
'./data/kraken/models/catmus-large/catmus-print-fondue-large.mlmodel'
|
xml_namespace
|
str
|
PAGEXML namespace used by Kraken 7: |
'http://schema.primaresearch.org/PAGE/gts/pagecontent/2019-07-15'
|
Returns:
| Type | Description |
|---|---|
|
|
Source code in pyreslib/kraken.py
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 | |
transcription_dir_to_transkribus(transcription_dir, session, collection_id, document_id)
Transcribes a whole Transkribus document by:
- Getting all images from API using pyreslib.transkribus.get_jpg_image
- Transcribing images using kraken pyreslib.kraken.transcribe_image
- Updating transcriptions backt to Transkribus via [pyreslib.kraken].update_kraken_XML_to_transkribus][].
Source code in pyreslib/kraken.py
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 | |
update_kraken_XML_to_transkribus(session, collection_id, document_id, page_number, page_xml_filepath, keep_regions=True)
Converts and updates PAGEXML generated by Kraken back to Transkribus via API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Transkribus session from |
required | |
collection_id
|
int
|
Collection ID identifier from Transkribus. |
required |
document_id
|
int
|
Document ID identifier from Transkribus. |
required |
page_number
|
int
|
Internal page number identifier from Transkribus. |
required |
page_xml_filepath
|
str
|
Filepath for Kraken generated PAGEXML file. |
required |
keep_regions
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
|
|
Source code in pyreslib/kraken.py
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 | |
Conversion modules
pyreslib.marc
csv2marc(csv_filepath, marc_filepath, separator='|')
Converts a record CSV into MARC as dictionary file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
csv_filepath
|
str
|
Filepath for the input CSV file. See template in |
required |
marc_filepath
|
str
|
Filepath for the MARC file serialized using pymarc. |
required |
separator
|
str
|
CSV file separator for multiple values. Default is pipe. |
'|'
|
Returns:
| Name | Type | Description |
|---|---|---|
marc_as_dict |
dict
|
MARC record as dictionary for Koha API. |
Examples:
>>> csv_filepath = "./data/koha_biblio/csv/records-yyyy-mm-dd.csv"
>>> marc_filepath = "./data/koha_biblio/marc/records-yyyy-mm-dd.marc"
>>> csv2marc(csv_filepath,marc_filepath)
>>> {"leader": ... "fields": [{"001": ... }, ...]}
Source code in pyreslib/marc.py
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 | |
generate_record_dict(marc_filepath, json_filepath, id_name)
Returns a list of dictionaries from a MARC record downloaded via Koha Cataloging batch import tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
marc_filepath
|
str
|
Location of the MARC file to be processed. Default directory is |
required |
json_filepath
|
str
|
Destination file. Default directory is |
required |
id_name
|
str
|
Field name of ID value. Use |
required |
Returns:
| Name | Type | Description |
|---|---|---|
record_dict |
list
|
List of dictionaries in MARC-in-JSON format. |
Source code in pyreslib/marc.py
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 | |
marc2csv(marc_filepath, csv_filepath, separator='|')
Converts a MARC record into CSV.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
marc_filepath
|
str
|
Filepath for the MARC file serialized using pymarc. |
required |
csv_filepath
|
str
|
Filepath for the output CSV file. See template in |
required |
separator
|
str
|
CSV file separator for multiple values. Default is pipe. |
'|'
|
Returns:
None
Examples:
>>> csv_filepath = "./data/koha_biblio/csv/records-yyyy-mm-dd.csv"
>>> marc_filepath = "./data/koha_biblio/marc/records-yyyy-mm-dd.marc"
>>> marc2csv(marc_filepath,csv_filepath)
Source code in pyreslib/marc.py
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 | |
pyreslib.bibtex
convert_biblio_to_bibtex(biblio_id, bibtex_filepath, koha_session, base_url)
Converts MARC-in-JSON record from Koha API into BibTeX file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
biblio_id
|
int
|
Unique idenfier for the record in Koha catalogue. |
required |
bibtex_filepath
|
str
|
String filepath indicating the location where the .bib output has to be stored. |
required |
koha_session
|
oauth2
|
Oauth2 session provided by |
required |
base_url
|
str
|
Koha API url from credentials. |
required |
Returns:
| Type | Description |
|---|---|
|
|
Examples:
Source code in pyreslib/bibtex.py
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 | |
pyreslib.rdf
This module exports bibliographic and authority records as RDF data structures, according to a given mapping.
auth_records_to_rdf(auth_ids, session, base_url, koha_namespace, auth_rdf_mapping_filepath='./data/mappings/lod/koha-rdf_mapping-auth.csv', namespaces_filepath='./data/mappings/lod/rdf_namespaces.json', output_dir='./data/rdf/auth/', serialization_format='turtle', explicit_abbreviations=True)
Convert authority records to RDF graph.
Given a list of authority IDs, returns an rdflib.Graph object with RDF serializations of the authority records according to the provided mapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
auth_ids
|
list
|
List of authority record IDs as integers. |
required |
session
|
Session
|
OAuth2 session provided by |
required |
base_url
|
str
|
Koha API URL from credentials. |
required |
koha_namespace
|
str
|
Prefix used for your Koha namespace in the namespaces JSON file (e.g., "oikoha"). Omit "auth" suffix. |
required |
auth_rdf_mapping_filepath
|
str
|
File path for MARC-to-RDF mapping CSV.
Default is |
'./data/mappings/lod/koha-rdf_mapping-auth.csv'
|
namespaces_filepath
|
str
|
JSON file with RDF namespaces.
Default is |
'./data/mappings/lod/rdf_namespaces.json'
|
output_dir
|
str
|
Output directory for RDF files. Files are named
|
'./data/rdf/auth/'
|
serialization_format
|
str
|
RDF serialization format. Options: "turtle", "json-ld", "pretty-xml", "xml". Default is "turtle". |
'turtle'
|
explicit_abbreviations
|
bool
|
Whether to expand MARC abbreviation codes
using |
True
|
Returns:
| Type | Description |
|---|---|
Graph
|
rdflib.Graph: Combined RDF graph containing all authority records. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If mapping or namespaces files not found. |
ValueError
|
If required namespaces not found in namespaces file. |
Examples:
>>> auth_ids = [1, 342, 17]
>>> session = koha.koha_session(client_id="...", ...)
>>> g = auth_records_to_rdf(
... auth_ids=auth_ids,
... session=session,
... base_url="https://koha.example.com/api/v1",
... koha_namespace="oikoha"
... )
>>> g.serialize(format="turtle", destination="authorities.ttl")
Source code in pyreslib/rdf.py
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 | |
biblio_records_to_rdf(biblio_ids, session, base_url, koha_namespace, biblio_rdf_mapping_filepath='./data/mappings/lod/koha-rdf_mapping-biblio.csv', namespaces_filepath='./data/mappings/lod/rdf_namespaces.json', output_dir='./data/rdf/biblio/', serialization_format='turtle', explicit_abbreviations=True)
Convert bibliographic records to RDF graph.
Given a list of biblio IDs, returns an rdflib.Graph object with RDF serializations of the bibliographic records according to the provided mapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
biblio_ids
|
list
|
List of bibliographic record IDs as integers. |
required |
session
|
Session
|
OAuth2 session provided by |
required |
base_url
|
str
|
Koha API URL from credentials. |
required |
koha_namespace
|
str
|
Prefix used for your Koha namespace in the namespaces JSON file (e.g., "oikoha"). Omit "biblio"/"auth" suffix. |
required |
biblio_rdf_mapping_filepath
|
str
|
File path for MARC-to-RDF mapping CSV.
Default is |
'./data/mappings/lod/koha-rdf_mapping-biblio.csv'
|
namespaces_filepath
|
str
|
JSON file with RDF namespaces.
Default is |
'./data/mappings/lod/rdf_namespaces.json'
|
output_dir
|
str
|
Output directory for RDF files. Files are named
|
'./data/rdf/biblio/'
|
serialization_format
|
str
|
RDF serialization format. Options: "turtle", "json-ld", "pretty-xml", "xml". Default is "turtle". |
'turtle'
|
explicit_abbreviations
|
bool
|
Whether to expand MARC abbreviation codes
using |
True
|
Returns:
| Type | Description |
|---|---|
Graph
|
rdflib.Graph: Combined RDF graph containing all bibliographic records. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If mapping or namespaces files not found. |
ValueError
|
If required namespaces not found in namespaces file. |
Examples:
>>> biblio_ids = [1, 342, 17]
>>> koha_session = koha.koha_session(client_id="...", ...)
>>> g = biblio_records_to_rdf(
... biblio_ids=biblio_ids,
... session=koha_session,
... base_url="https://koha.example.com/api/v1",
... koha_namespace="oikoha"
... )
>>> g.serialize(format="turtle", destination="biblios.ttl")
Source code in pyreslib/rdf.py
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 | |
pyreslib.omekas
change_item_property_value(session, value, property_name, resource_id, resource_type, value_position=0)
Change the value of a specific statement of a property of a given resource.
Args:
session: omeka_s_tools session generated by [pyreslib.omekas.omekas_session].
value (str): new value for the property.
property_name (str): name of the property to be updated, such as dcterms:title.
resource_id (int): ID of the item set to be added to the site.
resource_type (str): Resource type, such as items, item_sets and media.
value_position (int): position of the value to be change, in case of multiple statements for property. 0 is default.
Returns:
| Type | Description |
|---|---|
|
None |
Source code in pyreslib/omekas.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
generate_omekas_mapping(mappings_directory)
Generates a dictionary from a series of CSV files (auth,biblio,media...). Args: mappings_directory (str): Path of csv mapping files. Returns: A dictionary of mappings.
Source code in pyreslib/omekas.py
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 | |
omekas_session(api_url, key_identity, key_credential)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_url
|
str
|
API URL for your Omeka S instance. |
required |
key_identity
|
str
|
Omeka S key identity for the user. |
required |
key_credential
|
str
|
Omeka S key credential for the user. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
omekas_session |
|
Source code in pyreslib/omekas.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |