This repository has been archived by the owner on Apr 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watchbot.py
232 lines (195 loc) · 6.78 KB
/
watchbot.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
from bothandle import Browser
import re
# from beepy import beep
from bs4 import BeautifulSoup as BSP
from time import sleep, time
from assessment_data import answers
VERSION = "BETA 4.1.0"
class WatchBot:
"""WATCH BOT BETA-4x"""
def __init__(self,name,phnum):
self.uname = name
self.phnum = phnum
self.web = None
def run(self):
""" Cycle and Watch lessons """
back_url = [self.web.get_current_url()]
while back_url:
cl = self.cycle(back_url)
if len(back_url)==1:cl= self.cycle(back_url)
if not back_url: break
print()
self.start_watch(cl)
def cycle(self,back_url):
""" Navigate Courses """
cl = None
print("<-")
self.web.go_to(back_url.pop())
while True:
sleep(2)
nlid = nextlessonid(self.web.get_page_source())
if not nlid: break
cl = nlid
print(nlid,end=" -> ")
back_url.append(self.web.get_current_url())
self.web.click(nlid)
print()
return cl
def start_watch(self,cl):
""" Watch Lessons """
names = get_names(self.web)
sleep(.3)
while names:
if next_available(self.web,names[0]):
names.pop(0)
self.web.click("Next")
sleep(1)
elif "Assessment" in names[0]:
#beep(sound=3)
handle_assessment(self.web,self.uname,cl,answers.get(cl,'NAN'))
break
else:
print('Watching: '+names[0],end="\n")
s = time()
sleep_until_video_ends(self.web,names[0])
e = time()
print(f"==> Completed in {(e-s)/60:.2f}mins\n")
print(f"\nSuccessfully Completed: {cl}\n")
def openw(self,):
"""Open Chrome Window"""
self.web = Browser()
self.web.workbot(self.uname, self.phnum)
self.web.go_to("https://naanmudhalvan.azurewebsites.net/")
sleep(.3)
def signin(self):
""" Automated Signin """
# Signing in
self.web.click("SIGN IN",tag="button")
sleep(.3)
# Mobile and Verification
self.web.click(classname="PhoneInputCountrySelect",tag="select")
sleep(.3)
self.web.click("India",tag="option")
sleep(.4)
self.web.type(self.phnum,into="Enter your mobile number")
sleep(.4)
self.web.click(classname="primary-action",tag="div")
otp = usr_input("Enter OTP:", 6)
self.web.type(otp,"Enter 6 digit code",tag= "input")
self.web.click(classname="primary-action",tag="div")
# ------------------------------------------ UTILS------------------------------------------------------------
def script_exec_4x(obj):
obj.execute_script("""
document.querySelectorAll('video').forEach((videoElement) => {
videoElement.playbackRate = 4;
});""")
def make_soup(obj):
return BSP(obj.get_page_source(),"html.parser")
def usr_input(msg,constrain):
inp = ""
while len(inp) != constrain:
inp = input(msg)
return inp
# ---------------------------------------- For Course Navigation ---------------------------------------------
def nextlessonid(html):
pattern = "[0-9]{1,3}% completed"
reng = list(re.finditer(pattern, html))
if not reng: return
out = []
for r in reng:
i = r.start()
s,e=i,i-2
while html[s] != "%": s+=1
if int(html[i:s])>=100: continue
while s>1 and html[s-1] != '"': s-=1
return html[s:e]
return ""
# --------------------------------------------------- For Lessons Navigation ----------------------------------------------
def tosec(stamp):
a,b = stamp.split(':')
return (int(a)*60+int(b))/4
def get_names(obj):
illegal = ["This is a modal window.","","Previous","Next"]
soup = make_soup(obj)
out = [x.text for x in soup.find_all('p') if not x.text in illegal]
return out
def next_available(obj,name):
soup = make_soup(obj)
curr = None
for x in soup.find_all('p'):
if x.text == name:
curr = x
break
if not curr:
dd = soup.find(attrs={"class":"vjs-duration-display"})
exep = True
if dd:
m = tosec(dd.text[-5:])
n = tosec(soup.find(attrs={"class":"vjs-current-time"}).text[-9:-4])
exep = 0<=(m-n)<=1
return not ("disabled" in soup.find(attrs={"name":"Next"}).attrs) and exep
i = curr.parent.find('i')
return (i and i.attrs['data-icon-name'] == "Accept")
def sleep_until_video_ends(obj,name):
# Video time
soup = make_soup(obj)
dd = soup.find(attrs={"class":"vjs-duration-display"})
if not dd:
print("==> Not Video file")
return
script_exec_4x(obj)
tot = tosec(dd.text[-5:])
m = tot
print(f"==> Est. time {m/60:.2f}mins")
dif = 5
while True:
sleep(dif)
m-=dif
if m<=0:
soup = make_soup(obj)
n = tosec(soup.find(attrs={"class":"vjs-current-time"}).text[-9:-4])
#sleep delta
m = tot-n
else: script_exec_4x(obj)
if next_available(obj,name): break
def handle_assessment(obj,name,cl,link):
print(f"\n\nHey {name}, \n\tPlease Enter \'completed\' after manually completing the assessment and claiming the certificate!(i.e. 100% completed)")
print(f"\t{cl} Assessment Answers: {link}")
print("\tNew Tabs opened:- ",end=" ")
obj.new_tab(url="https://www.google.com")
print("Tab1-Google",end="")
if len(link)>3:
obj.new_tab(url=link)
print(", Tab2-Youtube")
print("You can close the new tabs.")
usr_input(">", 9)
def main():
print(f"""
\t~~~~ ~~~~ ~~~~ WatchBOT 4x ~~~~ ~~~~ ~~~~\n
Author: Satz
Version: {VERSION}
----> Server side problems will lead to crash.
----> Don't close the chrome window and Don't change the current page.
----> Follow bot instructions.
----> Stable internet connection is recommended (Set video quality lower for better experience).
----> Terminate the bot by pressing 'CTR+C'.
Note:
Bot can not able to autoplay the first video of the lesson, so the user is requested to play it manually.
You should manually complete the assessment test when you get notified by the bot.
\n
""")
name = input("Enter Name(For notifications):")
phnum = usr_input("Phone Number:", 10)
print("\n\n")
bot = WatchBot(name,phnum)
bot.openw()
print("Check your OTP before you press enter!")
bot.signin()
print("\n")
s = time()
tot = bot.run()
e = time()
if (e-s<10): print("Unusual Runtime")
print(f"\nTotal exc. Time {(e-s)/60:.2f}mins")
if __name__ == "__main__":
main()