-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a917be5
commit ab6c036
Showing
1 changed file
with
53 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,53 @@ | ||
import asyncio | ||
import json | ||
import os | ||
|
||
import aiohttp | ||
from aiohttp import ClientSession | ||
|
||
from zbmath_rest2oai.get_sets import get_sets | ||
|
||
AUTH = aiohttp.BasicAuth('swmath', os.environ.get('OAI_BASIC_PASSWORD')) | ||
|
||
URL = "https://oai-input.portal.mardi4nfdi.de/oai-backend/set/" | ||
|
||
|
||
async def post_item(session, data, set_name='unknown'): | ||
async with session.post(URL, | ||
data=data, | ||
auth=AUTH, | ||
headers={"Content-Type": "application/json"}) as response: | ||
if response.status not in [200, 409]: | ||
raise Exception(f"Unexpected response with status code {response.status} for set {set_name}:" | ||
f" {await response.text()}") | ||
return response | ||
|
||
|
||
async def async_write_oai(sets): | ||
tasks = [] | ||
|
||
async with ClientSession() as session: | ||
last_id = -1 | ||
records = 0 | ||
for identifier in sets.keys(): | ||
records += 1 | ||
last_id = identifier | ||
data = {'name': identifier, | ||
'spec': identifier, | ||
'tags': [identifier], | ||
'description': sets[identifier], | ||
} | ||
|
||
tasks.append(post_item(session, json.dumps(data), identifier)) | ||
|
||
await asyncio.gather(*tasks) | ||
|
||
return records, last_id | ||
|
||
|
||
def write_oai_sets(): | ||
asyncio.run(async_write_oai(get_sets(), )) | ||
|
||
|
||
if __name__ == '__main__': | ||
write_oai_sets() |