-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudemy_enroller_chrome.py
169 lines (121 loc) · 5.34 KB
/
udemy_enroller_chrome.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
# Install all the requirements by running requirements.py in IDLE or follow the alternate instructions at
# https://github.com/aapatre/Automatic-Udemy-Course-Enroller-GET-PAID-UDEMY-COURSES-for-FREE/ Make sure you have
# cleared all saved payment details on your Udemy account & the browser!
import time
from multiprocessing.dummy import Pool
import requests
from bs4 import BeautifulSoup
from ruamel.yaml import YAML
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
yaml = YAML()
with open("settings.yaml") as f:
settings = yaml.load(f)
email, password = settings["udemy"]["email"], settings["udemy"]["password"]
# Accounts for the edge case that someone removes the entire "zipcode" entry in settings.yaml instead of simply clearing the string or leaving it alone
# This shouldn't have to exist, but ?? here we are
if "zipcode" in settings["udemy"]:
zipcode = settings["udemy"]["zipcode"]
driver = webdriver.Chrome(ChromeDriverManager().install())
# Maximizes the browser window since Udemy has a responsive design and the code only works
driver.maximize_window()
# in the maximized layout
def getUdemyLink(url):
response = requests.get(url=url)
soup = BeautifulSoup(response.content, "html.parser")
linkForUdemy = soup.find("span",
class_="rh_button_wrapper").find("a").get("href")
return linkForUdemy
def gatherUdemyCourseLinks(courses):
"""
Threaded fetching of the udemy course links from tutorialbar.com
:param list courses: A list of tutorialbar.com course links we want to fetch the udemy links for
:return: list of udemy links
"""
thread_pool = Pool()
results = thread_pool.map(getUdemyLink, courses)
thread_pool.close()
thread_pool.join()
return results
def getTutorialBarLinks(url):
response = requests.get(url=url)
soup = BeautifulSoup(response.content, "html.parser")
links = soup.find("div", class_="rh-post-wrapper").find_all("a")
# print(links)
courses = []
x = 0
for i in range(12):
courses.append(links[x].get("href"))
x = x + 3
return courses
def udemyLogin(email_text, password_text):
driver.get("https://www.udemy.com/join/login-popup/")
userEmail = driver.find_element_by_name("email")
userPassword = driver.find_element_by_name("password")
userEmail.send_keys(email_text)
userPassword.send_keys(password_text)
driver.find_element_by_name("submit").click()
def redeemUdemyCourse(url):
driver.get(url)
print("Trying to Enroll for: " + driver.title)
# Enroll Now 1
element_present = EC.presence_of_element_located(
(By.XPATH, "//button[@data-purpose='buy-this-course-button']"))
WebDriverWait(driver, 10).until(element_present)
udemyEnroll = driver.find_element_by_xpath(
"//button[@data-purpose='buy-this-course-button']") # Udemy
udemyEnroll.click()
# Enroll Now 2
element_present = EC.presence_of_element_located((
By.XPATH,
'//*[@class="udemy pageloaded"]/div[1]/div[2]/div/div/div/div[2]/form/div[2]/div/div[4]/button',
))
WebDriverWait(driver, 10).until(element_present)
# Check if zipcode exists before doing this
if zipcode:
# Assume sometimes zip is not required because script was originally pushed without this
try:
zipcode_element = driver.find_element_by_id(
"billingAddressSecondaryInput")
zipcode_element.send_keys(zipcode)
# After you put the zip code in, the page refreshes itself and disables the enroll button for a split second.
time.sleep(1)
except NoSuchElementException:
pass
udemyEnroll = driver.find_element_by_xpath(
'//*[@class="udemy pageloaded"]/div[1]/div[2]/div/div/div/div[2]/form/div[2]/div/div[4]/button'
) # Udemy
udemyEnroll.click()
def main_function():
page = 1 # Change the page number here only if necessary, else ignore
loop_run_count = 0
while True:
print("Please Wait: Getting the course list from tutorialbar.com...")
print("Page: " + str(page) + ", Loop run count: " +
str(loop_run_count))
url = "https://www.tutorialbar.com/all-courses/" + "page/" + str(
page) + "/"
courses = getTutorialBarLinks(url)
udemyLinks = gatherUdemyCourseLinks(courses)
for counter, course in enumerate(udemyLinks):
print("Received Link {} : {}".format((counter + 1), course))
if loop_run_count == 0:
udemyLogin(email, password)
for link in udemyLinks:
# noinspection PyBroadException
try:
redeemUdemyCourse(link)
except BaseException as e:
print(
"Unable to enroll for this course either because you have already claimed it or the browser "
"window has been closed!")
page = page + 1
loop_run_count = loop_run_count + 1
print(
"Moving on to the next page of the course list on tutorialbar.com")
main_function()