コード例 #1
0
def read_one(artist_id, song_id):
    """
    This function responds to a request for
    /api/people/{artist_id}/songs/{song_id}
    with one matching song for the associated artist

    :param artist_id:       Id of artist the song is related to
    :param song_id:         Id of the song
    :return:                json string of song contents
    """
    # Query db for the song
    # First make query to join artist with songs
    # Then filter for input parameters
    song = (
            Song.query.join(Artist, Artist.artist_id == Song.artist_id)
            .filter(Artist.artist_id == artist_id)
            .filter(Song.song_id == song_id)
            .one_or_none()
            )

    # Was a song found
    if song is not None:
        song_schema = SongSchema()
        data = song_schema.dump(song)
        return data

    else:
        abort(404, f'Song not found for ID: {song_id}')
コード例 #2
0
def update(artist_id, song_id, song):
    """
    :param artist_id:  
    :param song_id:  
    :param song:  
    :return:
    """
    # Query for song assoc with artist_id param that has song_id param
    update_song = (
            Song.query.filter(Artist.artist_id == artist_id)
            .filter(Song.song_id == song_id)
            .one_or_none()
            )

    if update_song is not None:
        # turn passed in song to db object
        schema = SongSchema()
        update = schema.load(song, session=db.session)

        # set new obj id to song we want to modify
        update.artist_id = update_song.artist_id
        update.song_id = update_song.song_id

        # merge new obj to old and commit
        db.session.merge(update)
        db.session.commit()
        
        # return updated song in response
        return schema.dump(update_song), 200

    else:
        abort(404, f'Song not found for ID: {song_id}')
コード例 #3
0
def create(artist_id, song):
    """
    Create a new song associated with ID of a db artist 
    :param artist_id:   int, id of artist assoc with song 
    :param song:        str, text content of the song to create
    :return:            201, successfully created song
    """
    # get the parent artist for the song
    artist = Artist.query.filter(Artist.artist_id == artist_id).one_or_none()

    if artist is None:
        abort(404, f'Artist not found for ID: {artist_id}')
    
    song_schema = SongSchema()

    for existing_song in artist.songs:
        if existing_song.song_name == song['song_name']:
            abort(409, 'Artist {artist} already has a song named: {song}'.format(artist=artist.full_name, song=song['song_name']))

    # init a song schema instance
    new_song = song_schema.load(song, session=db.session)

    artist.songs.append(new_song)
    db.session.commit()

    # Serialize and return the newly created song
    data = song_schema.dump(new_song)
    return data, 201
コード例 #4
0
def get_one_song(artist, song_title):
    """

    :param artist:
    :param song_title:
    :return:
    """

    # Get the song requested
    song = Song.query.filter(
        and_(Song.artist == artist, Song.song == song_title)).one_or_none()

    # Did we find a song?
    if song is not None:

        # Serialize the data for the response
        song_schema = SongSchema()
        data = song_schema.dump(song).data
        return data

    # Otherwise, nope, didn't find that song
    else:
        abort(
            404,
            "Song title {song_title} is not found for artist: {artist} ".
            format(artist=artist, song_title=song_title),
        )
        return None
コード例 #5
0
def get_one_song_by_id(song_id):
    """

    :param song_id:
    :return:
    """

    # Get the song requested
    song = Song.query.filter(Song.song_id == song_id).one_or_none()

    # Did we find a song?
    if song is not None:

        # Serialize the data for the response
        song_schema = SongSchema()
        data = song_schema.dump(song).data
        return data

    # Otherwise, nope, didn't find that song
    else:
        abort(
            404,
            "Song is not found for song id: {song_id} ".format(
                song_id=song_id),
        )
        return None
コード例 #6
0
ファイル: song.py プロジェクト: jayasurya217/Python_api
def read_all():
    """
    This function responds to a request for /api/song
    with the complete lists of song
    :return:        json string of list of song
    """
    # Create the list of song from our data
    song = Song.query.order_by(Song.id).all()

    # Serialize the data for the response
    song_schema = SongSchema(many=True)
    data = song_schema.dump(song)
    return data
コード例 #7
0
def read_all():
    """
    this function responds to a requet
    to /api/songs and returns all songs
    sorted by song name
    :return:    json list of all songs for all people
    """
    # Query the db for all songs
    songs = Song.query.order_by(Song.song_name).all()
    # Serialize the list of songs from our data
    song_schema = SongSchema(many=True)
    data = song_schema.dump(songs)
    return data
