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 def importArtistAssignment(api, engine): print('Importing artist assignment...') tableName = "c__ob30_bez_kuenstler" bundleId = 'bc8826cc7d9c9373ce71cfc0251c2a4f' try: processedRows = pd.read_csv(f'./logs/{tableName}.csv') except FileNotFoundError: processedRows = pd.DataFrame(columns=['id', 'uuid', 'uri']) # Load sources table artistRelationsTable = pd.read_sql_table(tableName, con=engine) # Create artistRelations for index, row in artistRelationsTable.iterrows(): # For every row in table... if index < len(processedRows) and artistRelationsTable.loc[index, 'id'] == processedRows.loc[index, 'id']: # skip if already processed print(f'Skipping already processed artistAssignment {artistRelationsTable.loc[index, "id"]}') continue # Create Entity property dicts artistRelationValues = {} 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... value = str(value).replace('&###{{new_line}}###'.format(), '&') value = str(value).replace('###{{new_line}}###', '&') value = str(value).replace(' & ', '&') 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. match key: case 'id': docId = value[0] case 'f__uuid': artistRelationValues['fc150259d31fea8a3f992e7beb901fa4'] = value # UUID case 'f__3100_name': artistRelationValues['ff5bf58133f9351d03e2ee92b6f8bb7e'] = value # Artist Name case 'f__3475_ber__funkt_': artistRelationValues['fc0c7d8c6b736489210bc42ef0f1406a'] = value # Occupation case 'f__ob30_bez_kuenstler': artistRelationValues['f575d4f2c8ea5d37618cea708c2a7c5e'] = value # Relation case _: print(f'{key} is not a valid field, skipping.') artistRelation = Entity(api=api, fields=artistRelationValues, bundle_id=bundleId) api.save(artistRelation) print(f'Created artist assignment {index}: {artistRelation.uri} of {len(artistRelationsTable)}') # Write log processedRows = processedRows._append({'id': row['id'], 'uuid': artistRelationValues['fc150259d31fea8a3f992e7beb901fa4'][0], 'uri': artistRelation.uri}, ignore_index=True) processedRows.to_csv(f'./logs/{tableName}.csv', index=False) print('finished importing artist assignment')