예제 #1
0
 def test_invalid_format(self):
     with pytest.raises(NotAMimeTypeException):
         person.mutation_update_person(
             '2eeca6dd-c62c-490e-beb0-2e3899fca74f', format_="test,html")
     with pytest.raises(NotAMimeTypeException):
         person.mutation_create_person(
             title="A. J. Fynn",
             contributor="https://www.cpdl.org",
             creator="https://www.upf.edu",
             source="https://www.cpdl.org/wiki/index.php/A._J._Fynn",
             language="en",
             format_="text,html",
             description="Born circa 1860Died circa 1920A. J. Fynn was\
                                     an early 20th Century scholar in literature and anthropology"
         )
예제 #2
0
async def import_artist(keys: list):
    """
    Imports artists from Muziekweb for all given keys into the Trompa CE.
    """
    for key in keys:
        print(f"Retrieving artist with key {key} from Muziekweb")
        # Get data from Muziekweb
        artist = await get_mw_artist(key)

        if artist is None:
            print(f"No data received for {key}")
            continue

        artist.identifier = await lookupIdentifier("Person", artist.source)

        if artist.identifier is not None:
            print(f"Updating record {artist.identifier} in Trompa CE", end="")
            response = await ce.connection.submit_query(
                mutation_update_person(**artist.as_dict()))
            artist.identifier = response["data"]["UpdatePerson"]["identifier"]
        else:
            print("Inserting new record in Trompa CE", end="")
            response = await ce.connection.submit_query(
                mutation_create_person(**artist.as_dict()))
            artist.identifier = response["data"]["CreatePerson"]["identifier"]

        if artist.identifier is None:
            print(" - failed.")
        else:
            print(" - success.")

    print("Importing artists done.")
예제 #3
0
    def test_create_invalid_values(self):
        """Passing invalid values to language, format_, or gender cause exceptions"""
        with pytest.raises(ValueError):
            person.mutation_create_person(
                title="A. J. Fynn",
                contributor="https://www.cpdl.org",
                creator="https://www.upf.edu",
                source="https://www.cpdl.org/wiki/index.php/A._J._Fynn",
                format_="text/html",
                gender="test")

        with pytest.raises(UnsupportedLanguageException):
            person.mutation_create_person(
                title="A. J. Fynn",
                contributor="https://www.cpdl.org",
                creator="https://www.upf.edu",
                source="https://www.cpdl.org/wiki/index.php/A._J._Fynn",
                format_="text/html",
                language="pt")

        with pytest.raises(NotAMimeTypeException):
            person.mutation_create_person(
                title="A. J. Fynn",
                contributor="https://www.cpdl.org",
                creator="https://www.upf.edu",
                source="https://www.cpdl.org/wiki/index.php/A._J._Fynn",
                format_="html")
예제 #4
0
    def test_create(self):
        expected = self.read_file(
            os.path.join(self.data_dir, "create_person.txt"))

        created_person = person.mutation_create_person(
            title="A. J. Fynn",
            contributor="https://www.cpdl.org",
            creator="https://www.upf.edu",
            source="https://www.cpdl.org/wiki/index.php/A._J._Fynn",
            language="en",
            format_="text/html",
            gender="male",
            description=
            "Born circa 1860Died circa 1920A. J. Fynn was an early 20th Century scholar\
 in literature and anthropology")
        self.assert_queries_equal(created_person, expected)
예제 #5
0
def create_person(person):
    """Create a person object
    Arguments:
        person: a dictionary where keys are the parameters to the `mutation_create_person` function

    If `person` includes the keys 'birthplace' or 'deathplace', these items are extracted out,
    used to create a Place object, and then linked to the person
    """
    person["creator"] = CREATOR_URL

    birthplace = None
    if 'birthplace' in person:
        birthplace = person['birthplace']
        del person['birthplace']
    deathplace = None
    if 'deathplace' in person:
        deathplace = person['deathplace']
        del person['deathplace']

    mutation_create = mutation_person.mutation_create_person(**person)
    resp = connection.submit_request(mutation_create)
    # TODO: If this query fails?
    person_id = resp['data']['CreatePerson']['identifier']

    if birthplace:
        birthplace["creator"] = CREATOR_URL
        mutation_create = mutation_place.mutation_create_place(**birthplace)
        resp = connection.submit_request(mutation_create)
        birthplace_id = resp['data']['CreatePlace']['identifier']
        mutation_merge = mutation_place.mutation_merge_person_birthplace(
            person_id, birthplace_id)
        connection.submit_request(mutation_merge)

    if deathplace:
        deathplace["creator"] = CREATOR_URL
        mutation_create = mutation_place.mutation_create_place(**deathplace)
        resp = connection.submit_request(mutation_create)
        deathplace_id = resp['data']['CreatePlace']['identifier']
        mutation_merge = mutation_place.mutation_merge_person_deathplace(
            person_id, deathplace_id)
        connection.submit_request(mutation_merge)

    return person_id
