Skip to content

Instantly share code, notes, and snippets.

@Evantm
Last active January 30, 2019 02:16
Show Gist options
  • Save Evantm/247ca0aeeb198c957c732e49f3fb0a7c to your computer and use it in GitHub Desktop.
Save Evantm/247ca0aeeb198c957c732e49f3fb0a7c to your computer and use it in GitHub Desktop.
# meant to be run as a cronjob
# used to aggreagate data output from hoowmanypeople around and send to server
from datetime import datetime
import requests
def main(start,end):
now = datetime.now()
list_of_list_of_macs = []
# -- Scanning file --
with open(FILENAME, 'r') as f:
lines = f.readlines()
list_of_dicts = []
timeList = []
avg = 0.0
try:
for i in range(start, end, -1):
list_of_dicts.append(lines[i])
except:
() # Trying to read 10 lines of a 5 line file will cause this
for d in range(len(list_of_dicts)):
d = eval(list_of_dicts[d])
tempList = []
avg += len(d['cellphones'])
timeList.append(d['time'])
for num in d['cellphones']:
tempList.append(num['mac'])
list_of_list_of_macs.append(tempList)
# -- end of file scanning --
# -- iterating through list of list of macs to find unique values --
total = set() # Set of unqiue macs found in lists
for i in range(len(list_of_list_of_macs)):
for j in list_of_list_of_macs[i]:
if (i > 0):
if (j in list_of_list_of_macs[i - 1]):
total.add(j)
# -- fin --
report = {}
if (len(list_of_dicts) == 0):
report["value1"] = ('No elements')
requests.post("https://maker.ifttt.com/trigger/Howmanypeople/with/key/*************", data=report)
else:
avg = float('%.2f' % (avg / len(list_of_dicts))) # math to find average to two decimal places
print('Amount of unique Macs', len(total))
print('Average', avg)
report["value1"] = datetime.fromtimestamp(timeList[-1]), '-', datetime.fromtimestamp(timeList[0])
report["value2"] = ('Amount of unique Macs', len(total))
report["value3"] = ('Average', avg)
requests.post("https://maker.ifttt.com/trigger/Howmanypeople/with/key/*************", data=report)
def reset_file(filename):
seconds_since_midnight = (now - now.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds()
if (seconds_since_midnight < 120):
open(filename, 'w').close() #clear contents for privacy reasons at midnight. Also it helps keeps the file size manageable.
if __name__ == '__main__':
FILENAME = "log.json" # File to read
start = -1 # what line to start at
end = -7 # what line to end at
# difference between start and end is how many json lines to read. Can smooth out average.
main()
reset_file(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment