Example #1
0
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
Example #2
0
def build_bandcamp_search_response(album_details, max_attachments=None):
    album_map = {
        result_id: {
            'album': details[0],
            'artist': details[1],
            'url': details[2],
            'img': details[3],
            'tags': [],
        }
        for result_id, details in enumerate(album_details)
    }
    attachments = list(
        reversed([
            build_attachment(album_id,
                             album_details,
                             'bandcamp',
                             tags=False,
                             scrape=True)
            for album_id, album_details in album_map.items()
        ]))
    text = f'Your bandcamp search returned {len(album_map)} results'
    if max_attachments and len(album_map) > max_attachments:
        text += f' (but we can only show you {max_attachments})'
    return {
        'text': text,
        'attachments': attachments[:max_attachments],
    }
Example #3
0
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
Example #4
0
def build_search_response(albums, list_name, max_attachments=None, delete=False, add_to_my_list=False,
                          remove_from_my_list=False):
    details = albums_model.Album.details_map_from_albums(albums)
    attachments = [
        build_attachment(album_id, album_details, list_name,
                         add_to_my_list=add_to_my_list,
                         remove_from_my_list=remove_from_my_list,
                         delete=delete)
        for album_id, album_details in details.items()
    ]
    text = f'Your {list_name} search returned {len(details)} results'
    if max_attachments and len(details) > max_attachments:
        text += f' (but we can only show you {max_attachments})'
    return {
        'text': text,
        'attachments': attachments[:max_attachments],
    }
Example #5
0
def build_bandcamp_search_response(album_details, max_attachments=None):
    album_map = {
        result_id: {
            'album': details[0],
            'artist': details[1],
            'url': details[2],
            'img': details[3],
            'tags': [],
        } for result_id, details in enumerate(album_details)
    }
    attachments = list(reversed([
        build_attachment(album_id, album_details, 'bandcamp', tags=False, scrape=True)
        for album_id, album_details in album_map.items()
    ]))
    text = f'Your bandcamp search returned {len(album_map)} results'
    if max_attachments and len(album_map) > max_attachments:
        text += f' (but we can only show you {max_attachments})'
    return {
        'text': text,
        'attachments': attachments[:max_attachments],
    }
Example #6
0
def build_search_response(albums,
                          list_name,
                          max_attachments=None,
                          delete=False,
                          add_to_my_list=False,
                          remove_from_my_list=False):
    details = albums_model.Album.details_map_from_albums(albums)
    attachments = [
        build_attachment(album_id,
                         album_details,
                         list_name,
                         add_to_my_list=add_to_my_list,
                         remove_from_my_list=remove_from_my_list,
                         delete=delete)
        for album_id, album_details in details.items()
    ]
    text = f'Your {list_name} search returned {len(details)} results'
    if max_attachments and len(details) > max_attachments:
        text += f' (but we can only show you {max_attachments})'
    return {
        'text': text,
        'attachments': attachments[:max_attachments],
    }
Example #7
0
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])