Exemple #1
0
def insert_metadata(raw_data):
    url = raw_data.get("link")
    if url:
        with session_scope() as session:
            try:
                data = SongData(url)
                # This is a placeholder for until we can fetch real user details
                user = _insert_default_user(session)
                new_link = _insert_post(url, user, data.extraAttrs, raw_data,
                                        session)
                if new_link:
                    if data.track and data.artists:
                        new_song = _insert_song(data.track, session)
                        new_link.song_id = new_song.id
                        for artist_data in data.artists:
                            new_artist = _insert_artist(artist_data, session)
                            _insert_artist_song(new_artist, new_song, session)
                            _insert_song_genre(artist_data, new_song, session)
                    else:
                        new_song = _insert_song(data.extraAttrs, session)
                        new_link.song_id = new_song.id
            except ServerNotFoundError as e:
                logger.error("Google API unreachable!")
                raise e
            except Exception as e:
                if str(e) == "Unsupported URL!" or str(
                        e) == "Video unavailable!":
                    logger.error(str(e))
                    logger.error(f"FB Post URL: {raw_data['id']}")
                    raise e
                else:
                    data = SongData(url)
                    user = _insert_default_user(session)
                    new_link = _insert_post(url, user, data.extraAttrs,
                                            raw_data, session)
Exemple #2
0
    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
Exemple #3
0
    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
Exemple #4
0
    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
Exemple #5
0
def get_active_status():
    """ Checks the share_date of the latest post. """
    with session_scope() as session:
        posts = (session.query(UserPosts).order_by(
            UserPosts.share_date.desc()).limit(1).all())
        latest_post = posts[0]
        if datetime.now() - latest_post.share_date <= timedelta(
                days=int(MAX_ACTIVE_DAYS)):
            return True
        return False
Exemple #6
0
def get_song_url():
    with session_scope() as session:
        songs = []
        for u, l in session.query(
                UserPosts,
                Link).filter(UserPosts.link_id == Link.id).limit(50).all():
            if len(songs) <= 50:
                if get_spotify_id(l.url) is not None:
                    songs.append(get_spotify_id(l.url))
                else:
                    continue
            else:
                break
        return songs