-
Notifications
You must be signed in to change notification settings - Fork 0
/
count_browsers.py
57 lines (49 loc) · 1.57 KB
/
count_browsers.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
import time
import sys
import sqlite3
from datetime import datetime
DB_NAME = "db.sqlite"
def get_lines(time_obj):
conn = sqlite3.connect(DB_NAME)
cur = conn.cursor()
cur.execute("SELECT time_local,http_user_agent FROM logs WHERE created > ?", [time_obj])
resp = cur.fetchall()
return resp
def get_time_and_ip(lines):
browsers = []
times = []
for line in lines:
times.append(parse_time(line[0]))
browsers.append(parse_user_agent(line[1]))
return browsers, times
def parse_time(time_str):
try:
time_obj = datetime.strptime(time_str, '[%d/%b/%Y:%H:%M:%S %z]')
except Exception:
time_obj = ""
return time_obj
def parse_user_agent(user_agent):
browsers = ["Firefox", "Chrome", "Opera", "Safari", "MSIE"]
for browser in browsers:
if browser in user_agent:
return browser
return "Other"
if __name__ == "__main__":
browser_counts = {}
start_time = datetime(year=2017, month=3, day=9)
while True:
lines = get_lines(start_time)
browsers, times = get_time_and_ip(lines)
if len(times) > 0:
start_time = times[-1]
for browser, time_obj in zip(browsers, times):
if browser not in browser_counts:
browser_counts[browser] = 0
browser_counts[browser] += 1
count_list = browser_counts.items()
count_list = sorted(count_list, key=lambda x: x[0])
print("")
print(datetime.now())
for item in count_list:
print("{}: {}".format(*item))
time.sleep(5)