Skip to content

Instantly share code, notes, and snippets.

@gaborigloi
Last active October 24, 2019 16:41
Show Gist options
  • Save gaborigloi/29f66a778451f9aec9d0843c1866d408 to your computer and use it in GitHub Desktop.
Save gaborigloi/29f66a778451f9aec9d0843c1866d408 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
"""
Notify when 8 hours have passed since first login today
"""
import getpass
import subprocess
import time
from datetime import datetime, timezone, timedelta
from dateutil import parser
import tkinter as tk
TIME_AT_WORK = timedelta(hours=8)
def _main():
uname = getpass.getuser().encode('utf-8')
output = subprocess.check_output(['last', '--nohostname', '--since', 'today', '--time-format', 'iso', uname])
lines = output.decode('utf-8').split('\n')
logins = [
line for line in lines
if (not line.startswith('wtmp begins')) and line.strip() != '']
if logins:
login_times = []
for login in logins:
login_time = login.split()[2]
login_time = parser.parse(login_time)
login_times.append(login_time)
login_times.sort()
first_login_time = login_times[0]
else:
print('WARNING: could not find login times - input: "{}", assuming log time is given by "wtmp begins" message'.format(lines))
[wtmp_line] = [
line for line in lines
if line.startswith('wtmp begins')]
first_login_time = parser.parse(wtmp_line.split()[2])
print('First login:', first_login_time)
now = datetime.now(timezone.utc)
time_since_first_login = now - first_login_time
print('Time since first login:', time_since_first_login)
if time_since_first_login < TIME_AT_WORK:
time_to_leave = TIME_AT_WORK - time_since_first_login
print('Time until end:', time_to_leave)
leave_at = now + time_to_leave
print('End:', leave_at)
time.sleep(time_to_leave.total_seconds())
print('go home')
root = tk.Tk()
l = tk.Label(root, text='go home')
l.pack(side="top")
b1 = tk.Button(root, text='check when u need to b at work tmr')
b1.pack(side="top")
b = tk.Button(root, text='Shut down DANGER!!!!! :o')
b.pack(side="left")
c = tk.Button(root, text='Cancel', fg="yellow", command=root.destroy)
c.pack(side="left")
root.mainloop()
if __name__ == '__main__':
_main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment