예제 #1
0
def mapping():
    """Endpoint for getting mappings for a MusicBrainz entity.

    JSON parameters:
        mbid: MBID of the entity that you need to find a mapping for.

    Returns:
        List with mappings to a specified MBID.
    """
    mbid = request.json["mbid"]
    if not validate_uuid(mbid):
        raise BadRequest("Incorrect MBID (UUID).")

    conn = psycopg2.connect(**current_app.config["PG_INFO"])
    cur = conn.cursor()

    cur.execute(
        "SELECT spotify_uri "
        "FROM mapping "
        "WHERE is_deleted = FALSE AND mbid = %s", (mbid, ))

    response = Response(json.dumps({
        "mbid":
        mbid,
        "mappings": [row[0] for row in cur.fetchall()],
    }),
                        mimetype="application/json")
    response.headers["Access-Control-Allow-Origin"] = "*"
    return response
예제 #2
0
파일: views.py 프로젝트: mwiencek/mbspotify
def mapping():
    """Endpoint for getting mappings for a MusicBrainz entity.

    JSON parameters:
        mbid: MBID of the entity that you need to find a mapping for.

    Returns:
        List with mappings to a specified MBID.
    """
    mbid = request.json["mbid"]
    if not validate_uuid(mbid):
        raise BadRequest("Incorrect MBID (UUID).")

    conn = psycopg2.connect(current_app.config['PG_CONNECT'])
    cur = conn.cursor()

    cur.execute("SELECT spotify_uri "
                "FROM mapping "
                "WHERE is_deleted = FALSE AND mbid = %s",
                (mbid,))

    response = Response(
        json.dumps({
            "mbid": mbid,
            "mappings": [row[0] for row in cur.fetchall()],
        }),
        mimetype="application/json"
    )
    response.headers["Access-Control-Allow-Origin"] = "*"
    return response
예제 #3
0
def add():
    """Endpoint for adding new mappings to Spotify.

    Only connection to albums on Spotify is supported right now.

    JSON parameters:
        user: UUID of the user who is adding new mapping.
        mbid: MusicBrainz ID of an entity that is being connected.
        spotify_uri: Spotify URI of an album that is being connected.
    """
    user = request.json["user"]
    if not validate_uuid(user):
        raise BadRequest("Incorrect user ID (UUID).")

    mbid = request.json["mbid"]
    if not validate_uuid(mbid):
        raise BadRequest("Incorrect MBID (UUID).")

    uri = request.json["spotify_uri"]
    if not uri.startswith("spotify:album:"):
        raise BadRequest(
            "Incorrect Spotify URI. Only albums are supported right now.")

    conn = psycopg2.connect(**current_app.config["PG_INFO"])
    cur = conn.cursor()

    try:
        # Checking if mapping is already created
        cur.execute(
            "SELECT id FROM mapping "
            "WHERE is_deleted = FALSE "
            "AND mbid = %s "
            "AND spotify_uri = %s", (mbid, uri))
        if not cur.rowcount:
            # and if it's not, adding it
            cur.execute(
                "INSERT INTO mapping (mbid, spotify_uri, cb_user, is_deleted)"
                "VALUES (%s, %s, %s, FALSE)", (mbid, uri, user))
            conn.commit()
    except psycopg2.IntegrityError as e:
        raise BadRequest(str(e))
    except psycopg2.OperationalError as e:
        raise ServiceUnavailable(str(e))

    response = Response()
    response.headers["Access-Control-Allow-Origin"] = "*"
    return response
