misskey-microblogger-demo/src/Generate/GenerateUser.py
Victor Westerlund ac5b9f3955 Add random user generator (#1)
This PR adds scripts for generating random users, and generating random relationships between these users. This PR also refactors config file loading and writing into a class.

Reviewed-on: https://codeberg.org/vlw/misskey-microblogger/pulls/1
Co-authored-by: Victor Westerlund <victor.vesterlund@gmail.com>
Co-committed-by: Victor Westerlund <victor.vesterlund@gmail.com>
2024-11-14 10:02:03 +00:00

107 lines
No EOL
3.3 KiB
Python

import math
import json
import typing
import random
from pathlib import Path
from ..User.User import USER_CONFIG_DIR, User
from ..Enums import RelationshipType, NoteTones, NoteMoods, NoteTypes
from misskey.enum import NoteVisibility
from random_username.generate import generate_username
# Use this file as a template for generated users
USER_TEMPLATE_FILE = Path.cwd() / "data" / "users_template.json"
# Minimum and maximum cooldown time for posting new notes
CONFIG_POST_MIN_COOLDOWN = 300 # 10 minutes
CONFIG_POST_MAX_COOLDOWN = 259200 # 48 hours
# Available preferred reactions
REACTIONS = [
"",
"👍",
"😆"
]
class GenerateUser():
def __init__(self):
self.username = self.gen_username()
# Load user template file
with open(USER_TEMPLATE_FILE, "r") as f:
self.config = json.load(f)
# Return a random unique username
@staticmethod
def gen_username() -> str:
username = generate_username(1)[0]
return username if not (USER_CONFIG_DIR / f"{username}.json").exists() else GenerateUser.gen_username()
# Generate a random time interval
@staticmethod
def gen_interval() -> dict:
def to_time(h: int) -> float:
return float(f"{h}.{random.randint(0, 59)}")
start_hour_from = random.randint(0, 22)
start_hour_end = start_hour_from + 1
end_hour_from = random.randint(min(start_hour_end + 1, 22), 23)
end_hour_to = min(end_hour_from + 1, 23)
return {
"start": {
"from": to_time(start_hour_from),
"to": to_time(start_hour_end)
},
"end": {
"from": to_time(end_hour_from),
"to": to_time(end_hour_to)
}
}
def set_api_key(self, key: str) -> None:
self.config["key"] = key
def save_config(self) -> bool:
USER_CONFIG_DIR.mkdir(exist_ok=True)
with open(USER_CONFIG_DIR / f"{self.username}.json", "w") as f:
json.dump(self.config, f, indent=4, ensure_ascii=False)
def autorun(self) -> User:
# Generate a random online interval
self.config["online"]["intervals"].append(self.gen_interval())
# Set a random percent for user to post new notes
self.config["actions"]["posts"]["public"]["percent"] = random.randint(0, 100)
self.config["actions"]["posts"]["public"]["cooldown"] = random.randint(CONFIG_POST_MIN_COOLDOWN, CONFIG_POST_MAX_COOLDOWN)
# Set random reply chance for each relationship
for visiblity in [NoteVisibility.PUBLIC, NoteVisibility.SPECIFIED]:
for relationship in RelationshipType:
self.config["actions"]["replies"][visiblity.value]["percent"][relationship.value] = random.randint(0, 100)
# Set random react chances for each relationship
for relationship in RelationshipType:
self.config["actions"]["reacts"]["percent"][relationship.value] = random.randint(0, 100)
# Set preferred reactions for each relationship
for relationship in RelationshipType:
self.config["actions"]["reacts"]["prefrerred_reaction"][relationship.value] = random.choice(REACTIONS)
# Set random personality tone
for x in NoteTones:
self.config["personality"]["tone"]["probability"][x.value] = random.randint(0, 100)
# Set random personality mood
for x in NoteMoods:
self.config["personality"]["mood"]["probability"][x.value] = random.randint(0, 100)
# Set random personality type
for x in NoteTypes:
self.config["personality"]["type"]["probability"][x.value] = random.randint(0, 100)
self.save_config()
return User(self.username)