コード例 #8
0
ファイル: song.py プロジェクト: jayasurya217/Python_api
def create(song):
    """
    This function creates a new song in the song structure
    based on the passed in song data
    :param song:  song to create in structure
    :return:        201 on success, 406 on person exists
    """
    song_id = song.get("song_id")
    name = song.get("name")
    
    existing_song = (
        Song.query.filter(Song.id == id)
        .filter(Song.name == name)
        .one_or_none()
    )

    # Can we insert this song?
    if existing_song is None:

        # Create a song instance using the schema and the passed in song
        schema = SongSchema()
        new_song = schema.load(song, session=db.session)

        # Add the song to the database
        db.session.add(new_song)
        db.session.commit()

        # Serialize and return the newly created song in the response
        data = schema.dump(new_song)

        return data, 201

    # Otherwise, nope, song exists already
    else:
        abort(
            409,
            "song {id} {name} exists already".format(
                song_id=song_id, name=name
            ),
        )
コード例 #9
0
def update_song(song):
    """
    This function update a existing song in the song structure
    based on the passed in song data
    :param song:  song to update in song structure
    :return:      200 on success update song
    """
    song_title = song['song']
    artist = song['artist']

    existing_song = (Song.query.filter(Song.song == song_title).filter(
        Song.artist == artist).one_or_none())

    # Did we find a song?
    if existing_song is not None:

        existing_song.song = song['song']
        existing_song.artist = song['artist']
        existing_song.lyrics = song['lyrics']
        existing_song.year = helper_functions.get_timestamp_year()
        existing_song.timestamp = datetime.now()

        # turn the passed in song into a db object
        schema = SongSchema()

        # Commit new updates
        db.session.commit()

        # return updated song in the response
        data = schema.dump(existing_song).data

        return data, 200

    # Otherwise, nope, didn't find that song
    else:
        abort(
            404,
            "Song title {song_title} is not found for artist: {artist} ".
            format(artist=artist, song_title=song_title),
        )
コード例 #10
0
def get_top_sample_lyrics(given_phase, limits=10):
    """
    This function get songs which lyrics contains the given phase. Currently will return the top 10 matched songs.

    :param given_phase:
    :param limits:
    :return:
    """

    word_list = given_phase.split('+')

    given_phase_str = " ".join([word for word in word_list]).lower().strip()

    print('given_phase_str: ', given_phase_str)

    # Get the songs requested
    songs = Song.query.filter(
        and_(Song.lyrics.like(f'%{given_phase_str}%'),
             Song.lyrics != None)).order_by(func.random()).limit(limits).all()

    # Did we find a the songs?
    if songs is not None:

        # Serialize the data for the response
        song_schema = SongSchema(many=True)
        song_data = song_schema.dump(songs).data

        for data in song_data:
            data['lyrics'] = get_partial_match_substring(
                data['lyrics'].lower(), given_phase_str)

        return song_data

    # Otherwise, nope, didn't find any song
    else:
        abort(
            404,
            "Songs not found for given word: {given_word}".format(
                given_word=given_phase_str),
        )
コード例 #11
0
def create_update_song(song):
    """
        This function will create a new song if it is not already exist in the song structure.
        If the song is exist, it will update the song instead.
        based on the passed in song data
        :param song:  song to create in song structure
        :return:        201 on success create new song , 200 on success update song
        """
    song_title = song['song']
    artist = song['artist']

    existing_song = Song.query.filter(Song.song == song_title).filter(
        Song.artist == artist).one_or_none()

    # Is this song already exist?
    if existing_song is None:

        # Create a song instance using the schema and the passed in song
        schema = SongSchema()
        new_song = schema.load(song, session=db.session).data

        new_song.year = helper_functions.get_timestamp_year()
        new_song.timestamp = datetime.now()

        # Add the song to the database
        db.session.add(new_song)
        db.session.commit()

        # Serialize and return the newly created song in the response
        data = schema.dump(new_song).data

        return data, 201

    # If the song is already exist, update the song instead
    else:

        print("existing_song: ", existing_song)

        existing_song.song = song['song']
        existing_song.artist = song['artist']
        existing_song.lyrics = song['lyrics']
        existing_song.year = helper_functions.get_timestamp_year()
        existing_song.timestamp = datetime.now()

        # turn the passed in song into a db object
        schema = SongSchema()

        # Commit new updates
        db.session.commit()

        # return updated song in the response
        data = schema.dump(existing_song).data

        return data, 200
