1
|
from pydantic import BaseModel, Schema
|
2
|
from typing import Dict, List
|
3
|
from datetime import datetime
|
4
|
from fastapi import HTTPException
|
5
|
import logging
|
6
|
from time import time
|
7
|
|
8
|
rels = dict(issupplementto="IsSupplementTo", issupplementedby="IsSupplementedBy", references="References",
|
9
|
isreferencedby="IsReferencedBy")
|
10
|
|
11
|
class LinkProvider(BaseModel):
|
12
|
name:str = Schema(None, title= "The name of the Provider that provides the links", max_length= 300)
|
13
|
totalRelationships:int=Schema(None, title= "The number of links that It provides")
|
14
|
|
15
|
class LinkPublisher(BaseModel):
|
16
|
name:str = Schema(None, title= "The name of the Publisher that provides the links", max_length= 300)
|
17
|
totalRelationships:int=Schema(None, title= "The number of links that It provides")
|
18
|
|
19
|
|
20
|
class IdentifierType(BaseModel):
|
21
|
ID: str = None
|
22
|
IDScheme:str = None
|
23
|
IDURL:str= None
|
24
|
|
25
|
class ScholixProviderType(BaseModel):
|
26
|
name:str = None
|
27
|
identifier:List[IdentifierType] = []
|
28
|
|
29
|
class RelationshipType(BaseModel):
|
30
|
Name:str
|
31
|
SubType:str = None
|
32
|
SubTypeSchema:str = None
|
33
|
|
34
|
class CreatorType(BaseModel):
|
35
|
Name: str
|
36
|
Identifier:List[IdentifierType] = []
|
37
|
|
38
|
|
39
|
|
40
|
class ScholixItemType(BaseModel):
|
41
|
Identifier:IdentifierType = None
|
42
|
Title:str = None
|
43
|
Type:str
|
44
|
Creator:List[CreatorType] = []
|
45
|
PublicationDate:str = None
|
46
|
Publisher:List[ScholixProviderType] =[]
|
47
|
|
48
|
class ScholixType(BaseModel):
|
49
|
HarvestDate:str=None
|
50
|
LicenseURL:str=None
|
51
|
LinkProvider:List[ScholixProviderType] =[]
|
52
|
LinkPublicationDate: str = None
|
53
|
RelationshipType:RelationshipType
|
54
|
source:ScholixItemType
|
55
|
target:ScholixItemType
|
56
|
|
57
|
|
58
|
class PageResultType(BaseModel):
|
59
|
currentPage:int
|
60
|
totalLinks:int
|
61
|
totalPages:int
|
62
|
result:List[ScholixType] = []
|
63
|
|
64
|
|
65
|
def get_scholix_resource(item):
|
66
|
title = ''
|
67
|
if 'title' in item:
|
68
|
title = item.title
|
69
|
if title is not None and len(title):
|
70
|
if title[0] == '"' and title[-1] == '"':
|
71
|
title = title[1:-1]
|
72
|
identifier = [dict(ID=x.identifier, IDScheme=x.schema, IDURL=x.url) for x in
|
73
|
item.identifier]
|
74
|
identifier = identifier[0]
|
75
|
creator = []
|
76
|
if 'creator' in item and item.creator is not None:
|
77
|
creator = [dict(Name=x.name) for x in item.creator]
|
78
|
publicationDate = None
|
79
|
if 'publicationDate' in item:
|
80
|
publicationDate = item.publicationDate
|
81
|
publisher = []
|
82
|
if 'publisher' in item and item.publisher is not None:
|
83
|
publisher = [dict(name= x.name) for x in item.publisher if x.name is not None]
|
84
|
c_type = item.objectType
|
85
|
if item.objectType == 'publication':
|
86
|
c_type = 'literature'
|
87
|
|
88
|
resource = dict(Title=title, Identifier=identifier, Creator=creator, PublicationDate= publicationDate, Publisher = publisher, Type= c_type)
|
89
|
|
90
|
return resource
|
91
|
|
92
|
|
93
|
def convert_response(response):
|
94
|
now = datetime.now()
|
95
|
log = logging.getLogger("scholexplorer")
|
96
|
start = time()
|
97
|
for item in response.hits:
|
98
|
current_item = {'LinkPublicationDate': item.publicationDate, 'HarvestDate': item.publicationDate,
|
99
|
"LinkProvider": []}
|
100
|
for linkProvider in item.linkprovider:
|
101
|
current_item['LinkProvider'].append(ScholixProviderType(name=linkProvider.name,
|
102
|
identifier=[IdentifierType(ID=x.identifier, IDScheme=x.schema) for x in
|
103
|
linkProvider.identifiers]))
|
104
|
|
105
|
rel_sub_type = rels.get(item.relationship.name.lower(), "IsRelatedTo")
|
106
|
current_item['RelationshipType'] = dict(Name=rel_sub_type, SubType=item.relationship.name,
|
107
|
SubTypeSchema=item.relationship.schema)
|
108
|
|
109
|
current_item['source'] = get_scholix_resource(item.source)
|
110
|
current_item['target'] = get_scholix_resource(item.target)
|
111
|
|
112
|
yield current_item
|
113
|
end = time()
|
114
|
log.debug("response converted in {} ms".format(end-start))
|
115
|
|
116
|
def create_response(response, current_page):
|
117
|
log = logging.getLogger("scholexplorer")
|
118
|
if current_page > 9999:
|
119
|
raise HTTPException(status_code=400,detail="MAX NUMBER OF PAGE REACHED")
|
120
|
|
121
|
start = time()
|
122
|
|
123
|
|
124
|
result = {'totalLinks': response.hits.total.value, 'currentPage': current_page /100,
|
125
|
'totalPages': 1 + response.hits.total.value / 100, 'result': []}
|
126
|
result['result'] = convert_response(response)
|
127
|
end = time()
|
128
|
|
129
|
log.debug("response created in {} ms".format(end-start))
|
130
|
return result
|