Last active
June 30, 2021 10:12
-
-
Save lainq/07802ce6e9d555e9659162b1378bc4d1 to your computer and use it in GitHub Desktop.
time tracker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import os | |
import psutil | |
import win32gui | |
import win32process | |
import json | |
import sys | |
import prettytable | |
import winsound | |
class TimeTrackerStats(object): | |
def __init__(self, tracker): | |
self.tracker = tracker | |
self.content = self.tracker.read_content() | |
self.convert_to_minutes() | |
self.log_details() | |
def convert_to_minutes(self): | |
for application_name in self.content: | |
seconds = self.content[application_name] | |
seconds = seconds % (24 * 3600) | |
hour = seconds // 3600 | |
seconds %= 3600 | |
minutes = seconds // 60 | |
seconds %= 60 | |
self.content[application_name] = (hour, minutes, seconds) | |
def log_details(self): | |
table = prettytable.PrettyTable() | |
table.field_names = ["Application", "Time Used"] | |
for key in self.content: | |
hours, minutes, seconds = self.content[key] | |
time_string = ( | |
f"{hours}:{minutes}:{f'0{seconds}' if seconds < 10 else seconds}" | |
) | |
table.add_row([key, time_string]) | |
print(table) | |
class TimeTracker(object): | |
times_used = {} | |
total_ram_usage, count = 0, 0 | |
total_count = 0 | |
filename = os.path.join(os.path.expanduser("~"), ".time.tracker") | |
def __init__(self): | |
if not os.path.isfile(self.filename): | |
with open(self.filename, "w") as writer: | |
writer.write("") | |
self.times_used = self.read_content() | |
def start(self): | |
self.total_ram_usage, self.count = self.get_current_ram_usage(), self.count + 1 | |
while True: | |
application = self.get_active_executable_name() | |
if not application: | |
time.sleep(1) | |
continue | |
if application not in self.times_used: | |
self.times_used[application] = 0 | |
self.times_used[application] += 1 | |
if self.times_used[application] % 360 == 0: | |
winsound.MessageBeep() | |
print(f"You have used {application} for another one hour") | |
if self.total_count % 5 == 0: | |
print(":") | |
self.log(self.times_used) | |
time.sleep(1) | |
self.total_count += 1 | |
self.total_ram_usage, self.count = ( | |
self.get_current_ram_usage(), | |
self.count + 1, | |
) | |
print(self.times_used, self.total_ram_usage) | |
def log(self, content): | |
print("Logging data") | |
with open(self.filename, "w") as file_writer: | |
file_writer.write(json.dumps(content)) | |
def read_content(self): | |
with open(self.filename, "r") as file_reader: | |
try: | |
return json.loads(file_reader.read()) | |
except Exception as exception: | |
self.log({}) | |
return {} | |
def get_current_ram_usage(self): | |
return round(psutil.virtual_memory().percent, 1) | |
def get_active_executable_name(self): | |
try: | |
process_id = win32process.GetWindowThreadProcessId( | |
win32gui.GetForegroundWindow() | |
) | |
return ".".join(psutil.Process(process_id[-1]).name().split(".")[:-1]) | |
except Exception as exception: | |
return None | |
def argument_parser(arguments): | |
command, parameters = None, [] | |
for index, element in enumerate(arguments): | |
if index == 0: | |
command = element | |
continue | |
parameters.append(element) | |
return command, parameters | |
def perform_command(command, parameters): | |
tracker = TimeTracker() | |
if not command: | |
tracker.start() | |
return tracker | |
if command == "stats": | |
TimeTrackerStats(tracker) | |
elif command == "clear": | |
tracker.times_used = {} | |
tracker.log({}) | |
elif command == "save": | |
filename = input("filename: ") | |
with open(filename, "w") as writer: | |
writer.write(json.dumps(tracker.times_used)) | |
if __name__ == "__main__": | |
arguments = sys.argv[1:] | |
command, params = argument_parser(arguments) | |
perform_command(command, params) | |
# tracker = TimeTracker() | |
# tracker.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment