-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
131 lines (106 loc) · 4.07 KB
/
build.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#TODO: maybe just posts to discord? is that possible?
import re
import argparse
import json
parser = argparse.ArgumentParser()
parser.add_argument("keywords", help="what keywords you want to use")
parser.add_argument("input", help="name of input text file")
parser.add_argument("-n", "--nitro", help = "use --nitro if you have nitro and want the whole output in one message", action="store_true")
#parser.add_argument("-d", "--discord", help = "use --discord {channel_name} to post directly to discord - channel name mapping WIP", action = "store")
args = parser.parse_args()
def import_settings(setting):
#Imports pre-existing settings from json's and initializes constants
#uses specified keyword setting
with open(f"keywords_{setting}.json","r") as f:
keywords = json.load(f)
#define constants
global punList
punList = [
".", ">", "<", ",", "/", "?", '"', ":", ";", "[",
"]", "{", "}", "|", "~", "+", "=", "!", "$", "%"
]
return keywords
def re_text(text):
#opens text file, splits text into words & whitespace, splits punctuation from words (will rejoin later)
with open(text, "r", encoding='UTF-8') as f:
inp = f.read()
for pun in punList:
inp = inp.replace(pun, f" {pun}")
retext = re.split(r'(\s)', inp)
return retext
def init_colors(keywords):
# Initializes all variables used in the text replacement.
# TODO: make this file inputting/UI-able
colors = {
"start" : r"[1;", #start code for ansi color
"end" : r"[0m", #end code for ansi color
"black": r"30m", #color codes
"red" : r"31m",
"green" : r"32m",
"yellow" : r"33m",
"blue" : r"34m",
"magenta" : r"35m",
"cyan": r"36m",
"white": r"37m"
}
colordict = {}
#iterates over every color in keywords
#for each keyword, splits, lowers, and uses ___s format to
#associate each word with a color in colordict
for color in keywords:
for word in keywords[color]:
for x in word.split():
colordict[x.lower()] = colors[color]
colordict[(x.lower() + "s")] = colors[color]
colordict[(x.lower() + "ed")] = colors[color]
return colors, colordict
def colorize(colors, colordict, retext):
# loops over every "word" in retext and colors them in according to colordict
finaltldr = []
for word in retext:
if word.lower() in colordict:
tempstr = colors["start"] + colordict[word.lower()] + word + colors["end"]
finaltldr.append(tempstr)
else:
finaltldr.append(word)
joined = "".join(finaltldr)
#Reversing space in front of punctuation
for pun in punList:
joined = joined.replace(f" {pun}", pun)
print(joined)
print(len(joined))
return joined
def output(joined):
#outputs the final file
#amount of outputs is determined by nitro/message length
msgMax = 1900 if not args.nitro else 3900
if len(joined) > msgMax:
lines = joined.split("\n")
print("Creating Chunks")
chunks = []
curchunk = ""
for line in lines:
curchunk += line + "\n"
if len(curchunk)>1500:
chunks.append(curchunk)
curchunk = ""
print("Chunk Written")
if curchunk != "":
chunks.append(curchunk)
print("Chunk Written")
for i, chunk in enumerate(chunks):
output = "```ansi" + "\n" + chunk + "\n" + "```"
with open(f"out{i}.txt", "w", encoding="utf-16") as f:
f.write(output)
print(f"outputted file {i}")
else:
output = "```ansi" + "\n" + joined + "\n" + "```"
with open("out0.txt", "w", encoding="utf-16") as f:
f.write(output)
print("outputted")
if __name__ == "__main__":
keywords = import_settings(args.keywords)
retext = re_text(args.input)
colors, colordict = init_colors(keywords)
joined = colorize(colors, colordict, retext)
output(joined)