def get_posts_in_interval( url, start, limit, from_=datetime.now() - timedelta(days=7), to_=datetime.now() ): try: session = session_factory() query_posts = ( session.query(UserPosts) .filter(UserPosts.share_date >= from_) .filter(UserPosts.share_date <= to_) .order_by(UserPosts.likes_count.desc()) ) total = query_posts.count() posts = query_posts.all() paginated_response = get_paginated_response(posts, url, total, start, limit) paginated_response["posts"] = [ format(session, post) for post in paginated_response["posts"] ] return paginated_response, 200 except BaseException: LOG.error( f"Failed to fetch data with params from_ = {from_}, to_ = {to_}. Try later.", exc_info=True, ) response_object = { "status": "fail", "message": "Try again", } return response_object, 500
def get_underrated_posts(url, start, limit): with session_scope() as session: try: total = session.query(UserPosts).count() posts = ( session.query(UserPosts) .join(Link, UserPosts.link_id == Link.id) .filter(Link.custom_popularity < MAX_UNDERRATED_CUSTOM_POPULARITY) .filter(Link.views < MAX_UNDERRATED_VIEWS) .order_by(UserPosts.share_date.desc()) .offset(start) .limit(limit) ) paginated_response = get_paginated_response( posts, url, total, start=start, limit=limit ) paginated_response["posts"] = [ format(session, post) for post in paginated_response["posts"] ] return paginated_response, 200 except BaseException: LOG.error( f"Failed to fetch data with param start = {start} limit_ = {limit}. Try later.", exc_info=True, ) response_object = { "status": "fail", "message": "Try again", } return response_object, 500
def get_random_posts(url, start, limit): with session_scope() as session: try: total = session.query(UserPosts).count() posts = ( session.query(UserPosts) .order_by(func.random()) .offset(start) .limit(limit) ) paginated_response = get_paginated_response( posts, url, total, start=start, limit=limit ) paginated_response["posts"] = [ format(session, post) for post in paginated_response["posts"] ] return paginated_response, 200 except BaseException: LOG.error( f"Failed to fetch data with param start = {start} limit_ = {limit}. Try later.", exc_info=True, ) response_object = { "status": "fail", "message": "Try again", } return response_object, 500
def get_popular_posts(url, n, start, limit): """ Retrieves the most popular posts in the past n days""" with session_scope() as session: try: all_posts = ( session.query(UserPosts) .filter(UserPosts.share_date <= datetime.now() + timedelta(days=1)) .filter(UserPosts.share_date >= datetime.now() - timedelta(days=n)) .order_by(UserPosts.likes_count.desc()) ) total = all_posts.count() posts = all_posts.offset(start).limit(limit).all() paginated_response = get_paginated_response( posts, url, start=start, limit=limit, total=total ) paginated_response["posts"] = [ format(session, post) for post in paginated_response["posts"] ] return paginated_response, 200 except BaseException: LOG.error( f"Failed to fetch data with param n = {n}, start = {start}, limit = {limit} . Try later.", exc_info=True, ) response_object = { "status": "fail", "message": "Try again", } return response_object, 500