mimizukari commited on
Commit
dd9cdfe
·
verified ·
1 Parent(s): 6d2f6d1

heavily optimized & added new features

Browse files
Files changed (1) hide show
  1. randumbizer.py +112 -31
randumbizer.py CHANGED
@@ -1,43 +1,124 @@
1
  import random
2
  import pyperclip
3
 
4
- def read_random_lines(artist_file="artist.txt", sheet_file="Sheet1.txt"):
5
- with open(artist_file, "r") as artist, open(sheet_file, "r") as sheet:
6
- artist_lines = artist.readlines()
7
- sheet_lines = [line.strip() for line in sheet.readlines()]
8
 
9
- user_input = ""
 
 
 
10
 
11
- while True:
12
- if user_input:
13
- matching_lines = [line for line in sheet_lines if user_input in line]
14
- if matching_lines:
15
- sheet_line = random.choice(matching_lines)
16
- else:
17
- sheet_line = random.choice(sheet_lines)
18
- else:
19
- sheet_line = random.choice(sheet_lines)
20
 
21
- splits = sheet_line.split(", ")
22
- if splits[0] in ["1girl", "1boy", "1other"]:
23
- insertion_index = 3
24
- else:
25
- insertion_index = 2
 
26
 
27
- num_artist_lines = random.randint(1, 4)
28
- selected_artist_lines = random.sample(artist_lines, num_artist_lines)
 
 
 
 
 
29
 
30
- pre_artists = ", ".join(splits[:insertion_index])
31
- artist_section = ", ".join(line.strip() for line in selected_artist_lines)
32
- post_artists = ", ".join(splits[insertion_index:])
33
 
34
- result = f"{pre_artists}, {artist_section}, {post_artists}" if post_artists else f"{pre_artists}, {artist_section}"
35
- result = result.rstrip(", ")
 
 
 
 
 
36
 
37
- print(result)
38
- pyperclip.copy(result)
39
- print("\nContent copied to clipboard.")
 
 
 
 
 
 
40
 
41
- user_input = input("\nPress Enter for random or type search term...\n").strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
- read_random_lines()
 
 
1
  import random
2
  import pyperclip
3
 
4
+ class ContentGenerator:
5
+ def __init__(self, artist_file="artist.txt", sheet_file="Sheet1.txt"):
6
+ self.artist_lines = self._load_lines(artist_file)
7
+ self.sheet_lines = self._load_lines(sheet_file)
8
 
9
+ @staticmethod
10
+ def _load_lines(filename):
11
+ with open(filename, "r") as file:
12
+ return [line.strip() for line in file.readlines()]
13
 
14
+ def _copy_and_print(self, result):
15
+ pyperclip.copy(result)
16
+ print(result)
17
+ print("\nContent copied to clipboard.")
 
 
 
 
 
18
 
19
+ def get_random_artists(self):
20
+ num_lines = random.randint(1, 4)
21
+ artists = random.sample(self.artist_lines, num_lines)
22
+ result = ", ".join(artist for artist in artists)
23
+ self._copy_and_print(result)
24
+ return result
25
 
26
+ def get_anime_screenshot(self):
27
+ line = random.choice(self.sheet_lines)
28
+ splits = line.split(", ")
29
+ insertion_index = 3 if splits[0] in ["1girl", "1boy", "1other"] else 2
30
+ result = f"{', '.join(splits[:insertion_index])}, anime screenshot, {', '.join(splits[insertion_index:])}".rstrip(", ")
31
+ self._copy_and_print(result)
32
+ return result
33
 
34
+ def process_character_lines(self, num_characters, tags=None):
35
+ available_lines = self.sheet_lines.copy()
36
+ selected_lines = []
37
 
38
+ if tags:
39
+ for tag in tags:
40
+ matches = [line for line in available_lines if tag in line]
41
+ if matches:
42
+ line = random.choice(matches)
43
+ selected_lines.append(line)
44
+ available_lines.remove(line)
45
 
46
+ remaining = num_characters - len(selected_lines)
47
+ if remaining > 0:
48
+ all_matches = list(set(line for tag in tags for line in available_lines if tag in line))
49
+ if all_matches:
50
+ selected_lines.extend(random.sample(all_matches, min(remaining, len(all_matches))))
51
+
52
+ remaining = num_characters - len(selected_lines)
53
+ if remaining > 0 and available_lines:
54
+ selected_lines.extend(random.sample(available_lines, min(remaining, len(available_lines))))
55
 
56
+ artists = ", ".join(random.choice(self.artist_lines) for _ in range(random.randint(1, 4)))
57
+ return self._format_characters(selected_lines, artists)
58
+
59
+ def _format_characters(self, lines, artists):
60
+ counts = {"girl": 0, "boy": 0, "other": 0}
61
+ characters = []
62
+ sources = set()
63
+
64
+ for line in lines:
65
+ splits = line.split(", ")
66
+ if "no humans" in splits:
67
+ splits.remove("no humans")
68
+
69
+ char_type = splits[0] if splits[0] in ["1girl", "1boy", "1other"] else "other"
70
+ if char_type == "1girl":
71
+ counts["girl"] += 1
72
+ char_type = "girl"
73
+ elif char_type == "1boy":
74
+ counts["boy"] += 1
75
+ char_type = "boy"
76
+ else:
77
+ counts["other"] += 1
78
+ char_type = "other"
79
+
80
+ source_idx = 2 if char_type in ["girl", "boy", "other"] else 1
81
+ if source_idx < len(splits):
82
+ sources.add(splits[source_idx])
83
+ splits.pop(source_idx)
84
+ splits[0] = char_type
85
+ characters.append(", ".join(splits))
86
+
87
+ count_str = ", ".join(
88
+ f"{count}{key}{'s' if count > 1 else ''}"
89
+ for key, count in counts.items() if count > 0
90
+ ) + (", " + ", ".join(sources) if sources else "")
91
+ result = f"{count_str}, {artists} | " + " | ".join(characters)
92
+ self._copy_and_print(result)
93
+ return result
94
+
95
+ def process_default(self, search_term=None):
96
+ lines = [line for line in self.sheet_lines if search_term in line] if search_term else self.sheet_lines
97
+ line = random.choice(lines if lines else self.sheet_lines)
98
+
99
+ splits = line.split(", ")
100
+ insertion_index = 3 if splits[0] in ["1girl", "1boy", "1other"] else 2
101
+ artists = ", ".join(random.sample(self.artist_lines, random.randint(1, 4)))
102
+ result = f"{', '.join(splits[:insertion_index])}, {artists}, {', '.join(splits[insertion_index:])}".rstrip(", ")
103
+ self._copy_and_print(result)
104
+ return result
105
+
106
+ def main():
107
+ generator = ContentGenerator()
108
+ while True:
109
+ user_input = input("\nEnter 'a' for artists, 'c' for anime, '1-6' [+tags], or search term: ").strip()
110
+
111
+ if user_input == "a":
112
+ generator.get_random_artists()
113
+ elif user_input == "c":
114
+ generator.get_anime_screenshot()
115
+ elif user_input and user_input[0].isdigit():
116
+ parts = user_input.split(maxsplit=1)
117
+ num = min(int(parts[0]), 6)
118
+ tags = [tag.strip() for tag in parts[1].split(',')] if len(parts) > 1 else None
119
+ generator.process_character_lines(num, tags)
120
+ else:
121
+ generator.process_default(user_input if user_input else None)
122
 
123
+ if __name__ == "__main__":
124
+ main()