コード例 #12
0
ファイル: song.py プロジェクト: jayasurya217/Python_api
def read_one(song_id):
    """
    This function responds to a request for /api/song/{song_id}
    with one matching song from data
    :param person_id:   Id of person to find
    :return:            person matching id
    """
    # Get the song requested
    song = Song.query.filter(Song.song_id == song_id).one_or_none()

    # song is there?
    if song is not None:

        # Serialize the data for the response
        song_schema = SongSchema()
        data = song_schema.dump(song)
        return data

    # Otherwise, nope, didn't find that song
    else:
        abort(
            404,
            "Person not found for Id: {person_id}".format(song_id=song_id),
        )
コード例 #13
0
def create_song(song):
    """
    This function creates a new song in the song structure
    based on the passed in song data
    :param song:  song to create in song structure
    :return:        201 on success create new song
    """
    song_title = song['song']
    artist = song['artist']

    existing_song = (Song.query.filter(Song.song == song_title).filter(
        Song.artist == artist).one_or_none())

    # Can we insert this song?
    if existing_song is None:

        # Create a song instance using the schema and the passed in song
        schema = SongSchema()
        new_song = schema.load(song, session=db.session).data

        # Add the song to the database
        db.session.add(new_song)
        db.session.commit()

        # Serialize and return the newly created song in the response
        data = schema.dump(new_song).data

        return data, 201

    # Otherwise, update existing song
    else:
        abort(
            409,
            "Song title {song_title} is already exists for artist: {artist} ".
            format(artist=artist, song_title=song_title),
        )
コード例 #14
0
ファイル: song.py プロジェクト: jayasurya217/Python_api
def update(song_id, song):
    """
    This function updates an existing song in the structure
    Throws an error if a song with the name we want to update to
    already exists in the database.
    :param song_id:   Id of the song to update in the  structure
    :param song:      song to update
    :return:            updated structure
    """
    # Get the song requested from the db into session
    update_song = Song.query.filter(
        Song.song_id == song_id
    ).one_or_none()

    # Try to find an existing song with the same name as the update
    song_id = song.get("song_id")
    name = song.get("name")
    duration = song.get("duration")

    existing_song = (
        Song.query.filter(Song.song_id == song_id)
        .filter(Song.name == name)
        .one_or_none()
    )

    # Are we trying to find a song that does not exist?
    if update_song is None:
        abort(
            404,
            "Person not found for Id: {person_id}".format(song_id=song_id),
        )

    # prevent duplicate of another song already existing
    elif (
        existing_song is not None and existing_song.song_id != song_id
    ):
        abort(
            409,
            "song {song_id} {name} exists already".format(
                song_id=song_id, name=name
            ),
        )

    # Otherwise go ahead and update!
    else:

        # turn the passed in song into a db object
        schema = SongSchema()
        update = schema.load(song, session=db.session)

        # Set the id to the song we want to update
        update.song_id = update_song.song_id

        # merge the new object into the old and commit it to the db
        db.session.merge(update)
        db.session.commit()

        # return updated song in the response
        data = schema.dump(update_song)

        return data, 200
コード例 #15
0
app.secret_key = 'replace later'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

engine = create_engine("sqlite:///audio.db")
Session = scoped_session(sessionmaker(bind=engine))
session = Session()
# Make the WSGI interface available at the top level so wfastcgi can get it.
wsgi_app = app.wsgi_app

wsgi_app = app.wsgi_app
app.secret_key = 'replace later'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///audio.db'
db = SQLAlchemy(app)

import requests
song_schema = SongSchema()
songs_schema = SongSchema(many=True)
podcast_schema = PodcastSchema()
podcasts_schema = PodcastSchema(many=True)
audiobook_schema = AudiobookSchema()
audiobooks_schema = AudiobookSchema(many=True)


class UploadSong:
    def __init__(self, request):
        self.name = request.json['name']
        self.duration = request.json['duration']

    #uploaded_time = request.json['uploaded_time']