예제 #4
0
파일: views.py 프로젝트: mwiencek/mbspotify
def add():
    """Endpoint for adding new mappings to Spotify.

    Only connection to albums on Spotify is supported right now.

    JSON parameters:
        user: UUID of the user who is adding new mapping.
        mbid: MusicBrainz ID of an entity that is being connected.
        spotify_uri: Spotify URI of an album that is being connected.
    """
    user = request.json["user"]
    if not validate_uuid(user):
        raise BadRequest("Incorrect user ID (UUID).")

    mbid = request.json["mbid"]
    if not validate_uuid(mbid):
        raise BadRequest("Incorrect MBID (UUID).")

    uri = request.json["spotify_uri"]
    if not uri.startswith("spotify:album:"):
        raise BadRequest("Incorrect Spotify URI. Only albums are supported right now.")

    conn = psycopg2.connect(current_app.config['PG_CONNECT'])
    cur = conn.cursor()

    try:
        # Checking if mapping is already created
        cur.execute("SELECT id FROM mapping "
                    "WHERE is_deleted = FALSE "
                    "AND mbid = %s "
                    "AND spotify_uri = %s", (mbid, uri))
        if not cur.rowcount:
            # and if it's not, adding it
            cur.execute("INSERT INTO mapping (mbid, spotify_uri, cb_user, is_deleted)"
                        "VALUES (%s, %s, %s, FALSE)",
                        (mbid, uri, user))
            conn.commit()
    except psycopg2.IntegrityError as e:
        raise BadRequest(str(e))
    except psycopg2.OperationalError as e:
        raise ServiceUnavailable(str(e))

    response = Response()
    response.headers["Access-Control-Allow-Origin"] = "*"
    return response
예제 #5
0
파일: views.py 프로젝트: mwiencek/mbspotify
def mapping_jsonp(mbid):
    if not validate_uuid(mbid):
        raise BadRequest("Incorrect MBID (UUID).")

    conn = psycopg2.connect(current_app.config['PG_CONNECT'])
    cur = conn.cursor()

    cur.execute("SELECT mbid, spotify_uri "
                "FROM mapping "
                "WHERE is_deleted = FALSE AND mbid = %s",
                (mbid,))
    if not cur.rowcount:
        return jsonify({})
    # TODO: Return all mappings to a specified MBID (don't forget to update userscript).
    row = cur.fetchone()
    return jsonify({mbid: row[1]})
예제 #6
0
def mapping_jsonp(mbid):
    if not validate_uuid(mbid):
        raise BadRequest("Incorrect MBID (UUID).")

    conn = psycopg2.connect(**current_app.config["PG_INFO"])
    cur = conn.cursor()

    cur.execute(
        "SELECT mbid, spotify_uri "
        "FROM mapping "
        "WHERE is_deleted = FALSE AND mbid = %s", (mbid, ))
    if not cur.rowcount:
        return jsonify({})
    # TODO: Return all mappings to a specified MBID (don't forget to update userscript).
    row = cur.fetchone()
    return jsonify({mbid: row[1]})
예제 #7
0
파일: views.py 프로젝트: mwiencek/mbspotify
def vote():
    """Endpoint for voting against incorrect mappings.

    JSON parameters:
        user: UUID of the user who is voting.
        mbid: MusicBrainz ID of an entity that has incorrect mapping.
        spotify_uri: Spotify URI of an incorrectly mapped entity.
    """
    user = request.json["user"]
    if not validate_uuid(user):
        raise BadRequest("Incorrect user ID (UUID).")

    mbid = request.json["mbid"]
    if not validate_uuid(mbid):
        raise BadRequest("Incorrect MBID (UUID).")

    spotify_uri = request.json["spotify_uri"]

    conn = psycopg2.connect(current_app.config['PG_CONNECT'])
    cur = conn.cursor()

    try:
        cur.execute("SELECT id FROM mapping WHERE mbid = %s AND spotify_uri = %s",
                    (mbid, spotify_uri))
        if not cur.rowcount:
            raise BadRequest("Can't find mapping between specified MBID and Spotify URI.")
        mapping_id = cur.fetchone()[0]

        # Checking if user have already voted
        cur.execute("SELECT id FROM mapping_vote WHERE mapping = %s AND cb_user = %s",
                    (mapping_id, user))
        if cur.rowcount:
            raise BadRequest("You already voted against this mapping.")

        cur.execute("INSERT INTO mapping_vote (mapping, cb_user) VALUES (%s, %s)",
                    (mapping_id, user))
        conn.commit()

    except psycopg2.IntegrityError as e:
        raise BadRequest(str(e))
    except psycopg2.OperationalError as e:
        raise ServiceUnavailable(str(e))

    # Check if threshold is reached. And if it is, marking mapping as deleted.
    try:
        cur.execute("SELECT * "
                    "FROM mapping_vote "
                    "JOIN mapping ON mapping_vote.mapping = mapping.id "
                    "WHERE mapping.mbid = %s"
                    "      AND mapping.spotify_uri = %s"
                    "      AND mapping.is_deleted = FALSE",
                    (mbid, spotify_uri))
        if cur.rowcount >= current_app.config["THRESHOLD"]:
            cur.execute("UPDATE mapping SET is_deleted = TRUE "
                        "WHERE mbid = %s AND spotify_uri = %s",
                        (mbid, spotify_uri))
            conn.commit()

    except psycopg2.IntegrityError as e:
        raise BadRequest(str(e))
    except psycopg2.OperationalError as e:
        raise ServiceUnavailable(str(e))

    response = Response()
    response.headers["Access-Control-Allow-Origin"] = "*"
    return response
