def random_album(): form_data = flask.request.form try: album = albums_model.get_random_album() if album is None: return flask.current_app.not_found_message, 404 except DatabaseError as e: flask.current_app.logger.error('[db]: failed to get random album') flask.current_app.logger.error(f'[db]: {e}') return flask.current_app.db_error_message, 500 else: if 'post' in form_data.get('text', ''): response = { 'response_type': 'in_channel', 'text': f'{album.album_url}', 'unfurl_links': True } else: attachment = build_attachment( album.album_id, album.to_dict(), slack_blueprint.config['LIST_NAME'], tags=False, ) response = { 'response_type': 'ephemeral', 'text': 'Your random album is...', 'attachments': [attachment], } return flask.jsonify(response), 200
def random_album(): form_data = flask.request.form try: album = albums_model.get_random_album() if album is None: return flask.current_app.not_found_message, 404 except DatabaseError as e: flask.current_app.logger.error('[db]: failed to get random album') flask.current_app.logger.error(f'[db]: {e}') return flask.current_app.db_error_message, 500 else: if 'post' in form_data.get('text', ''): response = { 'response_type': 'in_channel', 'text': f'{album.album_url}', 'unfurl_links': True } else: attachment = build_attachment( album.album_id, album.to_dict(), slack_blueprint.config['LIST_NAME'], tags=False, ) response = { 'response_type': 'ephemeral', 'text': 'Your random album is...', 'attachments': [attachment], } return flask.jsonify(response), 200
def api_random(): try: album = albums_model.get_random_album() if album is None: return flask.jsonify({'text': 'not found'}), 404 response = { 'text': 'success', 'album': album.to_dict(), } return flask.jsonify(response), 200 except DatabaseError as e: print(f'[db]: failed to get random album') print(f'[db]: {e}') return flask.jsonify({'text': 'failed'}), 500
def api_random(): try: album = albums_model.get_random_album() if album is None: return flask.jsonify({'text': 'not found'}), 404 response = { 'text': 'success', 'album': album.to_dict(), } return flask.jsonify(response), 200 except DatabaseError as e: print(f'[db]: failed to get random album') print(f'[db]: {e}') return flask.jsonify({'text': 'failed'}), 500
def embedded_random(): try: album = albums_model.get_random_album() except DatabaseError as e: print('[db]: failed to get random album') print(f'[db]: {e}') return flask.current_app.db_error_message, 500 except TypeError: return flask.current_app.not_found_message, 404 if album: kwargs = dict(bot_url=site_blueprint.config['ALBUMLISTBOT_URL'], bookmarklet_url=flask.url_for('.bookmarklet', _external=True, _scheme="https"), list_name=site_blueprint.config['LIST_NAME'], album_id=album.album_id, name=album.album_name, artist=album.album_artist, album_url=album.album_url) return flask.render_template('index.html', **kwargs) return '', 200
def post_random_album(): if not channel or not slack_token: print('[random]: missing environment variables') return try: album = albums_model.get_random_album() if album is None: print('[random]: no random album found') return except DatabaseError as e: print('[db]: failed to get random album') print(f'[db]: {e}') return else: attachment = build_attachment( album.album_id, album.to_dict(), list_name, tags=True, ) print(f'[random]: posting random album to {channel}') text = f":new_moon_with_face: Today's album of the day is:" slack.chat.post_message(channel, text, attachments=[attachment])