-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proactively update Prefix.cc (#1056)
References OBOFoundry/OBOFoundry.github.io#1038
- Loading branch information
Showing
3 changed files
with
88 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: Send data to Prefix.cc | ||
on: | ||
workflow_dispatch: | ||
schedule: | ||
- cron: "5 4 * * *" # run at 4:05 in the morning (cron format is minute hour day month day, check https://crontab.guru) | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@master | ||
with: | ||
persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal token | ||
fetch-depth: 0 # otherwise, you will fail to push refs to dest repo | ||
- uses: actions/setup-python@v2 | ||
with: | ||
python-version: 3.12 | ||
|
||
- name: Install dependencies | ||
run: pip install tox tox-uv | ||
|
||
- name: Send a prefix to Prefix.cc | ||
run: tox -e send-prefixcc |
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 @@ | ||
"""Update Prefix.cc to reflect the content of the Bioregistry. | ||
.. seealso:: https://github.com/OBOFoundry/OBOFoundry.github.io/issues/1038 | ||
""" | ||
|
||
import random | ||
|
||
import click | ||
import requests | ||
|
||
import bioregistry | ||
|
||
|
||
def create(curie_prefix: str, uri_prefix: str) -> requests.Response: | ||
"""Send a CURIE prefix/URI prefix to the Prefix.cc "create" endpoint.""" | ||
return requests.post( | ||
f"https://prefix.cc/{curie_prefix}", | ||
data={"create": uri_prefix}, | ||
) | ||
|
||
|
||
def main(): | ||
"""Add an OBO Foundry prefix to Prefix.cc.""" | ||
prefix_cc_map = requests.get("https://prefix.cc/context").json()["@context"] | ||
records = [] | ||
for record in bioregistry.resources(): | ||
if not record.get_obofoundry_prefix(): | ||
continue | ||
uri_prefix = record.get_uri_prefix() | ||
if not uri_prefix: | ||
continue | ||
if uri_prefix == prefix_cc_map.get(record.prefix): | ||
# No need to re-create something that's already | ||
# both available and correct wrt Bioregistry/OBO | ||
continue | ||
records.append(record) | ||
|
||
if not records: | ||
click.echo("No records left to submit to Prefix.cc, good job!") | ||
return | ||
|
||
click.echo(f"{len(records):,} records remain to submit to Prefix.cc") | ||
|
||
# shuffle records to make sure that if there's an error, it doesn't | ||
# lock the update permenantly | ||
random.shuffle(records) | ||
|
||
# Pick only the first record, since we can only make one update per day | ||
record = records[0] | ||
click.echo(f"Attempting to create a record for {record.prefix} {uri_prefix}") | ||
res = create(record.prefix, uri_prefix) | ||
click.echo(res.text) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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