コード例 #1
0
def session_info(request, data):
    try:
        sk = data['sk']
        api_key = data['api_key']
        output_format = data.get('format', 'xml')
        username = data['username']
    except KeyError:
        raise InvalidAPIUsage(6, output_format=output_format)        # Missing Required Params

    session = Session.load(sk, api_key)
    if (not session) or User.load_by_name(username).id != session.user.id:
        raise InvalidAPIUsage(9, output_format=output_format)        # Invalid Session KEY

    print("SESSION INFO for session %s, user %s" % (session.id, session.user.name))

    doc, tag, text = Doc().tagtext()
    with tag('lfm', status='ok'):
        with tag('application'):
            with tag('session'):
                with tag('name'):
                    text(session.user.name)
                with tag('key'):
                    text(session.id)
                with tag('subscriber'):
                    text('0')
                with tag('country'):
                    text('US')

    return format_response('<?xml version="1.0" encoding="utf-8"?>\n' + yattag.indent(doc.getvalue()),
                           output_format)
コード例 #2
0
def api_auth_approve():
    """ Authenticate the user token provided.
    """
    user = User.load_by_name(current_user.musicbrainz_id)
    if "token" not in request.form:
        return render_template(
            "user/auth.html",
            user_id=current_user.musicbrainz_id,
            msg="Missing required parameters. Please provide correct parameters and try again."
        )
    token = Token.load(request.form['token'])
    if not token:
        return render_template(
            "user/auth.html",
            user_id=current_user.musicbrainz_id,
            msg="Either this token is already used or invalid. Please try again."
        )
    if token.user:
        return render_template(
            "user/auth.html",
            user_id=current_user.musicbrainz_id,
            msg="This token is already approved. Please check the token and try again."
        )
    if token.has_expired():
        return render_template(
            "user/auth.html",
            user_id=current_user.musicbrainz_id,
            msg="This token has expired. Please create a new token and try again."
        )
    token.approve(user.name)
    return render_template(
        "user/auth.html",
        user_id=current_user.musicbrainz_id,
        msg="Token %s approved for user %s, press continue in client." % (token.token, current_user.musicbrainz_id)
    )
コード例 #3
0
def user_info(request, data):
    """ Gives information about the user specified in the parameters.
    """
    try:
        api_key = data['api_key']
        output_format = data.get('format', 'xml')
        sk = data.get('sk')
        username = data.get('user')
        if not (sk or username):
            raise KeyError

        if not Token.is_valid_api_key(api_key):
            raise InvalidAPIUsage(
                CompatError.INVALID_API_KEY,
                output_format=output_format)  # Invalid API key

        user = User.load_by_sessionkey(sk, api_key)
        if not user:
            raise InvalidAPIUsage(
                CompatError.INVALID_SESSION_KEY,
                output_format=output_format)  # Invalid Session key

        query_user = User.load_by_name(username) if (
            username and username != user.name) else user
        if not query_user:
            raise InvalidAPIUsage(
                CompatError.INVALID_RESOURCE,
                output_format=output_format)  # Invalid resource specified

    except KeyError:
        raise InvalidAPIUsage(
            CompatError.INVALID_PARAMETERS,
            output_format=output_format)  # Missing required params

    doc, tag, text = Doc().tagtext()
    with tag('lfm', status='ok'):
        with tag('user'):
            with tag('name'):
                text(query_user.name)
            with tag('realname'):
                text(query_user.name)
            with tag('url'):
                text('http://listenbrainz.org/user/' + query_user.name)
            with tag('playcount'):
                text(User.get_play_count(query_user.id))
            with tag('registered',
                     unixtime=str(query_user.created.strftime("%s"))):
                text(str(query_user.created))

    return format_response(
        '<?xml version="1.0" encoding="utf-8"?>\n' +
        yattag.indent(doc.getvalue()), data.get('format', "xml"))
コード例 #4
0
def user_info(request, data):
    """ Gives information about the user specified in the parameters.
    """
    try:
        api_key = data['api_key']
        output_format = data.get('format', 'xml')
        sk = data.get('sk')
        username = data.get('user')
        if not (sk or username):
            raise KeyError

        if not Token.is_valid_api_key(api_key):
            raise InvalidAPIUsage(10, output_format=output_format)  # Invalid API key

        user = User.load_by_sessionkey(sk, api_key)
        if not user:
            raise InvalidAPIUsage(9, output_format=output_format)  # Invalid Session key

        query_user = User.load_by_name(username) if (username and username != user.name) else user
        if not query_user:
            raise InvalidAPIUsage(7, output_format=output_format)  # Invalid resource specified

    except KeyError:
        raise InvalidAPIUsage(6, output_format=output_format)       # Missing required params

    doc, tag, text = Doc().tagtext()
    with tag('lfm', status='ok'):
        with tag('user'):
            with tag('name'):
                text(query_user.name)
            with tag('realname'):
                text(query_user.name)
            with tag('url'):
                text('http://listenbrainz.org/user/' + query_user.name)
            with tag('playcount'):
                text(User.get_play_count(query_user.id))
            with tag('registered', unixtime=str(query_user.created.strftime("%s"))):
                text(str(query_user.created))

    return format_response('<?xml version="1.0" encoding="utf-8"?>\n' + yattag.indent(doc.getvalue()),
                           data.get('format', "xml"))
コード例 #5
0
def api_auth_approve():
    """ Authenticate the user token provided.
    """
    user = User.load_by_name(current_user.musicbrainz_id)
    if "token" not in request.form:
        return render_template(
            "user/auth.html",
            user_id=current_user.musicbrainz_id,
            msg=
            "Missing required parameters. Please provide correct parameters and try again."
        )
    token = Token.load(request.form['token'])
    if not token:
        return render_template(
            "user/auth.html",
            user_id=current_user.musicbrainz_id,
            msg=
            "Either this token is already used or invalid. Please try again.")
    if token.user:
        return render_template(
            "user/auth.html",
            user_id=current_user.musicbrainz_id,
            msg=
            "This token is already approved. Please check the token and try again."
        )
    if token.has_expired():
        return render_template(
            "user/auth.html",
            user_id=current_user.musicbrainz_id,
            msg=
            "This token has expired. Please create a new token and try again.")
    token.approve(user.name)
    return render_template(
        "user/auth.html",
        user_id=current_user.musicbrainz_id,
        msg="Token %s approved for user %s, press continue in client." %
        (token.token, current_user.musicbrainz_id))
コード例 #6
0
def session_info(request, data):
    try:
        sk = data['sk']
        api_key = data['api_key']
        output_format = data.get('format', 'xml')
        username = data['username']
    except KeyError:
        raise InvalidAPIUsage(
            CompatError.INVALID_PARAMETERS,
            output_format=output_format)  # Missing Required Params

    session = Session.load(sk, api_key)
    if (not session) or User.load_by_name(username).id != session.user.id:
        raise InvalidAPIUsage(
            CompatError.INVALID_SESSION_KEY,
            output_format=output_format)  # Invalid Session KEY

    print("SESSION INFO for session %s, user %s" %
          (session.id, session.user.name))

    doc, tag, text = Doc().tagtext()
    with tag('lfm', status='ok'):
        with tag('application'):
            with tag('session'):
                with tag('name'):
                    text(session.user.name)
                with tag('key'):
                    text(session.id)
                with tag('subscriber'):
                    text('0')
                with tag('country'):
                    text('US')

    return format_response(
        '<?xml version="1.0" encoding="utf-8"?>\n' +
        yattag.indent(doc.getvalue()), output_format)
コード例 #7
0
 def test_user_load_by_name(self):
     user = User.load_by_name(self.user.name)
     assert isinstance(user, User) == True
     self.assertDictEqual(user.__dict__, self.user.__dict__)
コード例 #8
0
 def test_user_load_by_name(self):
     user = User.load_by_name(self.user.name)
     assert isinstance(user, User) == True
     self.assertDictEqual(user.__dict__, self.user.__dict__)