예제 #6
0
def main(print_queries: bool, submit_queries: bool):
    mutation_musicbrainz = person.mutation_create_person(
        # These 5 fields are required:
        # Who created the item in the CE, either a link to the version of the software,
        #  or if not possible, https://trompamusic.eu
        creator="https://github.com/trompamusic/trompa-ce-client/tree/v0.1/demo",
        # Who this data came from
        contributor="https://musicbrainz.org",
        # The specific page that this data came from
        source="https://musicbrainz.org/artist/8d610e51-64b4-4654-b8df-064b0fb7a9d9",
        # The mimetype of `source`
        format_="text/html",
        # the html <title> of `source`
        title="Gustav Mahler - MusicBrainz",
        # The following fields are optional - fill them in if they are available
        name="Gustav Mahler",
        birth_date="1860-07-07",
        death_date="1911-05-18",
        family_name="Mahler",
        given_name="Gustav",
        gender="male",
        # The language of `source`
        language="en"
    )

    description = """<b>Gustav Mahler</b> (7 July 1860 – 18 May 1911) was an Austro-Bohemian Romantic composer, and one
     of the leading conductors of his generation. As a composer he acted as a bridge between the 19th century 
     Austro-German tradition and the modernism of the early 20th century. While in his lifetime his status as a 
     conductor was established beyond question, his own music gained wide popularity only after periods of relative 
     neglect, which included a ban on its performance in much of Europe during the Nazi era. After 1945 his 
     compositions were rediscovered by a new generation of listeners; Mahler then became one of the most frequently 
     performed and recorded of all composers, a position he has sustained into the 21st century. In 2016, 
     a BBC Music Magazine survey of 151 conductors ranked three of his symphonies in the top ten symphonies of 
     all time."""

    mutation_wikidata = person.mutation_create_person(
        creator="https://github.com/trompamusic/trompa-ce-client/tree/v0.1/demo",
        contributor="https://www.wikidata.org",
        source="https://www.wikidata.org/wiki/Q7304",
        format_="text/html",
        title="Gustav Mahler - Wikidata",
        name="Gustav Mahler",
        birth_date="1860-07-07",
        death_date="1911-05-18",
        family_name="Mahler",
        given_name="Gustav",
        gender="male",
        language="en",
        # From wikidata, language en, we use Wikipedia's extract API to get the first paragraph
        description=description,
        # The first image linked from wikidata
        image="https://upload.wikimedia.org/wikipedia/commons/b/b7/Gustav-Mahler-Kohut.jpg"
    )

    # A more basic record with just required fields
    mutation_viaf = person.mutation_create_person(
        creator="https://github.com/trompamusic/trompa-ce-client/tree/v0.1/demo",
        contributor="https://viaf.org",
        source="https://viaf.org/viaf/61732497/",
        format_="text/html",
        title="61732497"
    )

    print("\nPerson - Musicbrainz\n")
    if print_queries:
        print(mutation_musicbrainz)
    if submit_queries:
        pass

    print("\nPerson - Wikidata\n")
    if print_queries:
        print(mutation_wikidata)
    if submit_queries:
        pass

    print("\nPerson - VIAF\n")
    if print_queries:
        print(mutation_viaf)
    if submit_queries:
        pass

    # skos:exactMatch
    # These three Person objects refer to the same person, therefore we should join them together.
    # We use the relation skos:exactMatch, created with the MergePersonExactMatch mutation.
    # We use the Merge variant to ensure that we don't create the same relationship more than once.
    # Explicitly create this link in all directions between all objects

    # TODO: Ideally we should submit the above queries, get real ids, and then use them in this query
    person_identifiers = ["1fc48b62-0b6d-4f43-8ed5-e1c3280c6530", "a911036d-0549-41d8-86cd-d756dfa1d8a4",
                          "8e706d4b-6a70-46e6-8613-9db63d3f8d57"]

    print("\nPerson - Person exactMatch\n")
    for fr, to in itertools.permutations(person_identifiers, 2):
        mutation_match = person.mutation_person_add_exact_match_person(fr, to)
        if print_queries:
            print(mutation_match)
        if submit_queries:
            pass
async def import_tracks(key: str):
    """
    Imports audio fragments from Muziekweb for the key into the Trompa CE.
    """
    print(f"Retrieving release info with key {key} from Muziekweb")
    # Get data from Muziekweb
    tracks, music_works, persons = get_mw_audio_1track(key)
    # tracks = get_mw_audio(key)

    if tracks is None or len(tracks) == 0:
        print(f"No track data received for {key}")
        return

    #####################################
    # PERSONS
    # Loop the persons on all external links to add references for each CE_person
    #####################################
    list_person_ids = list()
    for person in persons:

        person.identifier = await lookupIdentifier("Person", person.source)

        if person.identifier is not None:
            print(f"Updating person {person.identifier} in Trompa CE\n")

            response = await ce.connection.submit_query_async(
                mutation_update_person(**person.as_dict()))
            person.identifier = response["data"]["UpdatePerson"]["identifier"]
            list_person_ids.append(person.identifier)
        else:
            print("Inserting new person {} in Trompa CE\n".format(person.name))

            response = await ce.connection.submit_query_async(
                mutation_create_person(**person.as_dict()))

            person.identifier = response["data"]["CreatePerson"]["identifier"]
            list_person_ids.append(person.identifier)

    print(f"Importing Persons for {key} done.")

    #####################################
    # Linking PERSONS
    # Loop the person identifiers and link them
    #####################################
    for from_id, to_id in itertools.permutations(list_person_ids, 2):
        query = mutation_person_add_exact_match_person(from_id, to_id)
        response = await ce.connection.submit_query_async(query)
        print(f"   - Linking Person {from_id} to Person {to_id} done.")

    #####################################
    # MUSICCOMPOSITION
    # Loop the music works to create the CE_MusicComposition on the CE
    #####################################
    for work in music_works:

        work.identifier = await lookupIdentifier("MusicComposition",
                                                 work.source)

        if work.identifier is not None:
            print(f"Updating work {work.identifier} in Trompa CE\n", end="")

            response = await ce.connection.submit_query_async(
                mutation_update_music_composition(**work.as_dict()))
            work.identifier = response["data"]["UpdateMusicComposition"][
                "identifier"]
        else:
            print("Inserting new work {} in Trompa CE\n".format(work.name))

            response = await ce.connection.submit_query_async(
                mutation_create_music_composition(**work.as_dict()))
            work.identifier = response["data"]["CreateMusicComposition"][
                "identifier"]

    print(f"Importing music composition {work.identifier} done.\n")

    #####################################
    # Linking PERSONS and MUSICCOMPOSITIONS
    # Loop the person identifiers and link them to music compositions
    #####################################
    for person_id in list_person_ids:
        query = mutation_merge_music_composition_composer(
            work.identifier, person_id)
        response = await ce.connection.submit_query_async(query)
        print(
            f"   - Linking Person {person_id} to MusicComposition {work.identifier} done.\n"
        )

    #####################################
    # AUDIOOBJECTS
    # Loop the tracks to create the CE_AudioObject on the CE
    #####################################
    for track in tracks:

        track.identifier = await lookupIdentifier("AudioObject", track.source)

        if track.identifier is not None:
            print(f"Updating record {track.identifier} in Trompa CE\n")

            response = await ce.connection.submit_query_async(
                mutation_update_audioobject(**track.as_dict()))
            track.identifier = response["data"]["UpdateAudioObject"][
                "identifier"]
        else:
            print("Inserting new track {} in Trompa CE\n".format(track.title))

            response = await ce.connection.submit_query_async(
                mutation_create_audioobject(**track.as_dict()))
            track.identifier = response["data"]["CreateAudioObject"][
                "identifier"]

    print(f"Importing tracks {track.identifier} done.\n")

    #####################################
    # Linking MUSICCOMPOSITIONS and AUDIOOBJECTS
    # Loop the musicworks identifiers and link them to audioobjects
    #####################################
    query = mutation_merge_audioobject_exampleofwork(track.identifier,
                                                     work.identifier)
    response = await ce.connection.submit_query_async(query)
    print(
        f"   - Linking MusicComposition {work.identifier} to AudioObject {track.identifier} done."
    )
