def make_documents_broad_match(document_ids): """If a work has more than one score, the documents are broadMatches of each other. We should create relations between *all permutations* of these documents.""" for from_id, to_id in itertools.permutations(document_ids, 2): query = cequery.document.get_mutation_merge_document_broad_match( from_id, to_id) connection.submit_query(query)
def import_or_update_work(work, artist_data): source = work["Source"] title = work["Title"] logger.info("Adding work: %s", title) existing_work = connection.get_music_composition_by_source(source) if len(existing_work) == 0: query = cequery.work.transform_data_create_composition(work) resp = connection.submit_query(query) music_composition = resp["data"]["CreateMusicComposition"] logger.info(" done") elif len(existing_work) == 1: logger.info(" already exists, updating") existing_work = existing_work[0] identifier = existing_work["identifier"] query = cequery.work.transform_data_update_composition( identifier, work) resp = connection.submit_query(query) music_composition = resp["data"]["UpdateMusicComposition"] else: raise Exception( "Unexpectedly got more than 1 entry for a given source") work_identifier = music_composition["identifier"] logger.info(" work id %s", work_identifier) composer = _get_composer_data(work, artist_data) composer_identifer = import_or_update_composer(composer) join_work_composer(work_identifier, composer_identifer) add_documents(work_identifier, work)
def join_work_and_documents(work_identifier, document_identifiers): """Mark that a score is the subjectOf a work""" for d_id in document_identifiers: query = cequery.document.get_query_remove_document_composition( work_identifier, d_id) connection.submit_query(query) query = cequery.document.get_query_add_document_composition( work_identifier, d_id) connection.submit_query(query)
def join_work_composer(work_identifier, composer_identifier): """Say that a composer wrote a score""" logger.info("joining work %s and composer %s", work_identifier, composer_identifier) # Remove any link that is already there (We'd have to do a query anyway to check if it exists, # so just do it unconditionally) # TODO: Could select this query when getting the work originally to check if we update it query = cequery.work.get_mutation_merge_composition_author( work_identifier, composer_identifier) connection.submit_query(query) logger.info("done")
def add_or_get_digital_document(document): logger.info("Adding document") query_resp = connection.get_digital_document_by_source(document["source"]) if len(query_resp) == 0: logger.info(" doesn't exist, adding") query = cequery.document.transform_data_create_document(document) resp = connection.submit_query(query) resp_document = resp["data"]["CreateDigitalDocument"] elif len(query_resp) == 1: logger.info(" exists") resp_document = query_resp[0] identifier = resp_document["identifier"] logger.info(" document id %s", identifier) return identifier
def import_or_update_composer(composer): """Import metdata for a composer from a datafile""" logger.info("Adding composer: %s", composer["name"]) existing_composer = connection.get_person_by_source(composer["source"]) if len(existing_composer) == 0: logger.info(" doesn't exist, adding") query = cequery.person.transform_data_artist(composer) resp = connection.submit_query(query) person = resp["data"]["CreatePerson"] elif len(existing_composer) == 1: logger.info(" exists, getting") person = existing_composer[0] else: logger.error( "Unexpectedly got more than 1 composer for a given source") identifier = person["identifier"] logger.info(" composer id: %s", identifier) return identifier
def join_work_and_documents(work_identifier, document_identifiers): """Mark that a score is a workExample a work""" for d_id in document_identifiers: query = cequery.document.get_mutation_merge_document_composition( work_identifier, d_id) connection.submit_query(query)
def import_work(work_mbid): work = mb.get_artist_by_id(work_mbid, includes=["url-rels"])["work"] query = person.transform_work(work) print(work) submit_query(query)
def import_artist(artist_mbid): artist = mb.get_artist_by_id(artist_mbid, includes=["url-rels"])["artist"] query = artist.transform_musicbrainz_artist(artist) submit_query(query)