예제 #8
0
def vote():
    """Endpoint for voting against incorrect mappings.

    JSON parameters:
        user: UUID of the user who is voting.
        mbid: MusicBrainz ID of an entity that has incorrect mapping.
        spotify_uri: Spotify URI of an incorrectly mapped entity.
    """
    user = request.json["user"]
    if not validate_uuid(user):
        raise BadRequest("Incorrect user ID (UUID).")

    mbid = request.json["mbid"]
    if not validate_uuid(mbid):
        raise BadRequest("Incorrect MBID (UUID).")

    spotify_uri = request.json["spotify_uri"]

    conn = psycopg2.connect(**current_app.config["PG_INFO"])
    cur = conn.cursor()

    try:
        cur.execute(
            "SELECT id FROM mapping WHERE mbid = %s AND spotify_uri = %s",
            (mbid, spotify_uri))
        if not cur.rowcount:
            raise BadRequest(
                "Can't find mapping between specified MBID and Spotify URI.")
        mapping_id = cur.fetchone()[0]

        # Checking if user have already voted
        cur.execute(
            "SELECT id FROM mapping_vote WHERE mapping = %s AND cb_user = %s",
            (mapping_id, user))
        if cur.rowcount:
            raise BadRequest("You already voted against this mapping.")

        cur.execute(
            "INSERT INTO mapping_vote (mapping, cb_user) VALUES (%s, %s)",
            (mapping_id, user))
        conn.commit()

    except psycopg2.IntegrityError as e:
        raise BadRequest(str(e))
    except psycopg2.OperationalError as e:
        raise ServiceUnavailable(str(e))

    # Check if threshold is reached. And if it is, marking mapping as deleted.
    try:
        cur.execute(
            "SELECT * "
            "FROM mapping_vote "
            "JOIN mapping ON mapping_vote.mapping = mapping.id "
            "WHERE mapping.mbid = %s"
            "      AND mapping.spotify_uri = %s"
            "      AND mapping.is_deleted = FALSE", (mbid, spotify_uri))
        if cur.rowcount >= current_app.config["THRESHOLD"]:
            cur.execute(
                "UPDATE mapping SET is_deleted = TRUE "
                "WHERE mbid = %s AND spotify_uri = %s", (mbid, spotify_uri))
            conn.commit()

    except psycopg2.IntegrityError as e:
        raise BadRequest(str(e))
    except psycopg2.OperationalError as e:
        raise ServiceUnavailable(str(e))

    response = Response()
    response.headers["Access-Control-Allow-Origin"] = "*"
    return response
예제 #9
0
 def test_validate_uuid(self):
     self.assertTrue(
         utils.validate_uuid("123e4567-e89b-12d3-a456-426655440000"))
     self.assertFalse(utils.validate_uuid("not-a-uuid"))
예제 #10
0
 def test_validate_uuid(self):
     self.assertTrue(utils.validate_uuid("123e4567-e89b-12d3-a456-426655440000"))
     self.assertFalse(utils.validate_uuid("not-a-uuid"))