示例#1
0
def get_posts():
    props = {'currentPage': 'posts'}
    base = request.args.to_dict()
    base.pop('o', None)

    query = request.args.get('q')
    offset = parse_int(request.args.get('o'), 0)
    props['limit'] = 25

    if not query or len(query) < 3:
        results = get_all_posts(offset)
        props['count'] = count_all_posts()
    else:
        results = get_all_posts_for_query(query, offset)
        props['count'] = count_all_posts_for_query(query)

    (result_previews, result_attachments, result_flagged, result_after_kitsune,
     result_is_image) = get_render_data_for_posts(results)

    response = make_response(
        render_template('posts.html',
                        props=props,
                        results=results,
                        base=base,
                        result_previews=result_previews,
                        result_attachments=result_attachments,
                        result_flagged=result_flagged,
                        result_after_kitsune=result_after_kitsune,
                        result_is_image=result_is_image), 200)
    response.headers['Cache-Control'] = 'no-store, max-age=0'
    return response
示例#2
0
def list():
    props = {'currentPage': 'artists'}
    base = request.args.to_dict()
    base.pop('o', None)

    # q = request.args.get('q')
    # commit = request.args.get('commit')
    # service = request.args.get('service')
    # sort_by = request.args.get('sort_by')
    # order = request.args.get('order')
    offset = parse_int(request.args.get('o'), 0)
    limit = 25

    (results, total_count) = ([], 0)
    results = get_top_artists_by_faves(offset, limit)
    total_count = get_count_of_artists_faved()
    props['display'] = 'most popular artists'

    props['count'] = total_count
    props['limit'] = limit
    response = make_response(
        render_template('artists.html',
                        props=props,
                        results=results,
                        base=base), 200)
    response.headers['Cache-Control'] = 's-maxage=60'
    return response
示例#3
0
文件: dms.py 项目: BassOfBass/Kemono2
def get_dms():
    base = request.args.to_dict()
    base.pop('o', None)

    offset = parse_int(request.args.get('o'), 0)  # noqa F811
    query = request.args.get('q')
    limit = limit_int(int(request.args.get('limit') or 25), 50)
    dms = None
    total_count = None

    if query is None:
        (dms, total_count) = get_dm_page(offset, limit)
    else:
        (dms, total_count) = do_dm_search(query, offset, limit)

    artists = [get_artist(dm.service, dm.user) for dm in dms]

    props = DMsProps(
        count=total_count,
        limit=limit,
        dms=dms,
        artists=artists
    )

    response = make_response(render_template(
        'all_dms.html',
        props=props,
        base=base,
    ), 200)
    response.headers['Cache-Control'] = 's-maxage=60'
    return response
示例#4
0
def get(service: str, artist_id: str):
    # cursor = get_cursor()

    base = request.args.to_dict()
    base.pop('o', None)
    base["service"] = service
    base["artist_id"] = artist_id

    offset = parse_int(request.args.get('o'), 0)
    query = request.args.get('q')
    limit = limit_int(int(request.args.get('limit') or 25), 50)

    favorited = False
    account = load_account()
    if account is not None:
        favorited = is_artist_favorited(account['id'], service, artist_id)

    (posts, total_count) = ([], 0)
    if not query or len(query) < 3:
        (posts, total_count) = get_artist_post_page(artist_id, service, offset,
                                                    limit)
    else:
        (posts, total_count) = do_artist_post_search(artist_id, service, query,
                                                     offset, limit)

    artist = get_artist(service, artist_id)
    if artist is None:
        return redirect(url_for('artists.list'))
    display_data = make_artist_display_data(artist)
    dm_count = count_user_dms(service, artist_id)

    (result_previews, result_attachments, result_flagged, result_after_kitsune,
     result_is_image) = get_render_data_for_posts(posts)

    props = ArtistPageProps(id=artist_id,
                            service=service,
                            session=session,
                            name=artist['name'],
                            count=total_count,
                            limit=limit,
                            favorited=favorited,
                            artist=artist,
                            display_data=display_data,
                            dm_count=dm_count)

    response = make_response(
        render_template('user.html',
                        props=props,
                        base=base,
                        results=posts,
                        result_previews=result_previews,
                        result_attachments=result_attachments,
                        result_flagged=result_flagged,
                        result_after_kitsune=result_after_kitsune,
                        result_is_image=result_is_image), 200)
    response.headers['Cache-Control'] = 's-maxage=60'
    return response
