/
work_logger.py
executable file
·66 lines (56 loc) · 2.38 KB
/
work_logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#! /usr/bin/env python
# Logs osticket URLs from the clipboard to a file
import os
import re
import logging
import datetime
import pyperclip
import subprocess as s
def write_to_file():
# Create a file in this example format: <Month><Date>.txt
current_month = datetime.datetime.today().strftime("%B_%Y")
today_file = datetime.datetime.today().strftime("%B%d.txt")
# Save the log file in the work folder
work_logs_path = '/home/terra/Documents/foxy/docs'
current_month_logs_dir = os.path.join(work_logs_path, current_month)
# Search only for osticket URLs in the clipboard
osticket_match_object = \
re.compile(r"""https://support.(leahscape|getfoxyproxy).(org|com)/
(\w+)/(\w+).+""", re.VERBOSE)
clipboard_contents = pyperclip.paste()
try:
if osticket_match_object.search(clipboard_contents).group():
osticket_url = clipboard_contents
if not os.path.exists(current_month_logs_dir):
os.makedirs(current_month_logs_dir)
with open(os.path.join(current_month_logs_dir, today_file), 'a+') \
as log_file:
# Don't save duplicate data to logfile
tickets = log_file.readlines()
found = False
for ticket in tickets:
if osticket_url in ticket:
found = True
logging.debug('Duplicate URL found, skipping')
if not found:
log_file.write(osticket_url + '\n')
logging.debug('%s written to logfile'
% osticket_url)
s.call(['notify-send', '-i', 'emblem-default',
'Ticket URL written to logfile'])
except AttributeError:
logging.debug("Clipboard does not contain osticket url, skipping")
pass
def main():
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logging.debug("Work Logger initialised")
s.call(['notify-send', '-i', 'emblem-generic', 'Work Logger initialised'])
active = True
while active:
logging.debug("Loop starting")
write_to_file()
if __name__ == '__main__':
main()