예제 #8
0
async def import_tracks(key: str):
    """
    Imports audio fragments from Muziekweb for the key into the Trompa CE.
    """
    print(f"Retrieving release info with key {key} from Muziekweb")
    # Get data from Muziekweb
    audio_objects, music_recordings, music_works, persons, music_groups = get_mw_audio_1track(
        key)
    # tracks = get_mw_audio(key)

    if audio_objects is None or len(audio_objects) == 0:
        print(f"No track data received for {key}")
        return

    #####################################
    # MUSICCOMPOSITION
    # Loop the music works to create the CE_MusicComposition on the CE
    #####################################
    for work in music_works:

        work.identifier = await lookupIdentifier("MusicComposition",
                                                 work.source)

        if work.identifier is not None:
            print(f"Updating work {work.identifier} in Trompa CE\n", end="")
            response = await ce.connection.submit_query_async(
                mutation_update_music_composition(**work.as_dict()))
            work.identifier = response["data"]["UpdateMusicComposition"][
                "identifier"]
        else:
            print("Inserting new work {} in Trompa CE\n".format(work.name))

            response = await ce.connection.submit_query_async(
                mutation_create_music_composition(**work.as_dict()))
            work.identifier = response["data"]["CreateMusicComposition"][
                "identifier"]

    print(f"Importing music composition {work.identifier} done.\n")

    #####################################
    # MUSICRECORDING
    # Loop the music recordings to create the CE_MusicRecording on the CE
    #####################################
    for recording in music_recordings:

        recording.identifier = await lookupIdentifier("MusicRecording",
                                                      recording.source)

        if recording.identifier is not None:
            print(
                f"Updating music recording {recording.identifier} in Trompa CE\n"
            )

            response = await ce.connection.submit_query_async(
                mutation_update_musicrecording(**recording.as_dict()))
            recording.identifier = response["data"]["UpdateMusicRecording"][
                "identifier"]
        else:
            print("Inserting new recording {} in Trompa CE\n".format(
                recording.title))

            response = await ce.connection.submit_query_async(
                mutation_create_musicrecording(**recording.as_dict()))
            recording.identifier = response["data"]["CreateMusicRecording"][
                "identifier"]

    print(f"Importing recordings {recording.identifier} done.\n")

    #####################################
    # AUDIOOBJECTS
    # Loop the audio objects to create the CE_AudioObject on the CE
    #####################################
    for audio in audio_objects:

        audio.identifier = await lookupIdentifier("AudioObject", audio.source)

        if audio.identifier is not None:
            print(f"Updating audio object {audio.identifier} in Trompa CE\n")

            response = await ce.connection.submit_query_async(
                mutation_update_audioobject(**audio.as_dict()))
            audio.identifier = response["data"]["UpdateAudioObject"][
                "identifier"]
        else:
            print("Inserting new audio {} in Trompa CE\n".format(audio.title))

            response = await ce.connection.submit_query_async(
                mutation_create_audioobject(**audio.as_dict()))
            audio.identifier = response["data"]["CreateAudioObject"][
                "identifier"]

    print(f"Importing audio {audio.identifier} done.\n")

    #####################################
    # Linking MUSICCOMPOSITIONS and MUSICRECORDING
    # Loop the musicworks identifiers and link them to audioobjects
    #####################################
    query = mutation_merge_music_composition_recorded_as(
        work.identifier, recording.identifier)
    response = await ce.connection.submit_query_async(query)
    print(
        f"   - Linking MusicComposition {work.identifier} to MusicRecording {recording.identifier} done."
    )

    #####################################
    # Linking MUSICRECORDING and AUDIOOBJECTS
    # Loop the musicworks identifiers and link them to audioobjects
    #####################################
    query = mutation_merge_music_recording_audio(recording.identifier,
                                                 audio.identifier)
    response = await ce.connection.submit_query_async(query)
    print(
        f"   - Linking MusicRecording {recording.identifier} to AudioObject {audio.identifier} done."
    )

    #####################################
    # PERSONS
    # Loop the persons on all external links to add references for each CE_person
    #####################################
    list_person_ids = list()
    for person in persons:

        person.identifier = await lookupIdentifier("Person", person.source)

        if person.identifier is not None:
            print(f"Updating person {person.identifier} in Trompa CE\n")

            response = await ce.connection.submit_query_async(
                mutation_update_person(**person.as_dict()))
            person.identifier = response["data"]["UpdatePerson"]["identifier"]
            list_person_ids.append(person.identifier)
        else:
            print("Inserting new person {} in Trompa CE\n".format(person.name))

            response = await ce.connection.submit_query_async(
                mutation_create_person(**person.as_dict()))

            person.identifier = response["data"]["CreatePerson"]["identifier"]
            list_person_ids.append(person.identifier)

    if list_person_ids:
        print(f"Importing Persons for {key} done.")

    #####################################
    # Linking PERSONS
    # Loop the person identifiers and link them
    #####################################
    if not music_groups:
        for from_id, to_id in itertools.permutations(list_person_ids, 2):
            query = mutation_person_add_exact_match_person(from_id, to_id)
            response = await ce.connection.submit_query_async(query)
            print(f"   - Linking Person {from_id} to Person {to_id} done.")

    #####################################
    # Linking PERSONS and MUSICCOMPOSITIONS
    # Loop the person identifiers and link them to music compositions
    #####################################
    for person_id in list_person_ids:
        query = mutation_merge_music_composition_composer(
            work.identifier, person_id)
        response = await ce.connection.submit_query_async(query)
        print(
            f"   - Linking Person {person_id} to MusicComposition {work.identifier} done.\n"
        )

    #####################################
    # MUSIC GROUPS
    # Loop the Music Groups on all external links to add references for each CE_person
    #####################################
    list_music_group_ids = list()
    for music_group in music_groups:

        music_group.identifier = await lookupIdentifier(
            "MusicGroup", music_group.source)

        if music_group.identifier is not None:
            print(
                f"Updating music group {music_group.identifier} in Trompa CE\n"
            )

            response = await ce.connection.submit_query_async(
                mutation_update_musicgroup(**music_group.as_dict()))
            music_group.identifier = response["data"]["UpdateMusicGroup"][
                "identifier"]
            list_music_group_ids.append(music_group.identifier)
        else:
            print("Inserting new music group {} in Trompa CE\n".format(
                music_group.name))

            response = await ce.connection.submit_query_async(
                mutation_create_musicgroup(**music_group.as_dict()))

            music_group.identifier = response["data"]["CreateMusicGroup"][
                "identifier"]
            list_music_group_ids.append(music_group.identifier)

    if list_music_group_ids:
        print(f"Importing Music Groups for {key} done.")

    #####################################
    # Linking MUSIC GROUPS
    # Loop the music groups identifiers and link them
    #####################################
    for from_id, to_id in itertools.permutations(list_music_group_ids, 2):
        query = mutation_musicgroup_add_exact_match_musicgroup(from_id, to_id)
        response = await ce.connection.submit_query_async(query)
        print(
            f"   - Linking Music Group {from_id} to Music Group {to_id} done.")

    #####################################
    # Linking MUSIC GROUPS and MUSICCOMPOSITIONS
    # Loop the music group identifiers and link them to music compositions
    #####################################
    for music_group_id in list_music_group_ids:
        query = mutation_merge_music_composition_composer(
            work.identifier, music_group_id)
        response = await ce.connection.submit_query_async(query)
        print(
            f"   - Linking MusicGroup {music_group_id} to MusicComposition {work.identifier} done.\n"
        )

    #####################################
    # Linking MUSIC GROUPS and PERSONS
    # Loop the music group identifiers and link them to person identifiers
    #####################################
    for music_group_id in list_music_group_ids:
        for person_id in list_person_ids:
            query = mutation_merge_musicgroup_member(person_id, music_group_id)
            response = await ce.connection.submit_query_async(query)
            print(
                f"   - Linking Person {person_id} to MusicGroup {music_group_id} done.\n"
            )