This repository has been archived by the owner on Oct 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
283 lines (252 loc) · 10.8 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# coding=utf-8
import re
import random
import getUID
import subprocess
import os
import cgi
import gzip
import json
import time
import ConfigParser
import datetime
import sys
from imp import reload
reload(sys)
sys.setdefaultencoding('utf-8')
#from jinja2 import Environment, FileSystemLoader
from os import listdir
from os.path import isfile, join
class Count:
def __init__(self):
self.number = 1
self.indexes = []
self.last_use = ""
class cd:
"""Context manager for changing the current working directory"""
def __init__(self, newPath):
self.newPath = os.path.expanduser(newPath)
def __enter__(self):
self.savedPath = os.getcwd()
os.chdir(self.newPath)
def __exit__(self, etype, value, traceback):
os.chdir(self.savedPath)
"""
TODO: handle more config inputs (ex. current_year, current_month)
"""
class Main:
def __init__(self):
self.config = ConfigParser.ConfigParser()
self.config.read(sys.argv[1])
#self.env = Environment(loader=FileSystemLoader(self.config.get("Files", "template_dir")))
#self.template = self.env.get_template('template.html')
self.file = None
self.most_active = {}
self.logs_path = self.config.get("Files", "logs_path")
print(sys.argv[1:])
self.channel = sys.argv[2]
self.generate_to = self.config.get("Files", "generate_to")
# per user statistics
self.user_question = {}
self.user_exclamation = {}
self.user_actions = {}
self.user_givemodes = {}
# total statistics
self.total_question = Count()
self.total_exclamation = Count()
self.total_actions = Count()
self.total_givemodes = Count()
self.activity_graph = [0]*24
# urls
self.urls = {}
#self.path = path
self.name = self.channel
self.file = []
self.yearly = False
self.year = self.config.get("Date", "year")
self.month = self.config.get("Date", "month")
path = "%s%s" % (self.logs_path, self.channel)
self.onlyfiles = [f for f in listdir(path) if isfile(join(path, f))]
self.filter_it(year=self.config.get("Date", "year"), month=self.config.get("Date", "month"))
def filter_it(self, year="\d+", month="\d+"):
"""
year - year to be generated
month - month to be generated
"""
if year == "current":
year = datetime.datetime.now().strftime("%Y")
if month == "current":
month = datetime.datetime.now().strftime("%m")
test = re.compile("(%s)\-(%s)\-\d+\.\w+" % (year, month), re.IGNORECASE)
self.onlyfiles = filter(test.search, self.onlyfiles)
#print(self.onlyfiles)
def bulk_lines(self):
def test_if_gz(filename):
if (filename.split(".")[::-1][0] == "gz"):
# print("gz")
return gzip.open("%s%s/%s" % (self.logs_path, self.channel, filename))
else:
# print("log")
return open("%s%s/%s" % (self.logs_path, self.channel, filename))
for filename in self.onlyfiles:
lines = (line.rstrip('\n') for line in test_if_gz(filename))
for index, line in enumerate(lines):
self.one_line(line, index, filename)
def one_line(self, line, n, filename):
index = n
line = line
filename = filename
if len(line) < 7:
return None
if line[6] == "-":
m = re.match("\d{2}:\d{2}\s-!-\smode/#\w+\s\[(.{2})\s(.+)\].*", line)
self.total_givemodes.number += 1
try:
if m.group(2) in self.user_givemodes:
self.user_givemodes[m.group(2)].number += 1
self.user_givemodes[m.group(2)].indexes += [n]
else:
self.user_givemodes.update({m.group(2): Count()})
self.user_givemodes[m.group(2)].indexes += [n]
except AttributeError:
return None
if line[6] == "<":
m = re.match("\d{2}:\d{2}\s<(\W|\s)(.*)>\s(.*)", line)
url_r = re.findall("(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)", line)
time = re.match("(\d{2}:\d{2})", line);
if not(m.group(2) in ["TheKubaX", "-_Mia"]):
try:
time = time.group(1).split(":")[0]
self.activity_graph[int(time)] += 1
except AttributeError:
pass
try:
for i in url_r:
if i in self.urls:
self.urls[i].number += 1
self.urls[i].last_use = m.group(2)
else:
self.urls.update({i: Count()})
self.urls[i].last_use = m.group(2)
except AttributeError:
pass
try:
try:
if "!" == m.group(3)[0]:
return None
except IndexError:
pass
if "!" in m.group(3):
self.total_exclamation.number += 1
if m.group(2) in self.user_exclamation:
self.user_exclamation[m.group(2)].number += 1
self.user_exclamation[m.group(2)].indexes += [n]
else:
self.user_exclamation.update({m.group(2): Count()})
self.user_exclamation[m.group(2)].indexes += [n]
if "?" in m.group(3):
self.total_question.number += 1
if m.group(2) in self.user_question:
self.user_question[m.group(2)].number += 1
self.user_question[m.group(2)].indexes += [n]
else:
self.user_question.update({m.group(2): Count()})
self.user_question[m.group(2)].indexes += [n]
if m.group(2) in self.most_active:
self.most_active[m.group(2)].number += 1
self.most_active[m.group(2)].indexes += [n]
else:
self.most_active.update({m.group(2): Count()})
self.most_active[m.group(2)].indexes += [n]
except AttributeError:
return None
else:
pass
if line[6] == " ":
m = re.match("\d{2}:\d{2}\s{2}\*\s([^\s]+)\s(.*)", line)
self.total_actions.number += 1
try:
if m.group(1) in self.user_actions:
self.user_actions[m.group(1)].number += 1
self.user_actions[m.group(1)].indexes += [n]
else:
self.user_actions.update({m.group(1): Count()})
self.user_actions[m.group(1)].indexes += [n]
except AttributeError:
return None
def get_most_active(self):
keys = self.most_active.keys()
my_x = {}
for i in keys:
my_x.update({i: self.most_active[i].number})
return sorted(my_x, key=my_x.get)[::-1][:25]
def get_runner_ups_active(self):
keys = self.most_active.keys()
my_x = {}
for i in keys:
my_x.update({i: self.most_active[i].number})
try:
return sorted(my_x, key=my_x.get)[::-1][25:35]
except IndexError:
return sorted(my_x, key=my_x.get)[::-1][25:]
def get_random_line(self, nick):
mine = u""
with cd("%s%s" % (self.logs_path, self.channel)):
base = "<\W%s>" % re.escape(nick)
user_input = "zgrep \"%s\" * | shuf -n 1" % base
mine = subprocess.Popen("%s" % user_input, shell=True, stdout=subprocess.PIPE).stdout.read()[20:].decode("utf-8")
return mine[3:]
def save_page(self):
my_keys = self.get_most_active()
most_active = []
for i in my_keys:
item = self.most_active[i]
uid = getUID.get_user_id(i)
most_active += [(i, item.number, cgi.escape("> ".join(self.get_random_line(i).split("> ")[1:])), uid)]
runner_ups = self.get_runner_ups_active()
def get_contents(what):
keys = what.keys()
my_x = {}
for i in keys:
my_x.update({i: what[i].number})
# print(sorted(my_x, key=my_x.get)[::-1][:2])
get_who = lambda n: sorted(my_x, key=my_x.get)[::-1][:2][n]
return [(get_who(0), what[get_who(0)].number), (get_who(1), what[get_who(1)].number)]
# graph percentage calculation
sum = 0
for i in self.activity_graph:
sum += i
sum = sum/240.0
for i in xrange(0, 24):
self.activity_graph[i] = int(self.activity_graph[i]) # int(self.activity_graph[i]/sum)
self.screaming = get_contents(self.user_exclamation)
self.asking = get_contents(self.user_question)
self.telling = get_contents(self.user_actions)
self.modding = get_contents(self.user_givemodes)
being = {"screaming": self.screaming, "asking": self.asking, "telling": self.telling, "modding": self.modding}
keys = self.urls.keys()
my_x = {}
for i in keys:
my_x.update({i: self.urls[i].number})
my_keys = sorted(my_x, key=my_x.get)[::-1][:10]
most_urls = []
for i in my_keys:
item = self.urls[i]
most_urls += [(i, item.number, item.last_use)]
total_num = [self.total_question.number, self.total_exclamation.number, self.total_actions.number, self.total_givemodes.number]
#output_from_parsed_template = self.template.render(name=self.name,
# most_active=most_active, runner_ups=runner_ups, being=being,
# urls_used=most_urls, total=total_num, activity_graph=self.activity_graph) # All necessary variables goes here
#with open("%s%s" % (self.config.get("Files", "generate_to"), (self.config.get("Files", "save_as") % self.name)), "wb") as fh:
# fh.write(output_from_parsed_template.encode("utf-8"))
with open("%sjson/%s.json" % (self.config.get("Files", "generate_to"), self.name), "w") as fh:
fh.write("")
json.dump(
{"name": self.name, "total_numbers": total_num, "most_active": most_active,
"runner_ups": runner_ups, "being": being, "most_used_urls": most_urls,
"activity_graph": self.activity_graph}, fh)
start_time = time.time()
a = Main()
a.bulk_lines()
a.save_page()
print("%s --- %s seconds ---" % (a.name, (time.time() - start_time)))