use datetime objects for parsing

master
Maximilian Zettler 4 years ago
parent 7a82a0e893
commit 8e1cd4ea0a
  1. 5
      constants.py
  2. 67
      timebot.py

@ -4,3 +4,8 @@ BREAK_START_ENTRY_CODE_ID = 48
BREAK_END_ENTRY_CODE_ID = 64
PUNCH_COMMANDS = ("punch_in", "punch_out", "break_start", "break_end")
SIMPLE_DATETIME_FORMAT = "%d.%m.%Y_%H:%M"
SIMPLE_DATETIME_FORMAT_HUMAN = "DD.MM.YYYY_hh:mm"
SIMPLE_DATE_FORMAT = "%d.%m.%Y"
SIMPLE_TIME_FORMAT = "%H:%M"

@ -8,7 +8,8 @@ import sys
import requests as requests
from constants import COMING_ENTRY_CODE_ID, LEAVING_ENTRY_CODE_ID, PUNCH_COMMANDS, BREAK_START_ENTRY_CODE_ID, \
BREAK_END_ENTRY_CODE_ID
BREAK_END_ENTRY_CODE_ID, SIMPLE_DATE_FORMAT, SIMPLE_TIME_FORMAT, SIMPLE_DATETIME_FORMAT, \
SIMPLE_DATETIME_FORMAT_HUMAN
logger = logging.getLogger()
logging.basicConfig(level=logging.INFO)
@ -59,16 +60,17 @@ class TimeBot:
request = session.post(self.baseurl + "Account/LogOn", data=login_data)
request.raise_for_status()
def add_entry(self, punch_date: str, punch_time: str, entry_code: int, note: str = None) -> requests.Response:
def add_entry(self, punch_datetime: datetime.datetime, entry_code: int, note: str = None) -> requests.Response:
"""
Add mobatime entry.
:param str punch_date: date string in format: ``DD.MM.YYYY``
:param str punch_time: time string in format: ``hh:mm``
:param datetime.datetime punch_datetime: datetim object
:param int entry_code: entry type code
:param str note: free text note added to the Mobatime entry
:return: requests response object
"""
punch_date = punch_datetime.strftime(SIMPLE_DATE_FORMAT)
punch_time = punch_datetime.strftime(SIMPLE_TIME_FORMAT)
entry_data = {
"periode0Date": punch_date,
"periode0Time": punch_time,
@ -154,37 +156,33 @@ class TimeBot:
request.raise_for_status()
return request.json()["data"]
def punch_in(self, punch_in_date: str, punch_in_time: str):
def punch_in(self, punch_datetime: datetime.datetime):
"""
:param str punch_in_date: date string in format: ``DD.MM.YYYY``
:param str punch_in_time: time string in format: ``hh:mm``
:param datetime.datetime punch_datetime: datetime object
:raises: on status code != 2xx
"""
self.add_entry(punch_in_date, punch_in_time, COMING_ENTRY_CODE_ID, note="da").raise_for_status()
self.add_entry(punch_datetime, COMING_ENTRY_CODE_ID, note="da").raise_for_status()
def punch_out(self, punch_out_date: str, punch_out_time: str):
def punch_out(self, punch_datetime: datetime.datetime):
"""
:param str punch_out_date: date string in format: ``DD.MM.YYYY``
:param str punch_out_time: time string in format: ``hh:mm``
:param datetime.datetime punch_datetime: datetime object
:raises: on status code != 2xx
"""
self.add_entry(punch_out_date, punch_out_time, LEAVING_ENTRY_CODE_ID, note="weg").raise_for_status()
self.add_entry(punch_datetime, LEAVING_ENTRY_CODE_ID, note="weg").raise_for_status()
def break_start(self, punch_date: str, punch_time: str):
def break_start(self, punch_datetime: datetime.datetime):
"""
:param str punch_date: date string in format: ``DD.MM.YYYY``
:param str punch_time: time string in format: ``hh:mm``
:param datetime.datetime punch_datetime: datetime object
:raises: on status code != 2xx
"""
self.add_entry(punch_date, punch_time, BREAK_START_ENTRY_CODE_ID, note="pause").raise_for_status()
self.add_entry(punch_datetime, BREAK_START_ENTRY_CODE_ID, note="pause").raise_for_status()
def break_end(self, punch_date: str, punch_time: str):
def break_end(self, punch_datetime: datetime.datetime):
"""
:param str punch_date: date string in format: ``DD.MM.YYYY``
:param str punch_time: time string in format: ``hh:mm``
:param datetime.datetime punch_datetime: datetime object
:raises: on status code != 2xx
"""
self.add_entry(punch_date, punch_time, BREAK_END_ENTRY_CODE_ID, note="pause ende").raise_for_status()
self.add_entry(punch_datetime, BREAK_END_ENTRY_CODE_ID, note="pause ende").raise_for_status()
if __name__ == '__main__':
@ -202,14 +200,16 @@ if __name__ == '__main__':
help="type of time entry; this can be `punch_in`, `punch_out`, `break_start`, `break_end`",
default="punch_in",
choices=PUNCH_COMMANDS)
parser_punch.add_argument("-s", help="timestamp in format `DD.MM.YYYY_hh:mm` or `now`", default="now")
parser_punch.add_argument("-s", help=f"timestamp in format `{SIMPLE_DATETIME_FORMAT_HUMAN}` or `now`", default="now")
# subparser command: `list-entries`
parser_list_entries = subparsers.add_parser("list-entries", help="use this command to list your time entries")
parser_list_entries.add_argument("--start-date",
help="start date filter in format `DD.MM.YYYY_hh:mm` (default: now - 10days; unset for default)")
help=f"start date filter in format `{SIMPLE_DATETIME_FORMAT_HUMAN}` "
f"(default: now - 10days; unset for default)")
parser_list_entries.add_argument("--end-date",
help="end date filter in format `DD.MM.YYYY_hh:mm` (default: now; unset for default)")
help=f"end date filter in format `{SIMPLE_DATETIME_FORMAT_HUMAN}` "
f"(default: now; unset for default)")
parser_list_entries.add_argument("--items", help="max items to request per page", default=20, type=int)
args = parser.parse_args()
@ -227,21 +227,24 @@ if __name__ == '__main__':
tb = TimeBot(baseurl=config["general"]["baseurl"], user=args.u, password=password)
if args.subparser_name == "punch":
if args.s == "now":
now = datetime.datetime.now()
punch_date = now.strftime("%d.%m.%Y")
punch_time = now.strftime("%H:%M")
punch_datetime = datetime.datetime.now()
else:
punch_date = args.s.split("_")[0]
punch_time = args.s.split("_")[1]
logger.info("running `{}` with date `{}` and time `{}`".format(args.t, punch_date, punch_time))
getattr(tb, args.t)(punch_date, punch_time)
punch_datetime = datetime.datetime.strptime(args.s, SIMPLE_DATETIME_FORMAT)
logger.info("running `{}` with date `{}` and time `{}`".format(
args.t,
punch_datetime.strftime(SIMPLE_DATE_FORMAT),
punch_datetime.strftime(SIMPLE_TIME_FORMAT),
))
getattr(tb, args.t)(punch_datetime)
elif args.subparser_name == "smart-punch":
pass
elif args.subparser_name == "list-entries":
end_date = None
if args.end_date:
end_date = datetime.datetime.strptime(args.end_date, "%d.%m.%Y_%H:%M")
end_date = datetime.datetime.strptime(args.end_date, SIMPLE_DATETIME_FORMAT)
start_date = None
if args.start_date:
start_date = datetime.datetime.strptime(args.start_date, "%d.%m.%Y_%H:%M")
start_date = datetime.datetime.strptime(args.start_date, SIMPLE_DATETIME_FORMAT)
data = tb.get_entries(entries=args.items, start_date=start_date, end_date=end_date)
for i in data:
print("Entry: {} - DateTime: {} - Note: {}".format(i["entryName"], i["dateTime"], i["note"]))

Loading…
Cancel
Save