-
Notifications
You must be signed in to change notification settings - Fork 42
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
Affiliations OpenAIRE OpenOrgs PIC #392
Closed
ptamarit
wants to merge
4
commits into
inveniosoftware:master
from
ptamarit:affiliations-openaire-openorgs-pic
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
42d6323
cli: make the update command work for writers without args
ptamarit 0e1fa04
datastreams: writers: add option to not insert
ptamarit 88ba835
datastreams: move OpenAIREProjectHTTPReader to generic OpenAIREHTTPRe…
ptamarit 9d54a2a
datastreams: affiliations: OpenAIRE transformer and writer adding PIC…
ptamarit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2024 CERN. | ||
# | ||
# Invenio-Vocabularies is free software; you can redistribute it and/or | ||
# modify it under the terms of the MIT License; see LICENSE file for more | ||
# details. | ||
|
||
"""OpenAIRE-related module.""" |
84 changes: 84 additions & 0 deletions
84
invenio_vocabularies/contrib/common/openaire/datastreams.py
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,84 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2024 CERN. | ||
# | ||
# Invenio-Vocabularies is free software; you can redistribute it and/or | ||
# modify it under the terms of the MIT License; see LICENSE file for more | ||
# details. | ||
|
||
"""OpenAIRE-related Datastreams Readers/Writers/Transformers module.""" | ||
|
||
import io | ||
|
||
import requests | ||
|
||
from invenio_vocabularies.datastreams.errors import ReaderError | ||
from invenio_vocabularies.datastreams.readers import BaseReader | ||
|
||
|
||
class OpenAIREHTTPReader(BaseReader): | ||
"""OpenAIRE HTTP Reader returning an in-memory binary stream of the latest OpenAIRE Graph Dataset tar file of a given type.""" | ||
|
||
def __init__(self, origin=None, mode="r", tar_href=None, *args, **kwargs): | ||
"""Constructor.""" | ||
self.tar_href = tar_href | ||
super().__init__(origin, mode, *args, **kwargs) | ||
|
||
def _iter(self, fp, *args, **kwargs): | ||
raise NotImplementedError( | ||
"OpenAIREHTTPReader downloads one file and therefore does not iterate through items" | ||
) | ||
|
||
def read(self, item=None, *args, **kwargs): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remark: this reader still does not do a comparison of the publication date with the last successful run. |
||
"""Reads the latest OpenAIRE Graph Dataset tar file of a given type from Zenodo and yields an in-memory binary stream of it.""" | ||
if item: | ||
raise NotImplementedError( | ||
"OpenAIREHTTPReader does not support being chained after another reader" | ||
) | ||
|
||
if self._origin == "full": | ||
# OpenAIRE Graph Dataset | ||
api_url = "https://zenodo.org/api/records/3516917" | ||
elif self._origin == "diff": | ||
# OpenAIRE Graph dataset: new collected projects | ||
api_url = "https://zenodo.org/api/records/6419021" | ||
else: | ||
raise ReaderError("The --origin option should be either 'full' or 'diff'") | ||
|
||
# Call the signposting `linkset+json` endpoint for the Concept DOI (i.e. latest version) of the OpenAIRE Graph Dataset. | ||
# See: https://github.com/inveniosoftware/rfcs/blob/master/rfcs/rdm-0071-signposting.md#provide-an-applicationlinksetjson-endpoint | ||
headers = {"Accept": "application/linkset+json"} | ||
api_resp = requests.get(api_url, headers=headers) | ||
api_resp.raise_for_status() | ||
|
||
# Extract the Landing page Link Set Object located as the first (index 0) item. | ||
landing_page_linkset = api_resp.json()["linkset"][0] | ||
|
||
# Extract the URL of the only tar file matching `tar_href` linked to the record. | ||
landing_page_matching_tar_items = [ | ||
item | ||
for item in landing_page_linkset["item"] | ||
if item["type"] == "application/x-tar" | ||
and item["href"].endswith(self.tar_href) | ||
] | ||
if len(landing_page_matching_tar_items) != 1: | ||
raise ReaderError( | ||
f"Expected 1 tar item matching {self.tar_href} but got {len(landing_page_matching_tar_items)}" | ||
) | ||
file_url = landing_page_matching_tar_items[0]["href"] | ||
|
||
# Download the matching tar file and fully load the response bytes content in memory. | ||
# The bytes content are then wrapped by a BytesIO to be file-like object (as required by `tarfile.open`). | ||
# Using directly `file_resp.raw` is not possible since `tarfile.open` requires the file-like object to be seekable. | ||
file_resp = requests.get(file_url) | ||
file_resp.raise_for_status() | ||
yield io.BytesIO(file_resp.content) | ||
|
||
|
||
VOCABULARIES_DATASTREAM_READERS = { | ||
"openaire-http": OpenAIREHTTPReader, | ||
} | ||
|
||
VOCABULARIES_DATASTREAM_TRANSFORMERS = {} | ||
|
||
VOCABULARIES_DATASTREAM_WRITERS = {} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nitpick: maintain 2022-2024?