This repository has been archived by the owner on Jun 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
62 lines (45 loc) · 1.58 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
from operator import itemgetter
from ua_parser import user_agent_parser
def seg_access_log(line):
delimiters = {'[': ']', '"': '"'}
idx, start, count, delimiter, results = 0, 0, len(line), ' ', []
while True:
idx = line.find(delimiter, start)
delimiter = ' ' # reset
if idx < 0:
break
if start < idx:
results.append(line[start:idx])
start = idx + 1
# if idx != count - 1 and line[idx + 1] in delimiters:
if line[idx + 1] in delimiters:
delimiter = delimiters[line[idx + 1]]
start += 1
if start < count:
results.append(line[start:].rstrip())
return results
def solve():
browsers = {}
with open("data/data-01.txt") as f:
for line in f.readlines():
ua_string = seg_access_log(line)[-4]
parsed_string = user_agent_parser.ParseUserAgent(ua_string)
browser = parsed_string["family"]
if browser == "Facebook":
print(browser)
if browser not in browsers:
browsers[browser] = 1
else:
browsers[browser] += 1
total_browsers = sum(browser for browser in browsers.values())
for browser in browsers:
browsers[browser] = "{0:.2f}%".format(100 * browsers[browser] / total_browsers)
del total_browsers
return browsers
def main():
data = solve()
browsers = sorted(data.items(), key=itemgetter(1), reverse=True)
for browser, ratio in browsers:
print(ratio, browser)
if __name__ == '__main__':
main()