Last active
July 10, 2018 08:12
-
-
Save VMuliadi/1f475ada3eded582e1182db3ff1704da to your computer and use it in GitHub Desktop.
SofwareSeni Hub Login from Command Line Application. Based on Python and NodeJS (PhantomJS)
This file contains 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
# /usr/bin/python | |
import os | |
import sys | |
import base64 | |
import getpass | |
import requests | |
from lxml import html | |
from selenium import webdriver | |
from prettytable import PrettyTable | |
username = '' | |
password = '' | |
action = sys.argv[1] | |
HOME_DIR = os.path.expanduser('~') | |
creds_file = HOME_DIR + '/.sshub' | |
def createCredsFile(): | |
# Create credential key | |
print('Creating creds file') | |
username = raw_input('Username : ') | |
password = getpass.getpass('Password : ') | |
with open(creds_file, 'w') as creds: | |
creds.write('username:' + username + "\n") | |
creds.write('password:' + base64.b64encode(password)) | |
print('Credential file created on ' + creds_file) | |
def login(username, password): | |
driver = webdriver.PhantomJS() | |
driver.get('http://hub.softwareseni.co.id/') | |
usernameInput = driver.find_element_by_css_selector('input#input-username') | |
passwordInput = driver.find_element_by_css_selector('input#input-password') | |
usernameInput.send_keys(username) | |
passwordInput.send_keys(password) | |
login_button = driver.find_element_by_css_selector('input#login-btn') | |
login_button.click() | |
return driver | |
def main(): | |
with open(creds_file, 'r') as creds: | |
for line in creds: | |
if line.startswith('username') and len(line.split(':')) > 0: username = line.split(':')[1].rsplit() | |
if line.startswith('password') and len(line.split(':')) > 0: password = base64.b64decode(line.split(':')[1]) | |
# Start Working | |
if action == 'start': | |
login_attempt = requests.post('http://hub.softwareseni.co.id/program/login', data = {'username': username, 'password': password}) | |
if not '<i class="icon-bell"></i> Start Working !' in login_attempt.text: | |
print 'Check your username and password!' | |
else: | |
try: | |
driver = login(username, password) | |
driver.get('http://hub.softwareseni.co.id/#!/timesheet') | |
workingTodayButton = driver.find_element_by_css_selector('button#start_working') | |
workingTodayButton.click() | |
print('Login Succeed') | |
driver.close() | |
except selenium.common.exceptions.NoSuchElementException: | |
print('You forget to fill yesterday timesheet. Please complete it before start working today.') | |
sys.exit() | |
# Finish Working, No Log | |
elif action == 'finish': | |
login_attempt = requests.post('http://hub.softwareseni.co.id/program/login', data = {'username': username, 'password': password}) | |
if not '<i class="icon-bell"></i> Start Working !' in login_attempt.text: | |
print 'Check your username and password!' | |
else: | |
try: | |
driver = login(username, password) | |
driver.get('http://hub.softwareseni.co.id/#!/timesheet') | |
workingTodayButton = driver.find_element_by_css_selector('button#stop_working') | |
workingTodayButton.click() | |
print('Thank You for Working Today') | |
driver.close() | |
except selenium.common.exceptions.NoSuchElementException: | |
print('You already click on Stop working Button') | |
sys.exit() | |
# Check Working Time | |
elif action == 'check': | |
try: | |
login_attempt = requests.post('http://hub.softwareseni.co.id/program/login', data = {'username': username, 'password': password}) | |
if not '<i class="icon-bell"></i> Start Working !' in login_attempt.text: | |
print 'Check your username and password!' | |
else: | |
working_time = '' | |
driver = login(username, password) | |
driver.get('http://hub.softwareseni.co.id/#!/timesheet') | |
working_time = driver.find_element_by_tag_name('table').text.rstrip().split('\n')[2] | |
print(working_time) | |
if int(working_time.split(' : ')[1].split(':')[0]) >= 9: | |
workingTodayButton = driver.find_element_by_css_selector('button#stop_working') | |
workingTodayButton.click() | |
print('Thank You for Working Today') | |
driver.close() | |
except Exception: | |
print('Are you working today?') | |
# Check Total Leave Balance | |
elif action == 'check_leave_quota': | |
login_attempt = requests.post('http://hub.softwareseni.co.id/program/login', data = {'username': username, 'password': password}) | |
if not '<i class="icon-bell"></i> Start Working !' in login_attempt.text: | |
print 'Check your username and password!' | |
else: | |
driver = login(username, password) | |
driver.get('http://hub.softwareseni.co.id/#!/my_leave_balance') | |
leave_quota = driver.find_element_by_css_selector('table.table').text | |
total_leave_quota = int(leave_quota.split('\n')[0].split('Annual Leave ')[1].rstrip()) | |
available_leave_quota = int(leave_quota.split('\n')[1].split('Annual Leave Balance')[1].rstrip()) | |
print('You may take a leave ' + str(available_leave_quota) + ' day(s)') | |
print('You already use ' + str(total_leave_quota - available_leave_quota) + ' day(s) leave') | |
driver.close() | |
# Check Leave Status | |
elif action == 'check_leave_status': | |
login_attempt = requests.post('http://hub.softwareseni.co.id/program/login', data = {'username': username, 'password': password}) | |
if not '<i class="icon-bell"></i> Start Working !' in login_attempt.text: | |
print 'Check your username and password!' | |
else: | |
leave_list = [] | |
driver = login(username, password) | |
driver.get('http://hub.softwareseni.co.id/#!/onleave_list') | |
leave_status = driver.find_element_by_css_selector('div.widget').text.split('\n') | |
for index, leave in enumerate(leave_status): | |
if leave.split(' ')[2].lower() == 'approved': | |
leave_dict = {} | |
leave_dict['index'] = index | |
leave_dict['proposed_date'] = leave.split(' ')[1] | |
if leave.split(' ')[3].lower() == 'annual' or | |
leave.split(' ')[3].lower() == 'unpaid' or | |
' '.join(leave.split(' ')[3:5]).lower() == 'sick leave' or | |
leave.split(' ')[3].lower() == 'marriage' or | |
leave.split(' ')[3].lower() == 'maternity': | |
leave_dict['status'] = ' '.join(leave.split(' ')[3:5]) | |
leave_dict['notes'] = ' '.join(leave.split(' ')[4:]) | |
elif leave.split(' ')[3].lower() == 'change' or | |
' '.join(leave.split(' ')[3:4].lower()) == 'sick change' or | |
leave.split(' ')[3].lower() == 'passed': | |
leave_dict['status'] = ' '.join(leave.split(' ')[3:6]) | |
leave_dict['notes'] = ' '.join(leave.split(' ')[6:]) | |
leave_list.append(leave_dict) | |
leave_table = PrettyTable(['#', 'Proposed Date', 'Status', 'Notes']) | |
for leave in leave_list: leave_table.add_row([leave['index'], leave['proposed_date'], leave['status'], leave['notes']]) | |
print(str(leave_table)) | |
else: | |
sys.exit() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
About :
This is a simple script that used to trigger "Start Working" button on SoftwareSeni's Hub.
Make sure you've been installed pip, python2, nodejs, nodejs-legacy, and npm
NodeJS libs dependencies :
npm install -g phantomjs-prebuilt
Python2 libs dependencies :
pip install --user requests selenium lxml
How to Run? :
python auto-sshub.py [start/finish/check/check_leave_quota/check_leave_status]
start
to Start Workingstop
to Stop Workingcheck
to Check Total Working Time. Auto Stop Working if Total Working Time is larger or equal to nine hourscheck_leave_quota
to Check your number of leave days on your accountcheck_leave_status
to Check your leave status on your account (Approved only)Example
python auto-sshub.py finish