def latest_import(): """ Get and update the timestamp of the newest listen submitted in previous imports to ListenBrainz. In order to get the timestamp for a user, make a GET request to this endpoint. The data returned will be JSON of the following format: .. code-block:: json { "musicbrainz_id": "the MusicBrainz ID of the user", "latest_import": "the timestamp of the newest listen submitted in previous imports. Defaults to 0" } :param user_name: the MusicBrainz ID of the user whose data is needed :statuscode 200: Yay, you have data! :resheader Content-Type: *application/json* In order to update the timestamp of a user, you'll have to provide a user token in the Authorization Header. User tokens can be found on https://listenbrainz.org/profile/ . The JSON that needs to be posted must contain a field named `ts` in the root with a valid unix timestamp. :reqheader Authorization: Token <user token> :reqheader Content-Type: *application/json* :statuscode 200: latest import timestamp updated :statuscode 400: invalid JSON sent, see error message for details. :statuscode 401: invalid authorization. See error message for details. """ if request.method == 'GET': user_name = request.args.get('user_name', '') user = db_user.get_by_mb_id(user_name) if user is None: raise APINotFound( "Cannot find user: {user_name}".format(user_name=user_name)) return jsonify({ 'musicbrainz_id': user['musicbrainz_id'], 'latest_import': 0 if not user['latest_import'] else int( user['latest_import'].strftime('%s')) }) elif request.method == 'POST': user = validate_auth_header() try: ts = ujson.loads(request.get_data()).get('ts', 0) except ValueError: raise APIBadRequest('Invalid data sent') try: db_user.increase_latest_import(user['musicbrainz_id'], int(ts)) except DatabaseException as e: current_app.logger.error( "Error while updating latest import: {}".format(e)) raise APIInternalServerError( 'Could not update latest_import, try again') return jsonify({'status': 'ok'})
def latest_import(): """ Get and update the timestamp of the newest listen submitted in previous imports to ListenBrainz. In order to get the timestamp for a user, make a GET request to this endpoint. The data returned will be JSON of the following format: { 'musicbrainz_id': the MusicBrainz ID of the user, 'latest_import': the timestamp of the newest listen submitted in previous imports. Defaults to 0 } :param user_name: the MusicBrainz ID of the user whose data is needed :statuscode 200: Yay, you have data! :resheader Content-Type: *application/json* In order to update the timestamp of a user, you'll have to provide a user token in the Authorization Header. User tokens can be found on https://listenbrainz.org/profile/ . The JSON that needs to be posted must contain a field named `ts` in the root with a valid unix timestamp. :reqheader Authorization: Token <user token> :statuscode 200: latest import timestamp updated :statuscode 400: invalid JSON sent, see error message for details. :statuscode 401: invalid authorization. See error message for details. """ if request.method == 'GET': user_name = request.args.get('user_name', '') user = db_user.get_by_mb_id(user_name) if user is None: raise NotFound("Cannot find user: {user_name}".format(user_name=user_name)) return jsonify({ 'musicbrainz_id': user['musicbrainz_id'], 'latest_import': 0 if not user['latest_import'] else int(user['latest_import'].strftime('%s')) }) elif request.method == 'POST': user = _validate_auth_header() try: ts = ujson.loads(request.get_data()).get('ts', 0) except ValueError: raise BadRequest('Invalid data sent') try: db_user.increase_latest_import(user['musicbrainz_id'], int(ts)) except DatabaseException as e: current_app.logger.error("Error while updating latest import: {}".format(e)) raise InternalServerError('Could not update latest_import, try again') return jsonify({'status': 'ok'})
def test_increase_latest_import(self): user = db_user.get_or_create(4, 'testlatestimportuser') val = int(time.time()) db_user.increase_latest_import(user['musicbrainz_id'], val) user = db_user.get_by_mb_id(user['musicbrainz_id']) self.assertEqual(val, int(user['latest_import'].strftime('%s'))) db_user.increase_latest_import(user['musicbrainz_id'], val - 10) user = db_user.get_by_mb_id(user['musicbrainz_id']) self.assertEqual(val, int(user['latest_import'].strftime('%s'))) val += 10 db_user.increase_latest_import(user['musicbrainz_id'], val) user = db_user.get_by_mb_id(user['musicbrainz_id']) self.assertEqual(val, int(user['latest_import'].strftime('%s')))