|
| 1 | +"""Module with the PublicationLookup class.""" |
| 2 | +from collections import namedtuple |
| 3 | +from typing import Union, Optional |
| 4 | + |
| 5 | +from pybliometrics.superclasses import Retrieval |
| 6 | +from pybliometrics.utils import make_int_if_possible, chained_get |
| 7 | + |
| 8 | + |
| 9 | +class PublicationLookup(Retrieval): |
| 10 | + |
| 11 | + @property |
| 12 | + def id(self) -> Optional[int]: |
| 13 | + """ID of the document (same as EID without "2-s2.0-").""" |
| 14 | + return make_int_if_possible(chained_get(self._json, ['publication', 'id'])) |
| 15 | + |
| 16 | + @property |
| 17 | + def title(self) -> Optional[str]: |
| 18 | + """Publication title.""" |
| 19 | + return chained_get(self._json, ['publication', 'title']) |
| 20 | + |
| 21 | + @property |
| 22 | + def doi(self) -> Optional[str]: |
| 23 | + """Digital Object Identifier (DOI).""" |
| 24 | + return chained_get(self._json, ['publication', 'doi']) |
| 25 | + |
| 26 | + @property |
| 27 | + def type(self) -> Optional[str]: |
| 28 | + """Type of publication.""" |
| 29 | + return chained_get(self._json, ['publication', 'type']) |
| 30 | + |
| 31 | + @property |
| 32 | + def publication_year(self) -> Optional[int]: |
| 33 | + """Year of publication.""" |
| 34 | + return make_int_if_possible(chained_get(self._json, ['publication', 'publicationYear'])) |
| 35 | + |
| 36 | + @property |
| 37 | + def citation_count(self) -> Optional[int]: |
| 38 | + """Count of citations.""" |
| 39 | + return make_int_if_possible(chained_get(self._json, ['publication', 'citationCount'])) |
| 40 | + |
| 41 | + @property |
| 42 | + def source_title(self) -> Optional[str]: |
| 43 | + """Title of source.""" |
| 44 | + return chained_get(self._json, ['publication', 'sourceTitle']) |
| 45 | + |
| 46 | + @property |
| 47 | + def topic_id(self) -> Optional[int]: |
| 48 | + """Topic id.""" |
| 49 | + return make_int_if_possible(chained_get(self._json, ['publication', 'topicId'])) |
| 50 | + |
| 51 | + @property |
| 52 | + def topic_cluster_id(self) -> Optional[int]: |
| 53 | + """Topic cluster id.""" |
| 54 | + return make_int_if_possible(chained_get(self._json, ['publication', 'topicClusterId'])) |
| 55 | + |
| 56 | + @property |
| 57 | + def link(self) -> Optional[str]: |
| 58 | + """URL link.""" |
| 59 | + return chained_get(self._json, ['link', '@href']) |
| 60 | + |
| 61 | + @property |
| 62 | + def authors(self) -> Optional[list[namedtuple]]: |
| 63 | + """Publication authors.""" |
| 64 | + out = [] |
| 65 | + fields = 'id name link' |
| 66 | + auth = namedtuple('Author', fields) |
| 67 | + for item in chained_get(self._json, ['publication', 'authors'], []): |
| 68 | + new = auth(id=make_int_if_possible(item['id']), name=item.get('name'), |
| 69 | + link=chained_get(item, ['link', '@href'])) |
| 70 | + out.append(new) |
| 71 | + return out or None |
| 72 | + |
| 73 | + @property |
| 74 | + def institutions(self) -> Optional[list[namedtuple]]: |
| 75 | + """Institutions linked to publication authors.""" |
| 76 | + out = [] |
| 77 | + fields = 'id name country country_code link' |
| 78 | + auth = namedtuple('Institution', fields) |
| 79 | + for item in chained_get(self._json, ['publication', 'institutions'], []): |
| 80 | + new = auth(id=make_int_if_possible(item['id']), name=item.get('name'), country=item.get('country'), |
| 81 | + country_code=item.get('countryCode'), link=chained_get(item, ['link', '@href'])) |
| 82 | + out.append(new) |
| 83 | + return out or None |
| 84 | + |
| 85 | + @property |
| 86 | + def sdgs(self) -> Optional[list[str]]: |
| 87 | + """List of Sustainable Development Goals (SDG).""" |
| 88 | + return chained_get(self._json, ['publication', 'sdg']) |
| 89 | + |
| 90 | + def __str__(self): |
| 91 | + """Print a summary string.""" |
| 92 | + authors = ', '.join(a.name for a in self.authors) if self.authors else "N/A" |
| 93 | + institutions = ', '.join(i.name for i in self.institutions) if self.institutions else "N/A" |
| 94 | + sdgs = ', '.join(self.sdgs) if self.sdgs else "N/A" |
| 95 | + s = (f"Publication Summary:\n" |
| 96 | + f"- ID: {self.id or 'N/A'}\n" |
| 97 | + f"- Title: {self.title or 'N/A'}\n" |
| 98 | + f"- DOI: {self.doi or 'N/A'}\n" |
| 99 | + f"- Type: {self.type or 'N/A'}\n" |
| 100 | + f"- Year: {self.publication_year or 'N/A'}\n" |
| 101 | + f"- Citation Count: {self.citation_count or 'N/A'}\n" |
| 102 | + f"- Source Title: {self.source_title or 'N/A'}\n" |
| 103 | + f"- Topic ID: {self.topic_id or 'N/A'}\n" |
| 104 | + f"- Topic Cluster ID: {self.topic_cluster_id or 'N/A'}\n" |
| 105 | + f"- Link: {self.link or 'N/A'}\n" |
| 106 | + f"- Authors: {authors}\n" |
| 107 | + f"- Institutions: {institutions}\n" |
| 108 | + f"- SDGs: {sdgs}\n") |
| 109 | + return s |
| 110 | + |
| 111 | + def __init__(self, identifier: int = None, refresh: Union[bool, int] = False, **kwds: str) -> None: |
| 112 | + """Interaction with the Publication Lookup API. |
| 113 | + :param identifier: The Scopus ID of the object. |
| 114 | + :param refresh: Whether to refresh the cached file if it exists. Default: `False`. |
| 115 | + :param kwds: Keywords passed on to requests header. Must contain |
| 116 | + fields and values specified in the respective |
| 117 | + API specification. |
| 118 | + """ |
| 119 | + self._view = '' |
| 120 | + self._refresh = refresh |
| 121 | + Retrieval.__init__(self, identifier=str(identifier), **kwds) |
0 commit comments