-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
512 lines (491 loc) · 51.7 KB
/
main.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#importing libraries here...
from selenium import webdriver
import subprocess
import datetime
import imaplib
import logging
import shutil
import email
import time
import sys
import re
#=====================================================================================================
#==[Bot staring time]================================================================================
starting_time = datetime.datetime.now()
bt = starting_time.strftime("%A"), "Date", starting_time.date(), "/", "Time", starting_time.time()
f = open('bot_time.txt', 'w')
f.write(str(bt))
f.close()
#=====================================================================================================
#=====[Making log file here]==============================>
logging.basicConfig(filename="bot.log", format='%(asctime)s %(message)s', filemode='w')
# Creating an object
logger = logging.getLogger()
# Setting the threshold of logger to DEBUG
logger.setLevel(logging.DEBUG)
logger.info("Bot started now")
#=======================================================================================================
#[servername - Patner.gtel.com] server 1 username and password
s1_admin_username = 'Anil'
s1_admin_password = 'Anil2522'
#[servername - Patner.gtel] server 2 username and password
s2_admin_username = 'dhriti'
s2_admin_password = '112233'
#[servername - patner.gtel.conaxia] server 3 username and password
s3_admin_username = 'dhritinetwork'
s3_admin_password = 'dhritinetwork508'
#======================================================================================================
# All website url list
url_1 = "https://partner.gtel.in/Partner/Default.aspx" #server_1
url_2 = "http://partner.conexiaworld.com" #server_2
url_3 = "https://partner.gtel.in/Partner/Default.aspx" #server_3
#======================================================================================================
#bot email id and password
botemail = "[email protected]"
botemailpassword = "xxx@123456"
#======================================================================================================
imap_url = 'imap.gmail.com'
#======================================================================================================
logger.info("Bot reading emails...")
logger.info("=====================================")
#=============[Reading email here..]===============================>>
mail = imaplib.IMAP4_SSL(imap_url, 993)
mail.login(botemail, botemailpassword)
num_of_mail = 0
#=======================================================================================================>
while True:
#=======[Loader start here]=========>>
#=======================================================================================>>>
#if any email is not comming in bot email box then wait infinite time....
print("Waitting for website response...")
logger.debug("bot waitting for website response here...")
#------------------------------------------------->
# animation = ["10%", "20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%"]
animation = ["[■□□□□□□□□□]", "[■■□□□□□□□□]", "[■■■□□□□□□□]", "[■■■■□□□□□□]", "[■■■■■□□□□□]", "[■■■■■■□□□□]", "[■■■■■■■□□□]", "[■■■■■■■■□□]", "[■■■■■■■■■□]", "[■■■■■■■■■■]"]
for i in range(len(animation)):
time.sleep(0.2)
sys.stdout.write("\r" + animation[i % len(animation)])
sys.stdout.flush()
print("\n")
#======[loader end]=================================================>
mail.select('INBOX')
typ, data = mail.search(None, "UNSEEN")
mail_ids = data[0]
id_list = mail_ids.split()
if len(id_list) > num_of_mail:
# ================================================================================>>
# 1st -> if any new comes than read that email store
f = open("test_1.txt", "w")
f.write("true")
f.close()
f = open("test_1.txt", "r")
website_response = f.read()
for i in reversed(id_list):
type, data = mail.fetch(i, '(RFC822)')
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1].decode('utf-8', errors='ignore'))
num_of_mail = len(id_list)
# ========[condition here]==========================================================================================>>
logger.warning("waiting for true false")
if website_response == 'true':
logger.info("condition is true now")
logger.info("reading email comes fro website")
# =====[Email reading here....]===========>>
# Connect to inbox
imap_server = imaplib.IMAP4_SSL(host='imap.gmail.com')
imap_server.login('[email protected]', 'xxx@123456')
#============================================================
imap_server.select() # Default is `INBOX`
_, message_numbers_raw = imap_server.search(None, 'ALL')
for message_number in message_numbers_raw[0].split():
_, msg = imap_server.fetch(message_number, '(RFC822)')
x = msg[0][1]
#============================================================
logger.debug("making a file mail_data_file.txt")
f = open('mail_data_file.txt', 'w')
f.write(str(x))
logger.info("writting in file")
f.close()
#============================================================
logger.info("opening a mail_date_file.txt and reading and cleaing now")
with open('mail_data_file.txt', 'r') as file:
for line in file:
for word in line.split():
# reading the file writting splited word inside the text file.
f = open("test.txt", "w")
# ignoring the html here....
v = word
result = re.sub("<.|/\'*?>", "", v)
clean = result
f.write(clean)
f.close()
#=========================>
logger.info("writing a one more file called test.txt")
string = open('test.txt').read()
new_str = re.sub('[\r\n\']', ' ', string)
logger.info("writing a one more file called test1.txt")
open('test1.txt', 'w').write(new_str)
#reamoving \r\n\ =====================>
f = open('test1.txt')
for line in f.readlines():
line = line[:-2].replace('\\r\\', '')
print(repr(line))
# =====================>
logger.info("writing a one more file called test2.txt")
f = open('test2.txt', 'w')
f.write(repr(line))
f.close()
# =============================>
logger.info("reading test2.txt")
string = open('test2.txt').read()
new_str = re.sub('[^a-zA-Z0-9\n\.]', '', string)
logger.info("opening username.txt file")
open('username.txt', 'w').write(new_str)
#======================================>>
logger.info("opening username.txt file agin")
f = open('username.txt', 'r')
clientname = f.read()
print("Client Name -->", clientname)
# Reading mail data for server umber detect and take action acc. to the server number.
with open('mail_data_file.txt', 'r') as file:
for line in file:
for part in line.split():
if "Server:" in part:
f = open('s1.txt', 'w')
f.write(part)
f.close()
string = open('s1.txt').read()
new_str = re.sub('[^a-zA-Z0-9\n\r\.]', ' ', string)
open('s2.txt', 'w').write(new_str)
with open('s2.txt', 'r') as f:
for line in f:
server_num = line.split(' ')[7]
f = open('server_number.txt', 'w')
f.write(server_num)
f.close()
f = open('server_number.txt', 'r')
server_number = f.read()
print("Server Number -->", server_number)
logger.info(" this is server number")
#=========================================================>>
#===[This is server [1]====>>
if server_num == '1':
logger.debug("Server one is accessed")
print("This is server 1")
print("Username --> ", s1_admin_username, "|" "Password --> ", s1_admin_password)
# ---------------------------------------------------------------------------->
logger.debug("insitining firefox webdriver now")
driver = webdriver.Firefox(executable_path="C:\Program Files (x86)\geckodriver.exe")
driver.get(url_1)
logger.info("driver is searching for server 1 url")
time.sleep(0.10)
# ============[Bot is click and input the username here]================>>>
logger.info("logining in website")
driver.find_element_by_xpath('//*[@id="txtUserName"]').click()
driver.find_element_by_xpath('//*[@id="txtUserName"]').send_keys(s1_admin_username)
# ============[Bot is click and input the Password here]================>>>
driver.find_element_by_xpath( '//*[@id="txtPassword"]').click()
driver.find_element_by_xpath('//*[@id="txtPassword"]').send_keys(s1_admin_password)
# ============[Bot is now clicking on login button to login in the website]====>>>
driver.find_element_by_xpath('//*[@id="save"]').click()
logger.info("successfully logged in")
# =======[ Now bot is trying to all user account ]=====================>>>
driver.get('https://partner.gtel.in/Partner/Accounts.aspx')
time.sleep(0.10)
# ======[ Now bot is trying to click search bar ]===============================>>>
driver.find_element_by_xpath('//*[@id="ContentPlaceHolder1_txtserch"]').click()
# ======[ Now bot is trying type on search bar ]==============================================>>
logger.info("Now client name writting in search bar")
driver.find_element_by_xpath('//*[@id="ContentPlaceHolder1_txtserch"]').send_keys(clientname)
time.sleep(1)
# ======[click on search button]===========================================>
driver.find_element_by_xpath('//*[@id="ContentPlaceHolder1_btnserch"]').click()
time.sleep(1)
# ================[Bot is trying to click on renew plan]=======================================>>>
popup = driver.find_element_by_xpath('/html/body/div[2]/form/div[4]/div[2]/div/div[1]/div[1]/table/tbody/tr/td[2]/div[1]/div/div/table/tbody/tr[2]/td[12]/input').click()
time.sleep(1)
# ==============[Scroll here this gonna be easy]=========================================>>>
scr1 = driver.find_element_by_xpath('/html/body/div[2]/form/div[4]/div[2]/div/div[1]/div[3]')
driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", scr1)
time.sleep(1)
# --[Bot is clicking on save button on popup-->>
driver.find_element_by_xpath('//*[@id="btnPurchaseRenewPlanClose"]').click()
time.sleep(1)
logger.info("recharge done successfully.")
# ============[This is reporting section start here]===============================>>>
driver.save_screenshot("user_status.png")
logger.info(" [+] Screenshot taken successfully")
# ===========[Now moving to another folder screenshot for managing space to next screenshot]=======>>
original = r'C:/Users/Dhritinet/PycharmProjects/Dhritinetwork_Bot/user_status.png'
target = r'C:/Users/Dhritinet/PycharmProjects/Dhritinetwork_Bot/db/user_status.png'
shutil.move(original, target)
# ===========[All Task Complete successfully logout now..]=======>>
driver.find_element_by_xpath('//*[@id="lbklogout"]').click()
time.sleep(0.10)
logger.info( " [+] Successfully task completed Now.")
logger.info(" [+] Closing entire browser")
""" Here Bot is trying close entire browser """
driver.close()
# ========[Deleting new email from inbox]===========================>
box = imaplib.IMAP4_SSL('imap.gmail.com', 993)
box.login(botemail, botemailpassword)
box.select('Inbox')
typ, data = box.search(None, 'ALL')
for num in data[0].split():
box.store(num, '+FLAGS', '\\Deleted')
box.expunge()
box.close()
box.logout()
logger.info(" [+] New Email is Deleted. ")
logger.info("Successfully server 1 recharge done.")
logger.info("Back in infinity loop")
# ============[finishing up logging ]================================>>
# =================================================================>
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
fromaddr = botemail
toaddr = "[email protected]"
# instance of MIMEMultipart
msg = MIMEMultipart()
# storing the senders email address
msg['From'] = fromaddr
# storing the receivers email address
msg['To'] = toaddr
# storing the subject
msg['Subject'] = "Subject of the Mail"
# string to store the body of the mail
body = "Body_of_the_mail"
# attach the body with the msg instance
msg.attach(MIMEText(body, 'plain'))
# open the file to be sent
filename = "user_status.png"
attachment = open("C:/Users/Dhritinet/PycharmProjects/Dhritinetwork_Bot/db/user_status.png", "rb")
# instance of MIMEBase and named as p
p = MIMEBase('application', 'octet-stream')
# To change the payload into encoded form
p.set_payload((attachment).read())
# encode into base64
encoders.encode_base64(p)
p.add_header('Content-Disposition',
"attachment; filename= %s" % filename)
# attach the instance 'p' to instance 'msg'
msg.attach(p)
# creates SMTP session
s = smtplib.SMTP('smtp.gmail.com', 587)
# start TLS for security
s.starttls()
# Authentication
s.login(fromaddr, botemailpassword)
# Converts the Multipart msg into a string
text = msg.as_string()
# sending the mail
s.sendmail(fromaddr, toaddr, text)
# terminating the session
s.quit()
# =================================================================>
#==========================================================================>
# ===[This is server [2]====>>
elif server_num == '2':
print("This is server 2")
print("Username --> ", s2_admin_username, "|" "Password --> ", s2_admin_password)
# ----------------------------------------------------------------------------------->
driver = webdriver.Firefox( executable_path="C:\Program Files (x86)\geckodriver.exe")
driver.get(url_2)
time.sleep(0.10)
# ============[Bot is click and input the username here]================>>>
driver.find_element_by_xpath('//*[@id="txtUserName"]').click()
driver.find_element_by_xpath('//*[@id="txtUserName"]').send_keys(s2_admin_username)
# ============[Bot is click and input the Password here]================>>>
driver.find_element_by_xpath('//*[@id="txtPassword"]').click()
driver.find_element_by_xpath('//*[@id="txtPassword"]').send_keys(s2_admin_password)
# ============[Bot is now clicking on login button to login in the website]====>>>
driver.find_element_by_xpath('//*[@id="save"]').click()
# =======[ Now bot is trying to all user account ]=====================>>>
driver.get('http://partner.conexiaworld.com/Partner/Accounts.aspx')
time.sleep(0.10)
# ======[ Now bot is trying to click search bar ]===============================>>>
driver.find_element_by_xpath('//*[@id="ContentPlaceHolder1_txtserch"]').click()
# ======[ Now bot is trying type on search bar ]==============================================>>
driver.find_element_by_xpath('//*[@id="ContentPlaceHolder1_txtserch"]').send_keys(clientname)
time.sleep(1)
# ======[click on search button]===========================================>
driver.find_element_by_xpath('//*[@id="ContentPlaceHolder1_btnserch"]').click()
time.sleep(1)
# ================[Bot is trying to click on renew plan]=======================================>>>
popup = driver.find_element_by_xpath('/html/body/div[2]/form/div[4]/div[2]/div/div[1]/div[1]/table/tbody/tr/td[2]/div[1]/div/div/table/tbody/tr[2]/td[12]/input').click()
time.sleep(1)
# ==============[Scroll here this gonna be easy]=========================================>>>
scr1 = driver.find_element_by_xpath('/html/body/div[2]/form/div[4]/div[2]/div/div[1]/div[3]')
driver.execute_script( "arguments[0].scrollTop = arguments[0].scrollHeight", scr1)
time.sleep(1)
# --[Bot is clicking on cancel button on popup-->>
driver.find_element_by_xpath('/html/body/div[2]/form/div[4]/div[2]/div/div[1]/div[3]/table/tbody/tr[40]/td/button[2]').click()
time.sleep(1)
# ============[This is reporting section start here]===============================>>>
driver.save_screenshot("user_status.png")
logger.info(" [+] Screenshot taken successfully")
# ===========[Now moving to another folder screenshot for managing space to next screenshot]=======>>
original = r'C:/Users/Dhritinet/PycharmProjects/Dhritinetwork_Bot/user_status.png'
target = r'C:/Users/Dhritinet/PycharmProjects/Dhritinetwork_Bot/db/user_status.png'
shutil.move(original, target)
# ===========[All Task Complete successfully logout now..]=======>>
driver.find_element_by_xpath('//*[@id="lbklogout"]').click()
time.sleep(0.10)
logger.info(" [+] Successfully task completed Now.")
logger.info(" [+] Closing entire browser")
""" Here Bot is trying close entire browser """
driver.close()
# ========[Deleting new email from inbox]===========================>
box = imaplib.IMAP4_SSL('imap.gmail.com', 993)
box.login(botemail, botemailpassword)
box.select('Inbox')
typ, data = box.search(None, 'ALL')
for num in data[0].split():
box.store(num, '+FLAGS', '\\Deleted')
box.expunge()
box.close()
box.logout()
logger.info(" [+] New Email is Deleted. ")
# ============[finishing up logging ]================================>>
mail_content = "Hi, Sir this is your client recharge from server 2 report from bot fo admin please check when you get time." \
"Thank you" \
"Dhritinetwork_Bot"
sender_address = botemail
sender_pass = botemailpassword
receiver_address = '[email protected]'
message = MIMEMultipart()
message['From'] = sender_address
message['To'] = receiver_address
message['Subject'] = 'Report from Dhritinetwork_Bot for Admin'
message.attach(MIMEText(mail_content, 'plain'))
attach_file_name = 'C:/Users/Dhritinet/PycharmProjects/Dhritinetwork_Bot/db/user_status.png'
attach_file = open(attach_file_name, 'rb') # Open the file as binary mode
payload = MIMEBase('application', 'octate-stream')
payload.set_payload((attach_file).read())
encoders.encode_base64(payload) # encode the attachment
# add payload header with filename
payload.add_header('Content-Decomposition', 'attachment', filename=attach_file_name)
message.attach(payload)
# Create SMTP session for sending the mail
session = smtplib.SMTP('smtp.gmail.com', 587) # use gmail with port
session.starttls() # enable security
session.login(sender_address, sender_pass) # login with mail_id and password
text = message.as_string()
session.sendmail(sender_address, receiver_address, text)
session.quit()
logger.info("mail sent")
#================================================================================>
#===[This is server [3]====>>
if server_num == '3':
print("This is 3rd server")
print("Username --> ", s3_admin_username, "|" "Password --> ", s3_admin_password)
#----------------------------------------------------------------------------------->
driver = webdriver.Firefox(executable_path="C:\Program Files (x86)\geckodriver.exe")
driver.get(url_3)
time.sleep(0.10)
# ============[Bot is click and input the username here]================>>>
driver.find_element_by_xpath('//*[@id="txtUserName"]').click()
driver.find_element_by_xpath('//*[@id="txtUserName"]').send_keys(s3_admin_username)
# ============[Bot is click and input the Password here]================>>>
driver.find_element_by_xpath('//*[@id="txtPassword"]').click()
driver.find_element_by_xpath('//*[@id="txtPassword"]').send_keys(s3_admin_password)
# ============[Bot is now clicking on login button to login in the website]====>>>
driver.find_element_by_xpath('//*[@id="save"]').click()
# =======[ Now bot is trying to all user account ]=====================>>>
driver.get('https://partner.gtel.in/Partner/Accounts.aspx')
time.sleep(0.10)
# ======[ Now bot is trying to click search bar ]===============================>>>
driver.find_element_by_xpath('//*[@id="ContentPlaceHolder1_txtserch"]').click()
# ======[ Now bot is trying type on search bar ]==============================================>>
logger.info("Now client name writting in search bar")
driver.find_element_by_xpath('//*[@id="ContentPlaceHolder1_txtserch"]').send_keys(clientname)
time.sleep(1)
# ======[click on search button]===========================================>
driver.find_element_by_xpath('//*[@id="ContentPlaceHolder1_btnserch"]').click()
time.sleep(1)
# ================[Bot is trying to click on renew plan]=======================================>>>
popup = driver.find_element_by_xpath('/html/body/div[2]/form/div[4]/div[2]/div/div[1]/div[1]/table/tbody/tr/td[2]/div[1]/div/div/table/tbody/tr[2]/td[12]/input').click()
time.sleep(1)
# ==============[Scroll here this gonna be easy]=========================================>>>
scr1 = driver.find_element_by_xpath('/html/body/div[2]/form/div[4]/div[2]/div/div[1]/div[3]')
driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight",scr1)
time.sleep(1)
# --[Bot is clicking on save button on popup-->>
driver.find_element_by_xpath('//*[@id="btnPurchaseRenewPlanClose"]').click()
time.sleep(1)
logger.info("recharge done successfully.")
# ============[This is reporting section start here]===============================>>>
driver.save_screenshot("user_status.png")
logger.info(" [+] Screenshot taken successfully")
# ===========[Now moving to another folder screenshot for managing space to next screenshot]=======>>
original = r'C:/Users/Dhritinet/PycharmProjects/Dhritinetwork_Bot/user_status.png'
target = r'C:/Users/Dhritinet/PycharmProjects/Dhritinetwork_Bot/db/user_status.png'
shutil.move(original, target)
# ===========[All Task Complete successfully logout now..]=======>>
driver.find_element_by_xpath('//*[@id="lbklogout"]').click()
time.sleep(0.10)
logger.info(" [+] Successfully task completed Now.")
logger.info(" [+] Closing entire browser")
#Here Bot is trying close entire browser=========================>
driver.close()
# ========[Deleting new email from inbox]===========================>
box = imaplib.IMAP4_SSL('imap.gmail.com', 993)
box.login(botemail, botemailpassword)
box.select('Inbox')
typ, data = box.search(None, 'ALL')
for num in data[0].split():
box.store(num, '+FLAGS', '\\Deleted')
box.expunge()
box.close()
box.logout()
logger.info(" [+] New Email is Deleted. ")
# ============[finishing up logging ]================================>>
mail_content = "Hi, Sir this is your client recharge from server 3 report from bot fo admin please check when you get time." \
"Thank you" \
"Dhritinetwork_Bot"
sender_address = botemail
sender_pass = botemailpassword
receiver_address = '[email protected]'
message = MIMEMultipart()
message['From'] = sender_address
message['To'] = receiver_address
message[
'Subject'] = 'Report from Dhritinetwork_Bot for Admin'
message.attach(
MIMEText(mail_content, 'plain'))
attach_file_name = 'C:/Users/Dhritinet/PycharmProjects/Dhritinetwork_Bot/db/user_status.png'
attach_file = open(attach_file_name, 'rb') # Open the file as binary mode
payload = MIMEBase('application', 'octate-stream')
payload.set_payload((attach_file).read())
encoders.encode_base64(payload) # encode the attachment
# add payload header with filename
payload.add_header('Content-Decomposition', 'attachment', filename=attach_file_name)
message.attach(payload)
# Create SMTP session for sending the mail
session = smtplib.SMTP('smtp.gmail.com', 587) # use gmail with port
session.starttls() # enable security
session.login(sender_address, sender_pass) # login with mail_id and password
text = message.as_string()
session.sendmail(sender_address, receiver_address, text)
session.quit()
logger.info("mail sent")
#====================================================================================>>
else:
logger.info(" [+] Excuting main file now.. ")
subprocess.call("python main.py")
# =================================================================>
# =======[The creadit Section from developer]========================>>
logger.info(" [+] Dhritinetwork_Bot - Version 1.0")
logger.info(" [+] Made By : Ashutosh kumar Gautam")
logger.info(" [+] Contact me : Email - [email protected]")
logger.info(" <-- [+] Closing Date and Time ")
logger.info("Back in infinite now...")
print(" [+] Back in infinite loop now...")
logger.info("[======================================================================================>")
subprocess.call("python main.py",shell=True)
subprocess.call("python main.py", shell=True)
continue