Skip to content

Commit

Permalink
Merge pull request #105 from MaRDI4NFDI/OpenAire_Dev
Browse files Browse the repository at this point in the history
Open aire dev
  • Loading branch information
Shirazos7 authored Oct 18, 2024
2 parents 1936ce7 + 7676414 commit 0ea6666
Show file tree
Hide file tree
Showing 5 changed files with 787 additions and 489 deletions.
150 changes: 76 additions & 74 deletions src/zbmath_rest2oai/restApi_software_Json.py
Original file line number Diff line number Diff line change
@@ -1,74 +1,76 @@
import json

import requests

from swhid import parse_csv_to_dict


# Main function to handle the API request, CSV parsing, and JSON modification
def process_metadata(csv_file_path, api_url, output_path=None):
# Extract the swmathid from the API URL
swmathid = int(api_url.split('/')[-1]) # This extracts the number after 'software/'

# Make a GET request to the API
response = requests.get(api_url)

# Check if the request was successful (status code 200)
if response.status_code == 200:
# Parse the response JSON content
data = response.json()

# Parse the CSV and get the dictionary of swmathid to swhid
swmathid_to_swhid = parse_csv_to_dict(csv_file_path)

# Prepare the output log
output_log = []

# Check if the swmathid from the API exists in the CSV mapping
if swmathid in swmathid_to_swhid:
matched_swhid = swmathid_to_swhid[swmathid]

# Modify the JSON structure with the matched swhid
swhdeposit_data = {
"swhdeposit:deposit": {
"swhdeposit:reference": {
"swhdeposit:object": {
"@swhid": matched_swhid
}
},
"swhdeposit:metadata-provenance": {
"schema:url": "https://staging.swmath.org/" # Split or modify the URL as needed
}
}
}

# Merge this into the original JSON
data.update(swhdeposit_data)

# Add a log entry for success
output_log.append(f"swmathid {swmathid} matched to swhid: {matched_swhid}")
else:
# If no match is found
output_log.append(f"swmathid {swmathid} not found in the CSV file.")

# Save the modified JSON data to a file
output_json_filename = 'output_with_swhid.json'
with open(output_path, 'w') as json_file:
json.dump(data, json_file, indent=4, ensure_ascii=False)
print(f"Data successfully saved to {output_json_filename}")

# Save the output log to a file
# output_log_filename = 'process_log.txt'
with open(output_log_filename, 'w') as log_file:
for log_entry in output_log:
log_file.write(log_entry + '\n')
print(f"Log successfully saved to {output_log_filename}")
else:
print(f"Failed to retrieve data. Status code: {response.status_code}")


csv_file_path = '../../test/data/software/swh_swmath.csv' # Path to your CSV file
api_url = 'https://api.zbmath.org/v1/software/825' # API URL, can be generalized for any software ID
output_path = '../../test/data/software/software_with_swhid.json'
output_log_filename = '../../test/data/software/logfile.txt'
process_metadata(csv_file_path, api_url, output_path)

import json

import requests

from swhid import parse_csv_to_dict


# Main function to handle the API request, CSV parsing, and JSON modification
def process_metadata(csv_file_path, api_url, output_path=None):
# Extract the swmathid from the API URL
swmathid = int(api_url.split('/')[-1]) # This extracts the number after 'software/'

# Make a GET request to the API
response = requests.get(api_url)

# Check if the request was successful (status code 200)
if response.status_code == 200:
# Parse the response JSON content
data = response.json()

# Parse the CSV and get the dictionary of swmathid to swhid
swmathid_to_swhid = parse_csv_to_dict(csv_file_path)

# Prepare the output log
output_log = []

# Check if the swmathid from the API exists in the CSV mapping
if swmathid in swmathid_to_swhid:
matched_swhid = swmathid_to_swhid[swmathid]

# Modify the JSON structure with the matched swhid
swhdeposit_data = {
"swhdeposit:deposit": {
"swhdeposit:reference": {
"swhdeposit:object": {
"@swhid": matched_swhid
}
},
"swhdeposit:metadata-provenance": {
"schema:url": "https://staging.swmath.org/" # Split or modify the URL as needed
}
}
}

# Merge this into the original JSON
data.update(swhdeposit_data)

# Add a log entry for success
output_log.append(f"swmathid {swmathid} matched to swhid: {matched_swhid}")
else:
# If no match is found
output_log.append(f"swmathid {swmathid} not found in the CSV file.")

# Save the modified JSON data to a file
output_json_filename = 'output_with_swhid.json'
with open(output_path, 'w') as json_file:
json.dump(data, json_file, indent=4, ensure_ascii=False)
print(f"Data successfully saved to {output_json_filename}")

# Save the output log to a file
# output_log_filename = 'process_log.txt'
with open(output_log_filename, 'w') as log_file:
for log_entry in output_log:
log_file.write(log_entry + '\n')
print(f"Log successfully saved to {output_log_filename}")
else:
print(f"Failed to retrieve data. Status code: {response.status_code}")


csv_file_path = '../../test/data/software/swh_swmath.csv' # Path to your CSV file
api_url = 'https://api.zbmath.org/v1/software/825' # API URL, can be generalized for any software ID
output_path = '../../test/data/software/software_with_swhid.json'
output_log_filename = '../../test/data/software/logfile.txt'
process_metadata(csv_file_path, api_url, output_path)

Loading

0 comments on commit 0ea6666

Please sign in to comment.