diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 93303a3..2c18ef3 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,20 +1,21 @@ repos: - - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v3.2.0 - hooks: - - id: trailing-whitespace - - id: end-of-file-fixer - - id: check-yaml - - repo: https://github.com/astral-sh/ruff-pre-commit - # Ruff version. - rev: v0.3.3 - hooks: - # Run the linter. - - id: ruff - args: [ --fix ] - # Run the formatter. - - id: ruff-format - - repo: https://github.com/pre-commit/mirrors-mypy - rev: 'v1.9.0' - hooks: - - id: mypy + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.6.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - repo: https://github.com/astral-sh/ruff-pre-commit + # Ruff version. + rev: v0.5.1 + hooks: + # Run the linter. + - id: ruff + args: [--fix] + # Run the formatter. + - id: ruff-format + - repo: https://github.com/pre-commit/mirrors-mypy + rev: 'v1.10.1' + hooks: + - id: mypy + additional_dependencies: ['types-requests'] diff --git a/pyproject.toml b/pyproject.toml index 1f5c3d0..71abf04 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,6 +18,7 @@ dependencies = [ "networkx~=3.0", "typer[all]~=0.9.0", "xlsxwriter~=3.2.0", + "types-requests~=2.32.0.20240622", ] [project.optional-dependencies] diff --git a/src/bibx/_entities/collection_builders/cross_ref.py b/src/bibx/_entities/collection_builders/cross_ref.py index 8cb9e40..181abf3 100644 --- a/src/bibx/_entities/collection_builders/cross_ref.py +++ b/src/bibx/_entities/collection_builders/cross_ref.py @@ -1,4 +1,6 @@ -from bibx._entities.collection import Collection +import requests + +from bibx._entities.collection import Article, Collection from bibx._entities.collection_builders.base import CollectionBuilder @@ -12,4 +14,56 @@ def with_count(self, count: int): return self def build(self) -> Collection: - return Collection([]) + url = f"https://api.crossref.org/works?query={self._query.lower().replace(' ', '+')}&filter=has-orcid:true,type:journal-article,has-references:true,from-pub-date:2003-01-01&rows={self._count}" + response = requests.get(url) + data = response.json() + items = data.get("message", {}).get("items", []) + + articles = [] + for item in items: + author_list = item.get("author", []) + authors = [ + f"{author.get('given', '')} {author.get('family', '')}" + for author in author_list + ] + publication_year = item.get("published").get("date-parts", [[2000]])[0][0] + title = item.get("title", None)[0] + journal = item.get("container-title", None)[0] + volume = item.get("volume", None) + issue = item.get("issue", None) + page = item.get("page", None) + doi = item.get("DOI", None) + times_cited = item.get("is-referenced-by-count", 0) + reference = item.get("reference", []) + references = [] + for ref in reference: + if ref.get("unstructured", None) is not None: + unique_reference = Article(title=ref.get("unstructured", None)) + else: + unique_reference = Article( + authors=[ref.get("author", [])], + year=ref.get("year", None), + title=ref.get("article-title", None), + journal=ref.get("journal-title", None), + volume=ref.get("volume", None), + issue=ref.get("issue", None), + page=ref.get("page", None), + doi=ref.get("DOI", None), + ) + references.append(unique_reference) + + article = Article( + authors=authors, + year=publication_year, + title=title, + journal=journal, + volume=volume, + issue=issue, + page=page, + doi=doi, + times_cited=times_cited, + references=references, + ) + articles.append(article) + + return Collection(articles) diff --git a/src/bibx/_entities/collection_builders/scopus_ris.py b/src/bibx/_entities/collection_builders/scopus_ris.py index 65f00ba..4597ae0 100644 --- a/src/bibx/_entities/collection_builders/scopus_ris.py +++ b/src/bibx/_entities/collection_builders/scopus_ris.py @@ -177,7 +177,7 @@ def _article_from_record(cls, record: str) -> Article: @classmethod def _parse_file(cls, file: TextIO) -> Iterable[Article]: if not _size(file): - return [] + yield from [] for item in file.read().split("\n\n"): if item.isspace(): continue