-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
94 lines (83 loc) · 2.81 KB
/
main.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
#!/usr/bin/python
# TODO
# Add date to table and chat messages for better formatting options and to properly wrap messages
# image improvmenet view
import codecs, re, sys
import base64
# for the color nicks
import hashlib
# script file_to_parse outputfile
#full paths!
filename = sys.argv[2]
sourcefile = sys.argv[1]
print(filename,sourcefile)
cssline = " \
<style> \
.responsive { \
width: 100%; \
max-width: 1000px; \
height: auto; \
} \
img { \
margin: 20px \
} \
body{ \
background-color: black; \
color: #ccc; \
} \
a { \
color: cornflowerblue\
}\
</style>"
file = open(filename, "w", encoding='utf-8')
buffer= ""
nicks = []
auxiliaryList = []
nicksAndColors = []
with codecs.open(sourcefile, encoding='utf-8') as f:
for line in f:
#encode greater/lowerthan signs
line = re.sub(r'<', '<', line)
line = re.sub(r'>', '>', line)
#Find the nick and color it
m = re.search('<(.+?)>', line)
if m:
found = m.group(1)
nicks.append(found)
for word in nicks:
if word not in auxiliaryList:
result = hashlib.sha256(word.encode())
hexcolor = result.hexdigest()[:6]
hexhtml = "#" + hexcolor
auxiliaryList.append(word)
nicksAndColors.append([hexhtml, word])
for pairs in nicksAndColors: # color nicks
line = re.sub("<" + pairs[1] + ">",
'<font color="' + pairs[0] + '"><b><' + pairs[1] + "></b></font>", line)
else: #color if line is not a message?
linkRegex = r'(?<=\] )<[\S]+'
linkFound = re.search(linkRegex, line)
if not linkFound:
line = re.sub(r"((?<=\] )[^&].*)", '<span style="color:#666666">' + r"\1"+ "</span>",line)
# check if it's an image
regex = r'https?://(?:[a-z0-9\-]+\.)+[a-z]{2,6}(?:/[^/#?]+)+\.(?:jpg|gif|png)'
mss = re.search(regex, line)
if mss:
founds = mss.group(0)
line = re.sub(regex, '</br><img class="responsive" src="' + founds + '">', line)
else: # if it's not an image, check for a normal link
linkRegex = r'http(s)?://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?'
linkFound = re.search(linkRegex, line)
if linkFound:
fooundl = linkFound.group(0)
line = re.sub(linkRegex, '<a href="' + fooundl + '">' + fooundl + '</a>', line)
# Add color to whole line
line = '<div style="color:#d9d9d9">' + line + '</div>' + "\n"
buffer= buffer + line
file.write(buffer)
f.close()
# prepend styling
file.seek(0, 0) # Move the cursor to top line
file.write('\n')
file.write(cssline)
file.close()