-
Notifications
You must be signed in to change notification settings - Fork 0
/
external_parsing.py
192 lines (146 loc) · 5.33 KB
/
external_parsing.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from gdparser import GDParser
# Mods are just abusive dictators but I'll give you your
# little badges here just so I can point you little tyrants
AUTHORITY = {
1:b"(Mod)",
2:b"(Eldermod)"
}
HTML_CSS = """<html>
<style>
/* Background objects to load... */
.Comment_A {
background-color:#2b4563;
color:rgb(255, 255, 255);
}
.Comment_B {
background-color:#1d2e42;
color:rgb(255, 255, 255);
}
.Username {
display:inline-block;
color:rgb(255, 255, 0);
font-family: Arial, Helvetica, sans-serif;
font-size: medium;
text-indent: 5px;
line-height: 2em
}
.Content {
color:rgb(255, 255, 255);
font-family: Arial, Helvetica, sans-serif;
font-size: small;
text-indent: 10px;
/* min-width: 936px; */
min-height: 20px;
line-height: 2em
}
.Special {
color:rgb(75, 255, 75);
font-family: Arial, Helvetica, sans-serif;
font-size: small;
text-indent: 10px;
/* min-width: 936px; */
min-height: 20px;
line-height: 2em
}
.time {
color: rgb(0, 0, 0);
font-family: Arial, Helvetica, sans-serif;
text-align:right;
transform: translate(-3vh,-1vh)
}
.votes {
line-height: 2em;
float:right;
color:#ffffff;
margin-right: 20px;
font-family: Arial, Helvetica, sans-serif;
}
.modbadge {
width: 2.2%;
transform: translate(0.5vh,-1vh);
}
a#link {
display:inline-block;
font-size: small;
font-family: Arial, Helvetica, sans-serif;
text-align:right;
text-indent: 5px;
margin-bottom:5px;
}
.remarks {
color: rgb(53, 145, 148);
font-family: Arial, Helvetica, sans-serif;
font-size: small;
}
</style>
<title>{title}</title>
<body style="background-color:#111b27">
<div class="remarks">Generated Through the DailyChatDownloader GUI Version</div>
<hr>
"""
# TODO Add links to Commandline version or a downloader of some sort...
HTML_BOTTOM = b'\n\t<hr></body>\n</html>'
def parse_comments(line:bytes):
return GDParser(line)
def to_html(line:bytes):
# Creates a pattern to use
use_a = True
for comment in parse_comments(line):
html = b'\t\t<div class="Comment_A">' if use_a == True else b'\t\t<div class="Comment_B">'
use_a = False if use_a else True
html += (b'\n\t\t\t\t<div class="Username">' + comment.author)
html += b" " + AUTHORITY.get(comment.modBadge, b"")
if comment.likes < 0:
html += b" (disliked) "
elif comment.spam:
html += b" (Spam) "
html += b"</div>\n"
html += b'\n\t\t\t\t<div class="votes"> [ Votes: ' + f"{comment.likes}".encode(errors="replace") + b" ]</div>\n"
if comment.moderatorChatColor:
html += b'\t\t\t\t<div class="Special">' + comment.body + b'</div>\n'
else:
html += b'\t\t\t\t<div class="Content">' + comment.body + b'</div>\n'
html += b'\t\t\t\t<div class="time">' + comment.age + b' ago</div>\n\t\t\t'
html += b'<a id="link" href="https://gdbrowser.com/u/' + comment.authorAccountID + b'">Visit Account #' + comment.authorAccountID + b'</a>\n\t\t</div>\n'
yield html
def to_json(line:bytes):
for p in GDParser(line):
yield p.as_json
def to_text(line:bytes):
for comment in GDParser(line):
l = comment.author
l += AUTHORITY.get(comment.modBadge, b"")
l += b": "
l += comment.body
l += (b"\tTime:" + comment.age)
l += (f"\tVotes: {comment.likes}".encode())
l += (b"\tAccountID:" + comment.authorAccountID)
l += (b"\tMessageID:" + comment.messageID)
yield l
def robtop_string_to_json(file:str):
output = file + "_json.txt"
with open(output,"wb") as wb:
with open(file,"rb") as rb:
for f in rb:
if f:
for comments in to_json(f.rstrip()):
wb.write(comments + b"\n")
def robtop_string_to_text(file:str):
output = file + "_text.txt"
with open(output,"wb") as wb:
with open(file,"rb") as rb:
for f in rb:
if f:
for comments in to_text(f.rstrip()):
wb.write(comments + b"\n")
def robtop_string_to_html(file:str, title:str):
html_file = file.removesuffix(".txt") + ".html"
with open(html_file,"wb") as wb:
wb.write(HTML_CSS.replace("{title}", title.removesuffix(".txt")).encode())
with open(file,"rb") as rb:
for f in rb:
if f:
for comments in to_html(f.rstrip()):
wb.write(comments)
# Close Html File...
wb.write(HTML_BOTTOM)