52 lines
1.4 KiB
Python
52 lines
1.4 KiB
Python
|
#!/usr/bin/env python3
|
||
|
|
||
|
import json
|
||
|
import urllib.request
|
||
|
import argparse
|
||
|
import collections
|
||
|
from operator import itemgetter
|
||
|
|
||
|
parser = argparse.ArgumentParser(description="API Call to collect data")
|
||
|
parser.add_argument("tenant", type=str, help="Tenant Name")
|
||
|
parser.add_argument("token", type=str, help="Tenat API Token")
|
||
|
parser.add_argument("-t", "--timeperiod", type=int, default='604800', help="Timeperiod (default: 604800)")
|
||
|
|
||
|
try:
|
||
|
args = parser.parse_args()
|
||
|
tenant = args.tenant
|
||
|
token = args.token
|
||
|
timeperiod = args.timeperiod
|
||
|
|
||
|
except argparse.ArgumentError as e:
|
||
|
print(str(e))
|
||
|
|
||
|
def print_dict(dict):
|
||
|
for key, value in sorted(dict.items(), key = itemgetter(1), reverse = True):
|
||
|
print ("{:<35s}{:5d}".format(key, value))
|
||
|
|
||
|
base_url = "https://{}.goskope.com/api/v1/events?token={}&type=page&timeperiod={}".format(tenant, token, timeperiod)
|
||
|
|
||
|
req = urllib.request.Request(base_url)
|
||
|
with urllib.request.urlopen(req) as response:
|
||
|
content = response.read()
|
||
|
json_content = json.loads(content)
|
||
|
|
||
|
domains = collections.Counter()
|
||
|
categories = collections.Counter()
|
||
|
|
||
|
for i in range (0, len (json_content['data'])):
|
||
|
domain = json_content["data"][i]["domain"]
|
||
|
ccl = json_content["data"][i]["ccl"]
|
||
|
category = json_content["data"][i]["category"]
|
||
|
domains[domain] += 1
|
||
|
categories[category][count] += 1
|
||
|
|
||
|
|
||
|
#print ("===== Domains =====")
|
||
|
#print_dict(domains)
|
||
|
|
||
|
|
||
|
print ("\n===== Categories =====")
|
||
|
print_dict(categories)
|
||
|
|