forked from pybliometrics-dev/pybliometrics
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pybliometrics-dev#360 ScienceDirect Object Retrieval with eid
- Loading branch information
1 parent
1817501
commit af361f1
Showing
7 changed files
with
116 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from io import BytesIO | ||
from typing import Literal, Optional, Union | ||
|
||
from pybliometrics.sciencedirect import ArticleRetrieval | ||
from pybliometrics.superclasses import Retrieval | ||
from pybliometrics.utils import ( | ||
chained_get, | ||
check_parameter_value, | ||
detect_id_type, | ||
VIEWS, | ||
) | ||
|
||
class ObjectRetrieval(Retrieval): | ||
@property | ||
def object(self) -> BytesIO: | ||
"""The object retrieved.""" | ||
return self._object | ||
|
||
def __init__(self, | ||
identifier: Union[int, str], | ||
filename: str, | ||
id_type: Optional[str] = None, | ||
refresh: Union[bool, int] = False, | ||
**kwds: str | ||
): | ||
"""Class to retrieve a specific object of a document. | ||
:param identifier: The indentifier of the document. | ||
:param filename: Filename of the object to be retrieved. To get a list of all available | ||
objects of a document (and its corresponding filename) use the class `ObjectMetadata`. | ||
:param id_type: The type of identifier supplied. Allowed values: doi, pii, scopus_id, pubmed_id, eid. | ||
:param refresh: Whether to refresh the cached file if it exists. Default: False. | ||
""" | ||
identifier = str(identifier) | ||
|
||
if id_type is None: | ||
id_type = detect_id_type(identifier) | ||
else: | ||
allowed_id_types = ('doi', 'pii', 'scopus_id', 'pubmed_id', 'eid') | ||
check_parameter_value(id_type, allowed_id_types, "id_type") | ||
|
||
if id_type != 'eid': | ||
identifier = self._get_eid(identifier) | ||
file_identifier = f'{identifier}-{filename}' | ||
|
||
self._view = '' | ||
self._refresh = refresh | ||
|
||
super().__init__(file_identifier, 'ObjectRetrieval', 'eid', **kwds) | ||
|
||
self._object = BytesIO(self._object) | ||
|
||
def _get_eid(self, identifier: str) -> str: | ||
"""Get the EID of a document.""" | ||
am = ArticleRetrieval(identifier, field='eid') | ||
return am.eid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"""Test class ObjectRetrieval""" | ||
import xml.etree.ElementTree as ET | ||
|
||
from io import BytesIO | ||
from PIL import Image | ||
|
||
from pybliometrics.sciencedirect import init, ObjectRetrieval | ||
|
||
init() | ||
|
||
or_1 = ObjectRetrieval('S156984322300331X', | ||
'gr10.jpg', | ||
refresh=30) | ||
or_2 = ObjectRetrieval('10.1016/j.rcim.2020.102086', | ||
'si92.svg', | ||
id_type='doi', | ||
refresh=30) | ||
|
||
|
||
def test_object(): | ||
"""Tests whether the object is a BytesIO object.""" | ||
assert isinstance(or_1.object, BytesIO) | ||
assert isinstance(or_2.object, BytesIO) | ||
|
||
|
||
def test_is_jpg(): | ||
"""Tests whether the object is a JPEG image.""" | ||
obj_1 = or_1.object | ||
with Image.open(obj_1) as img: | ||
assert img.format.lower() == 'jpeg' | ||
|
||
|
||
def test_is_svg(): | ||
"""Tests whether the object is an SVG image.""" | ||
obj_2 = or_2.object | ||
tree = ET.parse(obj_2) | ||
root = tree.getroot() | ||
assert root.tag == '{http://www.w3.org/2000/svg}svg' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters