- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1
Added functionality to search for articles using CrossRef #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|  | ||
| 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}" | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest you create a client and type-check the API response using pydantic, the client would look like this:
https://gist.github.com/odarbelaeze/8c9fa5b8735463c94137116af48a73e2
| "networkx~=3.0", | ||
| "typer[all]~=0.9.0", | ||
| "xlsxwriter~=3.2.0", | ||
| "types-requests~=2.32.0.20240622", | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2 things here, you might want to add types request to the dev dependencies, and you'll need to include requests in the actual package dependencies:
diff --git a/pyproject.toml b/pyproject.toml
index 71abf04..845842a 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -18,7 +18,8 @@ dependencies = [
     "networkx~=3.0",
     "typer[all]~=0.9.0",
     "xlsxwriter~=3.2.0",
-    "types-requests~=2.32.0.20240622",
+    "requests~=2.32.3",
+    "pydantic~=2.8.2",
 ]
 
 [project.optional-dependencies]
@@ -27,6 +28,7 @@ dev = [
     "pre-commit~=2.20.0",
     "ruff~=0.3.3",
     "mypy~=1.9.0",
+    "types-requests~=2.32.0.20240622",
 ]
 
 [project.scripts]| 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) | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you type check the author, this will end up looking more like:
diff --git a/src/bibx/_entities/collection_builders/cross_ref.py b/src/bibx/_entities/collection_builders/cross_ref.py
index 181abf3..1b330c7 100644
--- a/src/bibx/_entities/collection_builders/cross_ref.py
+++ b/src/bibx/_entities/collection_builders/cross_ref.py
@@ -1,5 +1,8 @@
-import requests
-
+from bibx._entities.clients.cross_ref import (
+    CrossRefClient,
+    StructuredReference,
+    UnstructuredReference,
+)
 from bibx._entities.collection import Article, Collection
 from bibx._entities.collection_builders.base import CollectionBuilder
 
@@ -14,43 +17,43 @@ class CrossRefCollectionBuilder(CollectionBuilder):
         return self
 
     def build(self) -> 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", [])
+        client = CrossRefClient()
+        works = client.get_works(self._query, self._count)
+        items = works.message.items
 
         articles = []
         for item in items:
-            author_list = item.get("author", [])
+            author_list = item.author
             authors = [
-                f"{author.get('given', '')} {author.get('family', '')}"
-                for author in author_list
+                f"{author.given or ''} {author.family or ''}" 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", [])
+            publication_year = item.published.date_parts[0][0]
+            title = item.title[0]
+            journal = item.container_title[0]
+            volume = item.volume
+            issue = item.issue
+            page = item.page
+            doi = item.doi
+            times_cited = item.is_referenced_by_count
+            reference = item.reference
             references = []
             for ref in reference:
-                if ref.get("unstructured", None) is not None:
-                    unique_reference = Article(title=ref.get("unstructured", None))
-                else:
+                if isinstance(ref, UnstructuredReference):
+                    unique_reference = Article(title=ref.unstructured)
+                    references.append(unique_reference)
+                    continue
+                if isinstance(ref, StructuredReference):
                     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),
+                        authors=[ref.author or ""],
+                        year=ref.year,
+                        title=ref.article_title,
+                        journal=ref.journal_title,
+                        volume=ref.volume,
+                        issue=ref.issue,
+                        page=ref.page,
+                        doi=ref.doi,
                     )
-                references.append(unique_reference)
+                    references.append(unique_reference)
 
             article = Article(
                 authors=authors,| It would be nice to add at least one test for the collection builder. | 
On my machine the tests made by pre-commit gave me the following error:
error: Library stubs not installed for "requests" [import-untyped]. However, I ran the possible solutions and it did not work.Executed Commands:
python3 -m pip install types-requestsmypy --install-type