-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssign_fasih_by_idwilker.py
297 lines (271 loc) · 16.6 KB
/
Assign_fasih_by_idwilker.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
def RUN():
## Try all
try:
## Loop try importing Library
while True:
## if have thes lib
try:
from getpass import getpass
from selenium import webdriver
#from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
import datetime
import pandas as pd
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
#import IPython
import csv
from win10toast import ToastNotifier
#from IPython.display import Audio
#sound_file = '../sound.mp3'
break
## If dont have that lib, then install them
except:
import sys
import subprocess
subprocess.check_call([sys.executable, '-m', 'pip', 'install', "win10toast==0.9", 'PyYAML==3.11'])
subprocess.check_call([sys.executable, '-m', 'pip', 'install', "selenium==3.141.0", 'PyYAML==3.11'])
subprocess.check_call([sys.executable, '-m', 'pip', 'install', "urllib3==1.26.16", 'PyYAML==3.11'])
subprocess.check_call([sys.executable, '-m', 'pip', 'install', "pandas", 'PyYAML==3.11'])
import os
os.system('cls')
## end true importing library
## notification for windows os
class MyToastNotifier(ToastNotifier):
def __init__(self):
super().__init__()
def on_destroy(self, hwnd, msg, wparam, lparam):
super().on_destroy(hwnd, msg, wparam, lparam)
return 0
notif = MyToastNotifier()
## Intro info login SSO
print('# ASSIGN FASIH VIA ID WILKERSTAT v12.05.2024 #\n[untuk yang gada id_wilkerstat hubungi admin dulu de] \n')
print('# Masukin SSO untuk login fasih (password emang gamuncul, \nketik aja trus enter kalo udah):')
from _EDIT import sso_pegawai as user
ssoname = user['username']
ssopass = user['password']
## Open mozilla
print("# Opening mozilla, tunggu buka dulu")
#s = Service(r'../geckodriver.exe')
driver = webdriver.Firefox(executable_path = "geckodriver.exe")
# service_log_path="C:\\Users\\[username]\\AppData\\Local\\Temp\\geckodriver.log")
try:
## GET TO URL
print("# Opening fasih-sm.bps.go.id")
#notif.show_toast("Assign fasih PY", "I Need U to PRESS ENTER :(", duration = 1)
#input("# Udah make vpn? Jika udah, PRESS ENTER")
driver.get('https://fasih-sm.bps.go.id')
print("# Loading, don't do anything")
## Login SSO
WebDriverWait(driver, 15).until( #using explicit wait for x seconds
EC.presence_of_element_located((By.XPATH, "id('login-in')/A[2]")) #finding the element
).click()
## input SSO
WebDriverWait(driver, 15).until( #using explicit wait for x seconds
EC.presence_of_element_located((By.XPATH, 'id("kc-login")')) )
driver.find_element_by_xpath('//*[@id="username"]').send_keys(ssoname)
driver.find_element_by_xpath('//*[@id="password"]').send_keys(ssopass)
driver.find_element_by_xpath('//*[@id="kc-login"]').send_keys(Keys.RETURN)
except:
raise Exception("Login SSO dulu")
## !! Pilih survei
WebDriverWait(driver, 100).until( #using explicit wait for x seconds
EC.presence_of_element_located((By.XPATH, 'id("navbarUserDropdown")/SPAN[1]/IMG[1]')) )
notif.show_toast("Assign fasih PY", "I Need U to MILIH SURVEY :(", duration = 1)
input('# Pilih survei yang mau diassign \n(sampe milih bulan kalau emang survei bulanan).\nJika udah, PRESS ENTER')
##
print('# Data yang mau diassign ada di `assign_daftar.csv`. Kolom wajib ada:')
columns_wajib = ['nm_sampel','ID_Wilkerstat','email_petugas','usersso_pengawas']
print(columns_wajib)
## Read data csv yang mo diassign
print('# Reading assign_daftar.csv')
df = pd.read_csv('assign_daftar.csv')
df.index += 1
print("Size row data: ", len(df))
print("Columns: ", [i for i in df.columns])
print(df)
columns_df = [x for x in columns_wajib if x in df.columns]
## cek kolom data csv
if columns_df != columns_wajib:
kataerror = '# Maaf data tidak memenuhi kriteria. \nKolom wajib ada: '+str(columns_wajib)+\
'\nData anda: '+str([i for i in df.columns])+\
'\nSilakan cek data kembali'
print(kataerror)
notif.show_toast("Assign fasih PY", "ERROR HAPPEN :(", duration = 1)
raise Exception(kataerror)
#exit
## Assign using ID WILKERSTAT
start = 1
end = len(df)
gagal = 0
rowgada = 0
## jika gagal loop terus :""
time.sleep(1)
#notif.show_toast("Assign fasih PY", "I Need U to PRESS ENTER :(", duration = 1)
#input("# PRESS ENTER to Start Assigning")
print('# Start assigning fasih --- ')
while True:
## loop per row daftar_assign csv
try:
for dfrow in range(start,end): ## loop row df
## Search by idwilker
now = datetime.datetime.now()
print(f"{now}| {df.index[dfrow]-1}| {df.ID_Wilkerstat[dfrow]} |", end="")
searchbar = driver.find_element_by_xpath('id("assignmentDatatable_filter")/LABEL[1]/INPUT[1]')
searchbar.clear()
searchbar.send_keys(df.ID_Wilkerstat[dfrow], Keys.RETURN)
time.sleep(2)
## match (nama DAFTAR SAMPEL.csv HARUS SAMA DENGAN DAFTAR SAMPEL FASIH)
try:
## cek ada isian row ato tidak
WebDriverWait(driver, 5).until( #using explicit wait for x seconds
EC.presence_of_element_located((By.XPATH, 'id("assignmentDatatable")/TBODY[2]/TR[1]/TD[3]')) )
pass
except:
## jika kosong maka CONT
print('kosong')
with open('Assign_fasih_log.csv','a', newline='') as fd:
writer = csv.writer(fd)
writer.writerow([df.index[dfrow]-1, now, df.ID_Wilkerstat[dfrow], df.nm_sampel[dfrow], "tidak ada di fasih"])
continue
## Jumlah row yang ditampilin di fasih
#jml_row = 10+1
jml_row = int(driver.find_element_by_css_selector('div#assignmentDatatable_info').text.split()[3]) + 1
for i in range(1,jml_row): ## loop row fasih, cek row yang diinginkan
## cek nama kolom
headusersaatini = driver.find_elements_by_css_selector('.thead-bps td:nth-child(10)')[0].text
if headusersaatini!="User Saat ini": raise Exception("Kolom sudah berubah, hubungi admin suruh sesuaikan lagiii")
idwilker_web = driver.find_elements_by_css_selector('.ng-star-inserted:nth-child('+str(i)+') > .ng-star-inserted:nth-child(8)')[0].text
usersaatini = driver.find_elements_by_css_selector('.ng-star-inserted:nth-child('+str(i)+') > td:nth-child(10)')[0].text
#driver.find_element_by_xpath('id("assignmentDatatable")/TBODY[2]/TR['+i+']/TD[5]').text()
## cek kesamaan
if (df.ID_Wilkerstat[dfrow] == idwilker_web):
#print('== ')
rowgada = 0
break #break loop row fasih
else:
rowgada = 1
break
## jika gada row yang sama maka CONT
if (rowgada == 1):
print('ganemu, mungkin kosong')
with open('Assign_fasih_log.csv','a', newline='') as fd:
writer = csv.writer(fd)
writer.writerow([df.index[dfrow]-1, now, df.ID_Wilkerstat[dfrow], df.nm_sampel[dfrow], "ganemu, mungkin tidak ada di fasih, ricek"])
continue
## cek row matching and tick jika user saat ini masi ()
tick = 0
if (usersaatini !='()'):
## jika dah ke assign maka CONT
print('skip dah ke assign ke '+usersaatini)
## write log
with open('Assign_fasih_log.csv','a', newline='') as fd:
writer = csv.writer(fd)
writer.writerow([df.index[dfrow]-1, now,df.ID_Wilkerstat[dfrow], df.nm_sampel[dfrow], usersaatini, df.email_petugas[dfrow]])
continue #next loop row df
else:
tick = 1
## tick centang buat select by assign
driver.find_elements_by_css_selector('.ng-star-inserted:nth-child('+str(i)+') > td > .ng-untouched')[0].click()
#.ng-star-inserted:nth-child(2) > td > .ng-untouched
## dropdown assign
driver.find_elements_by_css_selector('.btn-group:nth-child(3) > .dropdown-toggle')[0].click() #dropdown assign button
driver.find_elements_by_css_selector('.show > .dropdown-item:nth-child(1)')[0].click() #pilihan assign by selection
WebDriverWait(driver, 15).until( #using explicit wait for x seconds
EC.presence_of_element_located((By.XPATH, "//BODY/NGB-MODAL-WINDOW[1]/DIV[1]/DIV[1]/APP-MODAL-ASSIGN[1]/DIV[2]/DIV[1]/DIV[1]/NGX-SELECT[1]/DIV[1]/DIV[2]/DIV[1]")) #finding the element
)
## assign pengawas
driver.find_elements_by_css_selector('.form-group:nth-child(1) > .col-md-6 .ngx-select__toggle')[0].click()
driver.find_elements_by_css_selector('input.ngx-select__search')[0].send_keys(df.usersso_pengawas[dfrow])
time.sleep(1.2)
driver.find_elements_by_css_selector('a.ngx-select__item')[0].click()
time.sleep(1)
## assign petugas
driver.find_elements_by_css_selector('.form-group:nth-child(2) > .col-md-6 .ngx-select__toggle')[0].click()
driver.find_elements_by_css_selector('input.ngx-select__search')[0].send_keys(df.email_petugas[dfrow])
time.sleep(1)
driver.find_elements_by_css_selector('a.ngx-select__item')[0].click()
## konfirm ok assign
#driver.find_elements_by_css_selector('button.btn-primary:nth-child(1)')[0].send_keys(Keys.RETURN)
## klik assign btn
WebDriverWait(driver, 15).until( #using explicit wait for x seconds
EC.presence_of_element_located((By.CSS_SELECTOR, "button.btn-primary:nth-child(1)")) #assign btn
).click()
## klik konfirmasi
WebDriverWait(driver, 15).until( #using explicit wait for x seconds
#EC.presence_of_element_located((By.XPATH, "//BODY/DIV[2]/DIV[1]/DIV[6]/BUTTON[1]")) #konfirmasi
EC.presence_of_element_located((By.CSS_SELECTOR, "button.swal2-confirm")) #konfirmasi
).click()
## jika gamau ilang modalnya setelah konfirm
try:
WebDriverWait(driver, 2).until( #using explicit wait for x seconds
#EC.presence_of_element_located((By.XPATH, "//BODY/DIV[2]/DIV[1]/BUTTON[1]"))
EC.presence_of_element_located((By.CSS_SELECTOR, "button.swal2-close")) #close berhasil tapi error jika ada
).click()
#time.sleep(1)
## close modal selection petugas
driver.find_elements_by_css_selector('button.close')[0].click()
status_msg = WebDriverWait(driver,2).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, "ngb-modal-window.modal")))
actions.move_to_element(status_msg).perform()
status_msg.find_element_by_css_selector("button.close").click()
except: pass
if (tick == 1): #then untick
driver.find_elements_by_css_selector('.ng-star-inserted:nth-child('+str(i)+') > td > .ng-untouched')[0].click()
tick = 0
print('sukses assign ke '+df.email_petugas[dfrow])
## write log
with open('Assign_fasih_log.csv','a', newline='') as fd:
writer = csv.writer(fd)
writer.writerow([df.index[dfrow]-1, now,df.ID_Wilkerstat[dfrow], df.nm_sampel[dfrow], usersaatini, df.email_petugas[dfrow]])
print('SLEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEESE')
notif.show_toast("Assign fasih PY", "YEYYY SELESE :)", duration = 1)
break
## jika gagal print error
except Exception as e:
print('# Error terjadi: '+str(e))
start = dfrow-1
if start <0: start = 0
gagal += 1
print('# Gagal:',gagal)
## lemme ngitung diff time each attempt yang gagal.
## 7annya cuman pengen kalo 5x attempt gagal waktunya cuman < 3s maka BREAK, biar ga lama2 sampe 100 baru break
listgagal = []
listgagal.append(now)
if len(listgagal)>5:
listgagal.pop(0) #remove timegagal index pertama
diff = listgagal[4]-listgagal[0] #itung delta time
difftime = divmod(diff.days * seconds_in_day + diff.seconds, 1)[0]
if gagal%2 == 0 :
driver.refresh()
time.sleep(5)
########
## !! Komen di bawah ini perlu jika tiap refresh bulan surveinya berbeda. or apalah ketentuannya
########
#notif.show_toast("Assign fasih PY", "I Need U to MILIH SURVEY lagi :(", duration = 1)
print('# Refreshing')
print("# Restart lagi at row: ",start)
WebDriverWait(driver, 15).until( #using explicit wait for x seconds
EC.presence_of_element_located((By.CSS_SELECTOR, 'td.sorting_disabled:nth-child(2)')) )
continue
if ((gagal >100) or (difftime<3)): #!! jika different time gagalnya gagal maka remove aja OR blabla nya
#Audio(sound_file, autoplay=True)
print("# Gagal "+str(gagal)+"X, ada yang salah inih. Dahla nyerah: "+str(e))
notif.show_toast("Assign fasih PY", "ERROR HAPPEN :(", duration = 1)
raise Exception("# Gagal "+str(gagal)+"X, ada yang salah inih. Dahla nyerah: "+str(e))
break
print("# Restart lagi at row: ",start)
continue
## play notif
#Audio(sound_file, autoplay=True)
input("CLOSE PROGRAMS")
## except all
except Exception as e:
print("\n# Error: "+str(e))
notif.show_toast("Assign fasih PY", "ERROR HAPPEN :(", duration = 1)
input("CLOSE AND RESTART")