Example #1
0
def seed_posts(num_of_posts):
    """
    Seed the database with some posts.
    """
    users = User.query.all()
    posts1 = []
    posts2 = []
    post_objs = []

    print('Fetching posts...')

    for i in range(int(num_of_posts/10)):
        posts1.extend(requests.get(
            'https://official-joke-api.appspot.com/jokes/ten').json())

    for i in range(20):
        posts2.extend(requests.get(
            f'https://icanhazdadjoke.com/search?limit=30&page={i}',
            headers={'accept': 'application/json'}).json().get('results'))

    try:
        print('Saving posts to database...')
        for p in posts1:
            user = random.choice(users)

            post = Post()
            post.body = f"{p.get('setup')} - {p.get('punchline')}"
            post.user_id = user.id
            post.likes.extend(random.sample(users, k=random.randrange(36)))
            post.created_on = random_timestamp(
                datetime.datetime(2019, 1, 1), datetime.datetime(2019, 7, 30))
            # post.tags.extend(item.)
            post_objs.append(post)

        for item in posts2:
            user = random.choice(users)

            post = Post()
            post.body = item.get('joke')
            post.user_id = user.id
            post.created_on = random_timestamp(
                datetime.datetime(2019, 7, 1), datetime.datetime(2020, 1, 31))
            post.likes.extend(random.sample(users, k=random.randrange(30)))
            post_objs.append(post)

        db.session.add_all(post_objs)
        db.session.commit()

        print(f'Post table seeded with {num_of_posts} posts...')
    except Exception as error:
        print(f'Error: {error}')
Example #2
0
def seed_comments(num_of_comments):
    users = User.query.all()
    posts = Post.query.all()
    comments_list = []

    try:
        print('Fetching comments...')
        for i in range(int(num_of_comments/500)):
            comments_list.extend(requests.get(
                'https://jsonplaceholder.typicode.com/comments').json())

        comments = [comment.get('body') for comment in comments_list]

        comments_list = []

        for c in comments:
            user = random.choice(users)
            post = random.choice(posts)

            comment = Post(body=c)
            comment.user_id = user.id
            comment.comment_id = post.id
            comment.created_on = random_timestamp(
                datetime(2021, 1, 1), datetime(2021, 2, 11))
            comment.likes.extend(random.sample(users, k=random.randrange(12)))
            comments_list.append(comment)

        print('Saving to database...')
        db.session.add_all(comments_list)
        db.session.commit()

    except Exception as error:
        print(f'Error: {error}')
Example #3
0
def seed_posts(num_of_posts):
    """Seed the database with some posts."""
    users = User.query.all()
    posts1 = []
    posts2 = []
    post_objs = []
    tag_list = ['safe', 'broad', 'witty', 'humorous']

    print('Fetching posts...')

    for i in range(int(num_of_posts/15)):
        posts1.extend(requests.get(
            'https://official-joke-api.appspot.com/jokes/ten').json())

    for i in range(int(num_of_posts/15)):
        posts2.extend(requests.get(
            'https://sv443.net/jokeapi/v2/joke/Any?\
                blacklistFlags=nsfw,racist,sexist&type=twopart&amount=10',
            headers={'accept': 'application/json'}).json().get('jokes'))

    try:
        print('Saving posts to database...')
        for p in posts1:
            user = random.choice(users)

            tag = p.get('type')
            tags = [tag, random.choice(tag_list)]
            body = f"{p.get('setup')} - {p.get('punchline')}"

            post = Post()
            post.body = body
            post.user_id = user.id

            post.likes.extend(random.sample(users, k=random.randrange(36)))
            post.created_on = random_timestamp(
                datetime(2020, 7, 1), datetime(2020, 9, 28))

            for t in tags:
                tag = Tag.query.filter_by(name=t).first()

                if tag:
                    post.tags.append(tag)
                else:
                    tag = Tag(name=t)
                    db.session.add(tag)
                    post.tags.append(tag)

            post_objs.append(post)

        for p in posts2:
            user = random.choice(users)

            tag = p.get('category').lower()
            flags = p.get('flags')
            tags = [key.lower() for key in flags if flags[key] is True]
            tags.append(tag)
            if bool(p.get('safe')):
                tags.append('safe')

            body = f"{p.get('setup')} - {p.get('delivery')}"

            post = Post()
            post.body = body
            post.user_id = user.id
            post.created_on = random_timestamp(
                datetime(2020, 9, 1), datetime(2020, 12, 31))
            post.likes.extend(random.sample(users, k=random.randrange(30)))

            for t in tags:
                tag = Tag.query.filter_by(name=t).first()

                if tag:
                    post.tags.append(tag)
                else:
                    tag = Tag(name=t)
                    db.session.add(tag)
                    post.tags.append(tag)

            post_objs.append(post)

        db.session.add_all(post_objs)
        db.session.commit()

        print(f'Post table seeded with {num_of_posts} posts...')
    except Exception as error:
        print(f'Error: {error}')