示例#5
0
def list_banned_artists(page: str):
    total_count = count_banned_artists()
    limit = DEFAULT_PAGE_LIMIT
    current_page = parse_int(page)
    total_pages = math.floor(total_count / limit) + 1
    is_valid_page = current_page and current_page <= total_pages

    # current page is zero or greater than total pages
    if (not is_valid_page):
        redirect_url = url_for(
            '.list_banned_artists',
            page=total_pages,
            **request.args
        )

        return redirect(redirect_url)

    is_last_page = current_page == total_pages
    pagination_init = TDPaginationInit(
        current_page=current_page,
        limit=limit,
        total_count=total_count,
        total_pages=total_pages
    )
    offset = (current_page - 1) * limit
    sql_limit = total_count - offset if is_last_page else limit
    pagination_db = TDPaginationDB(
        pagination_init=pagination_init,
        offset=offset,
        sql_limit=sql_limit
    )

    banned_artists = get_banned_artists(pagination_db)

    pagination = TDPagination(
        total_count=total_count,
        total_pages=total_pages,
        current_page=current_page,
        limit=limit
    )

    response_body = TDArtistResponse(
        pagination=pagination,
        banned_artists=banned_artists
    )

    api_response = TDAPIResponseSuccess(
        is_successful=True,
        data=response_body
    )

    return make_response(jsonify(api_response), 200)
示例#6
0
def list():
    account = load_account()
    if account is None:
        flash(
            'We now support accounts! Register for an account and your current favorites will automatically be added to your account.'
        )
        return redirect(url_for('account.get_login'))

    props = {'currentPage': 'favorites'}
    base = request.args.to_dict()
    base.pop('o', None)

    favorites = []
    fave_type = get_value(request.args, 'type', 'artist')
    if fave_type == 'post':
        favorites = get_favorite_posts(account['id'])
        sort_field = restrict_value(get_value(request.args, 'sort'),
                                    ['faved_seq', 'published'], 'faved_seq')
    else:
        favorites = get_favorite_artists(account['id'])
        sort_field = restrict_value(get_value(request.args, 'sort'),
                                    ['faved_seq', 'updated'], 'updated')

    offset = parse_int(request.args.get('o'), 0)
    sort_asc = True if get_value(request.args, 'order') == 'asc' else False
    results = sort_and_filter_favorites(favorites, offset, sort_field,
                                        sort_asc)

    props['fave_type'] = fave_type
    props['sort_field'] = sort_field
    props['sort_asc'] = sort_asc
    props['count'] = len(favorites)
    props['limit'] = 25

    response = make_response(
        render_template(
            'favorites.html',
            props=props,
            base=base,
            source='account',
            results=results,
        ), 200)
    response.headers['Cache-Control'] = 's-maxage=60'
    return response
示例#7
0
def updated():
    props = {'currentPage': 'artists'}
    base = request.args.to_dict()
    base.pop('o', None)

    offset = parse_int(request.args.get('o'), 0)
    limit = 25

    props['limit'] = limit

    results = get_artists_by_update_time(offset=offset)
    props["count"] = len(get_all_non_discord_artists())

    base = request.args.to_dict()
    base.pop('o', None)

    response = make_response(
        render_template('updated.html',
                        base=base,
                        props=props,
                        results=results), 200)
    response.headers[
        'Cache-Control'] = 'max-age=60, public, stale-while-revalidate=2592000'
    return response
示例#8
0
def list_artists(page: str):
    result = validate_artists_request(request)

    if not result["is_successful"]:
        api_response = TDAPIResponseFailure(
            is_successful=False,
            errors=result["errors"]
        )
        response = make_response(jsonify(api_response), 422)
        return response

    search_params: TDArtistsParams = result['data']
    # artist_name = search_params["name"]
    current_page = parse_int(page)
    limit = DEFAULT_PAGE_LIMIT
    artist_count = count_artists(
        search_params["service"],
        # artist_name
    )
    total_pages = math.floor(artist_count / limit) + 1
    is_valid_page = current_page and current_page <= total_pages

    # current page is zero or greater than total pages
    if (not is_valid_page):
        redirect_url = url_for('.list_artists', page=total_pages, **request.args)

        return redirect(redirect_url)

    is_last_page = current_page == total_pages
    pagination_init = TDPaginationInit(
        current_page=current_page,
        limit=limit,
        total_count=artist_count,
        total_pages=total_pages
    )

    offset = (current_page - 1) * limit
    sql_limit = artist_count - offset if is_last_page else limit
    pagination_db = TDPaginationDB(
        pagination_init=pagination_init,
        offset=offset,
        sql_limit=sql_limit
    )
    artists = get_artists(
        pagination_db,
        search_params,
    )
    pagination = TDPagination(
        total_count=artist_count,
        total_pages=total_pages,
        current_page=current_page,
        limit=limit
    )
    response_body = TDArtistResponse(
        pagination=pagination,
        artists=artists
    )
    api_response = TDAPIResponseSuccess(
        is_successful=True,
        data=response_body
    )
    response = make_response(jsonify(api_response), 200)
    response.headers['Cache-Control'] = 's-maxage=60'

    return response