Skip to content

Instantly share code, notes, and snippets.

@kagesenshi
Last active July 9, 2024 07:25
Show Gist options
  • Save kagesenshi/0baf4cb1098f2e27bc313a0aea182289 to your computer and use it in GitHub Desktop.
Save kagesenshi/0baf4cb1098f2e27bc313a0aea182289 to your computer and use it in GitHub Desktop.
Checkin/Checkout OfficeCentral using CLI
[Unit]
Description=Checkin attendance in OfficeCentral
Wants=attendance-checkin.timer
[Service]
Type=oneshot
ExecStart=/usr/bin/python3 /path/to/attendance.py -a /path/to/login_file -i
[Install]
WantedBy=multi-user.target
[Unit]
Description=Checkin attendance in OfficeCentral
Requires=attendance-checkin.service
[Timer]
Unit=attendance-checkin.service
OnCalendar=Mon..Fri *-*-* 08:50:00
[Install]
WantedBy=timers.target
[Unit]
Description=Checkout attendance in OfficeCentral
Wants=attendance-checkout.timer
[Service]
Type=oneshot
ExecStart=/usr/bin/python3 /path/to/attendance.py -a /path/to/login_file -o
[Install]
WantedBy=multi-user.target
[Unit]
Description=Checkout attendance in OfficeCentral
Requires=attendance-checkout.service
[Timer]
Unit=attendance-checkout.service
OnCalendar=Mon..Fri *-*-* 18:30:00
[Install]
WantedBy=timers.target
#!/usr/bin/python3
#
# Checkin / checkout office central using command line
#
# Usage:
#
#
# checkin:
# python3 attendance.py -a login_file -i
#
# checkout:
# python3 attendance.py -a login_file -o
#
#
# `login_file` is a simple text file with 2 lines, first line is username, second line is password:
#
# username
# password
#
#
import requests
from lxml.html import fromstring as load_html
import argparse
import base64
import sys
import os
from datetime import datetime
LOGIN_URL = 'https://v2.officecentral.asia/Account/login'
CHECKIN_URL = 'https://v2.officecentral.asia/StaffPortal/Attendance/CheckIn'
CHECKOUT_URL = 'https://v2.officecentral.asia/StaffPortal/Attendance/CheckOut'
LOCATIONS = {'office': {
'latitude': 2.925964,
'longitude': 101.649996,
'currenttimezone': 'Asia/Kuala_Lumpur',
'autoaddress': 'C2-2-7, Block C2, CBD Perdana 3, Lingkaran Cyber Point Timur, Cyber 12, 63000 Cyberjaya, Selangor',
'remarks': '',
}}
def login(session, username, password):
print("Logging in")
r = session.get(LOGIN_URL)
e = load_html(r.text)
login_form = e.cssselect('.login-form')[0]
verification_token = login_form.cssselect('[name="__RequestVerificationToken"]')[0].value
form_data = {
'username': username,
'password': password,
'__RequestVerificationToken': verification_token,
'remember': '1',
'returnurl': '/'
}
r = session.post(LOGIN_URL, data=form_data)
if r.status_code != 200:
raise Exception("Invalid Login")
print('Login successful')
def checkin(session, location):
print("Checking in")
data = {'submit': 'Check In'}
data.update(LOCATIONS[location])
r = session.post(CHECKIN_URL, data=data)
if r.status_code != 200:
raise Exception("Unable to checkin\n\n" + r.text)
print(f'Checkin successful at "{location}" on {datetime.now().isoformat()}')
def checkout(session, location):
print("Checking out")
data = {'submit': ' Check Out '}
data.update(LOCATIONS[location])
r = session.post(CHECKOUT_URL, data=data)
if r.status_code != 200:
raise Exception("Unable to checkout\n\n" + r.text)
print(f'Checkout successful at "{location}" on {datetime.now().isoformat()}')
parser = argparse.ArgumentParser()
parser.add_argument('-a', '--login-file', required=True)
parser.add_argument('-i', '--checkin', action='store_true', default=False)
parser.add_argument('-o', '--checkout', action='store_true', default=False)
parser.add_argument('-l', '--location', default='office')
args = parser.parse_args()
filename = os.path.basename(sys.argv[0])
if args.checkin and args.checkout:
print(f'{filename}: error: Only one of "--checkin" or "--checkout" can be specified', file=sys.stderr)
sys.exit(1)
elif not (args.checkin or args.checkout):
print(f'{filename}: error: Either "--checkin" or "--checkout" is required', file=sys.stderr)
sys.exit(1)
if args.location not in LOCATIONS.keys():
print(f'{filename}: error: Unknown location "{args.location}"', file=sys.stderr)
sys.exit(1)
with open(args.login_file) as f:
username, password = f.read().strip().split('\n')
session = requests.Session()
login(session, username, password)
if args.checkin:
checkin(session, args.location)
if args.checkout:
checkout(session, args.location)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment