full import scripts
This commit is contained in:
parent
4d74913447
commit
34c9b1c0a9
136 changed files with 289629 additions and 10 deletions
86
98__r__importArtistToMentionedRelation.py
Normal file
86
98__r__importArtistToMentionedRelation.py
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
import uuid # For UUID creation
|
||||
from initDb import initDb # For database initialization
|
||||
from wisski.api import Api, Pathbuilder, Entity # For WissKI API
|
||||
import os # For environment variable loading
|
||||
from dotenv import load_dotenv # For environment variable loading
|
||||
import pandas as pd # For dataframe handling
|
||||
|
||||
# Initialize the database
|
||||
print('Initializing the database...')
|
||||
engine, metadata, Session = initDb(True, './schemas/')
|
||||
if engine == False:
|
||||
print('Database initialization failed.')
|
||||
exit()
|
||||
|
||||
# Load the environment variables
|
||||
load_dotenv()
|
||||
|
||||
# Initialize the WissKI API
|
||||
print('Initializing the WissKI API...')
|
||||
api_url = os.getenv('API_URL')
|
||||
auth = (os.getenv('API_USERNAME'), os.getenv('API_PASSWORD'))
|
||||
headers = {"Cache-Control": "no-cache"}
|
||||
api = Api(api_url, auth, headers)
|
||||
api.pathbuilder = api.get_pathbuilder('relations')
|
||||
|
||||
test = False
|
||||
|
||||
tableName = "r__kue__7060_erwaehnt__datum_"
|
||||
bundleId = 'bc2b0ddca583320a56a67b304dc0a045' # Artist to mentioned relation
|
||||
|
||||
try:
|
||||
processedRows = pd.read_csv(f'./logs/processed-{tableName}.csv')
|
||||
except FileNotFoundError:
|
||||
processedRows = pd.DataFrame(columns=['docId', 'uuid', 'uri'])
|
||||
|
||||
# Load sources table
|
||||
sqlTable = pd.read_sql_table(tableName, con=engine)
|
||||
|
||||
entityValues = {}
|
||||
|
||||
# Create entities
|
||||
for index, row in sqlTable.iterrows():
|
||||
# For every row in table...
|
||||
if index < len(processedRows) and sqlTable.loc[index, 'id'] == processedRows.loc[index, 'docId']:
|
||||
# skip if already processed
|
||||
print(f'Skipping already processed entity {sqlTable.loc[index, 'id']}')
|
||||
continue
|
||||
# Create Entity property dicts
|
||||
entityValues = {}
|
||||
for key, value in row.items():
|
||||
# For every column in row...
|
||||
if (value is None) or (value == ''):
|
||||
# skip if cell has no value
|
||||
continue
|
||||
# Properties of an entity have to be an array, so...
|
||||
if '&' in str(value):
|
||||
# ...Explode "&"-separated values to array items
|
||||
value = [x.strip() for x in str(value).split('&')]
|
||||
else:
|
||||
# ...Or parse to array
|
||||
value = [value]
|
||||
# Map columns to fields. We use assignments for reification.
|
||||
docId = ''
|
||||
match key:
|
||||
case 'id':
|
||||
docId = value[0]
|
||||
case 'f__kue__uuid':
|
||||
entityValues['f47b1ffe8394f389497b9e23407ad72f'] = value # Date
|
||||
fUuid = value[0]
|
||||
case 'f__7060_erwaehnt__datum___uuid':
|
||||
entityValues['fabb90d487512fc5bf8d7379ff2d8bdb'] = value # Mentioned UUID
|
||||
case _:
|
||||
print(f'{key} is not a valid field, skipping.')
|
||||
|
||||
# Create Material
|
||||
entity = Entity(api=api, fields=entityValues, bundle_id=bundleId)
|
||||
api.save(entity)
|
||||
|
||||
print(f'Created entity {index}: {entity.uri} of {len(sqlTable)}')
|
||||
|
||||
# Write log
|
||||
processedRows = processedRows._append({'docId': docId, 'uuid': fUuid, 'uri': entity.uri}, ignore_index=True)
|
||||
processedRows.to_csv(f'./logs/processed-{tableName}.csv', index=False)
|
||||
if test:
|
||||
exit()
|
||||
print('finish')
|
||||
Loading…
Add table
Add a link
Reference in a new issue