-
Notifications
You must be signed in to change notification settings - Fork 0
/
balance.py
164 lines (133 loc) · 4.85 KB
/
balance.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#! /usr/bin/env python3
import os
import sys
import time
import notify2
from datetime import datetime
from pprint import pprint
from daterelate.daterelate import relate
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, TimeoutException
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as conditions
from selenium.webdriver.support import ui
from configreader import ConfigReader
os.environ['DISPLAY'] = ':0' # Set the display if set to run as cronjob
HOMEPAGE = 'http://myaccount.telkom.co.ke'
TODAY = datetime.now()
config = ConfigReader('defaults.ini')
TITLE = config.get('notificationtitle', default='Telkom Balance')
NUMBER = config.get('number', section='credentials', default='')
PASSWD = config.get('pass', section='credentials', default='')
s = os.path.join(os.path.expanduser('~'), 'bin')
driver_path = config.get('driverspath', default=s)
chrome_driver_name = config.get('chromedrivername',
section='Chrome',
default='chromedriver')
firefox_driver_name = config.get('firefoxdrivername',
section='Firefox',
default='geckodriver')
headless = config.get('headless', default=True)
chrome_options = ChromeOptions()
firefox_options = FirefoxOptions()
if headless:
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
firefox_options.add_argument('--headless')
os.environ['MOZ_HEADLESS'] = '1'
browsers = {
'chrome': {
'filename': chrome_driver_name,
'class': webdriver.Chrome,
'kwargs': {
'chrome_options': chrome_options
}
},
'firefox': {
'filename': firefox_driver_name,
'class': webdriver.Firefox,
'kwargs': {
'firefox_options': firefox_options,
'log_path': os.path.join(
driver_path, 'geckodriver.log')
}
}
}
choice_browser = browsers[config.get('Browser', default='chrome')]
driver_full_path = os.path.join(
driver_path, choice_browser['filename'])
choice_browser['kwargs']['executable_path'] = driver_full_path
read = notify2.init(TITLE)
notifier = notify2.Notification(TITLE, 'Querying')
notifier.show()
def alert(message, title=TITLE):
notifier.update(title, message)
notifier.show()
kwargs = choice_browser.get('kwargs', {})
def login(browser):
"""Log in to the website using the provided credentials"""
number_input = browser.find_element_by_xpath(
"//div[@class='login_form']/div[@id='divInputNumber']/input")
pwd_input = browser.find_element_by_xpath(
"//div[@class='login_form']/div[@id='divInputPwd']/input")
login_btn = browser.find_element_by_id('userLoginBtn')
number_input.clear()
number_input.send_keys(NUMBER)
pwd_input.clear()
pwd_input.send_keys(PASSWD)
login_btn.click()
def query():
"""Load selenium and scrape"""
browser = choice_browser.get(
'class', webdriver.Chrome)(**kwargs)
browser.get(HOMEPAGE)
try:
# Wait until logout button is visible
ui.WebDriverWait(browser, 3).until(
conditions.visibility_of_element_located(
(By.ID, 'userLogoutBtn')))
# browser.find_element_by_id('userLogoutBtn').click()
except TimeoutException:
# Login if logout button not visible
login(browser)
try:
# Locate details table
ui.WebDriverWait(browser, 15).until(
conditions.visibility_of_element_located(
(By.CLASS_NAME, 'table_main')))
except TimeoutException:
browser.close()
sys.exit(1)
else:
table = browser.find_element_by_class_name('table_main')
rows = table.find_elements_by_tag_name('tr')
messages = []
for row in rows[1:6]:
message = ''
tds = row.find_elements_by_tag_name('td')
for td in tds:
try:
input = td.find_element_by_tag_name('input')
except NoSuchElementException:
text = td.text.strip()
message += '{}'.format(text)
else:
text = input.get_attribute('value')
message += ': {}\n'.format(text)
try:
date = datetime.strptime(text, '%d-%m-%Y')
except ValueError:
pass
else:
message += relate(date, TODAY, future='to expiry', past='ago')
messages.append(message)
browser.close()
pprint(messages)
for message in messages:
# Display notification
alert(message)
time.sleep(3)
if __name__ == "__main__":
query()