#!/usr/bin/env python3
#======================================================================================================
# Read a file with each line in JSON format
# Convert to list of dictionaries
# Take the timezone 'tz' element 
#   (other elements are: 'a'-browser, 'c'-country, 'gr'-state, 'r'-requesting url, 'u'-requested url)   
# Count the timezone elements
# Sort the timezones element count (descending)
# print timezones in descending order (in most popular)
#======================================================================================================
import json
import operator

print("Content-Type: text/html \n")            #http header with newline char (required for web) 

file = '/home/sultans/web/python/demo/analytics/url_requests.txt'

fileObj = open(file,'rb')                      #open file for reading binary
lines   = fileObj.readlines()                  #read entire file into a list

#======================================
def create_list_dict(lines):                   #convert JSON lines to a list of dictionaries
    list_dict = []                             #create a list
    for line in lines:                         #loop through all the lines
        dict = json.loads(line)                #convert the JSON line into a dictionary
        list_dict.append(dict)                 #append the dictionary to the list
    return(list_dict)                          #return list of dictionaries
#======================================
def extract(list_dict, elem):                  #extract an element from a list of dictionaries
    array = []                                 #create an array
    for dict in list_dict:                     #loop through the list of dictionaries
        if elem in dict and dict[elem]:        #if the element exists, and has a value
            array.append(dict[elem])           #append the value to the list
    return(array)                              #return the list
#======================================
def count(list):                               #count occurrences of each element in a list
    counters = {}                              #create a dictionary
    for elem in list:                          #loop through all the element in the list 
        if elem not in counters:               #if element is not in the dictionary
            counters[elem] = 1                 #add element to the dictionary, and set count=1
        else:                                  #if element already exists in the dictionary
            counters[elem] += 1                #add 1 to count
    return(counters)                           #return the dictionary {element:count}
#======================================

list_dict = create_list_dict(lines)
elements  = extract(list_dict,'tz')
counts    = count(elements)

#sort_by_key  = sorted(counts.items(), key=operator.itemgetter(0) )
sort_by_value = sorted(counts.items(), key=operator.itemgetter(1), reverse=True)

for elem in sort_by_value:
    print(elem)



#=== link to see the python code =================================================
import os, sys
sys.path.insert(0,'/home/sultans/web/python/demo')
import zCode                          #import func to display the Python code
filename = os.path.abspath(__file__)  #get absolute file name 
zCode.display(filename)               #call it
#=================================================================================