forked from jinyus/related_post_gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_fake_posts.py
73 lines (59 loc) · 1.24 KB
/
gen_fake_posts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import json
import random
import string
import sys
from typing import Dict, List
from faker import Faker
from nanoid import generate
fake = Faker()
tags = [
"docker",
"python",
"javascript",
"golang",
"rust",
"java",
"kafka",
"redis",
"postgres",
"mongodb",
"mysql",
"cassandra",
"react",
"vue",
"angular",
"svelte",
"flask",
"django",
"asp.net",
"spring",
"express",
"flutter",
"unreal",
"godot",
"wasm",
"tutorial",
"course",
"game",
]
def gen_posts(count: int):
posts = map(
lambda x: {
"_id": generate_random_string(8),
"title": fake.sentence(),
"tags": sorted(random.sample(tags, random.randint(4, 5))),
},
range(count),
)
json_to_file("posts.json", list(posts))
def json_to_file(filepath: str, data: List | Dict):
with open(filepath, "w") as myfile:
myfile.write(json.dumps(data))
def generate_random_string(length: int) -> str:
return generate(string.ascii_lowercase + string.digits, size=length)
if __name__ == "__main__":
num = sys.argv[-1]
if num.isdigit():
gen_posts(int(num))
else:
sys.exit("Please enter a number")