-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
76 lines (55 loc) · 1.97 KB
/
bot.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 requests
import random
from faker import Faker
fake = Faker()
from faker.providers import BaseProvider
fake.add_provider(BaseProvider)
BASE_URL = "http://127.0.0.1:8000/"
API_USER_CREATE_URL = BASE_URL + "api/users/create/"
OBTAIN_TOKEN_URL = BASE_URL + "api/token/"
CREATE_POST_URL = BASE_URL + "api/posts/"
with open("config.json") as f:
content = f.read()
json_data = json.loads(content)
USER_PREFIX = 'botuser'
def create_user(i):
data = {
'username': USER_PREFIX + str(i),
'email': fake.email(),
'password': fake.password()
}
r = requests.post(API_USER_CREATE_URL, data=data)
return data['username'], data['password']
def get_token(username, password):
r = requests.post(OBTAIN_TOKEN_URL, data={
'username': username,
'password': password
})
return json.loads(r.text).get('access')
def create_post(username, password):
token = get_token(username, password)
headers = {'Authorization': 'Bearer ' + token}
r = requests.post(CREATE_POST_URL, data={
'title': fake.sentence(),
'text': fake.sentence(100)
}, headers=headers)
return json.loads(r.text).get('id')
def like_post(post_id, username, password):
LIKE_URL = BASE_URL + f"api/posts/{post_id}/like/"
token = get_token(username, password)
headers = {'Authorization': 'Bearer ' + token}
r = requests.get(LIKE_URL, headers=headers)
print(f"like {post_id}: status {r.status_code}")
for i in range(json_data['number_of_users']):
username, password = create_user(i)
user_posts_count = random.randint(1, json_data['max_posts_per_user'])
posts = []
for p in range(user_posts_count):
post_id = create_post(username, password)
posts.append(post_id)
print(f'Created {user_posts_count} posts from {username}')
print(posts)
for l in range(json_data['max_likes_per_user']):
post_id = random.choice(posts)
like_post(post_id, username, password)