Skip to content

Commit

Permalink
Merge pull request #27 from aapatre/zip-keyerror
Browse files Browse the repository at this point in the history
Possible fix for KeyError: 'zipcode'
  • Loading branch information
fakeid30 authored Oct 9, 2020
2 parents 9e4b18b + e18121a commit c66af23
Show file tree
Hide file tree
Showing 7 changed files with 357 additions and 258 deletions.
86 changes: 50 additions & 36 deletions udemy_enroller_chrome.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,45 @@
# 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
from bs4 import BeautifulSoup

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.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
import time

yaml = YAML()
with open('settings.yaml') as f:
with open("settings.yaml") as f:
settings = yaml.load(f)

email, password, zipcode = settings['udemy']['email'], settings['udemy']['password'], settings['udemy']['zipcode']
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())

driver.maximize_window() # Maximizes the browser window since Udemy has a responsive design and the code only works
# 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
)
response = requests.get(url=url)

soup = BeautifulSoup(response.content, 'html.parser')
soup = BeautifulSoup(response.content, "html.parser")

linkForUdemy = soup.find('span', class_="rh_button_wrapper").find('a').get('href')
linkForUdemy = soup.find("span",
class_="rh_button_wrapper").find("a").get("href")

return linkForUdemy

Expand All @@ -54,20 +59,18 @@ def gatherUdemyCourseLinks(courses):


def getTutorialBarLinks(url):
response = requests.get(
url=url
)
response = requests.get(url=url)

soup = BeautifulSoup(response.content, 'html.parser')
soup = BeautifulSoup(response.content, "html.parser")

links = soup.find('div', class_="rh-post-wrapper").find_all('a')
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'))
courses.append(links[x].get("href"))
x = x + 3

return courses
Expand All @@ -90,29 +93,37 @@ def redeemUdemyCourse(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']"))
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 = 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"))
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)

# 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)
# 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
# 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
'//*[@class="udemy pageloaded"]/div[1]/div[2]/div/div/div/div[2]/form/div[2]/div/div[4]/button'
) # Udemy
udemyEnroll.click()


Expand All @@ -124,9 +135,11 @@ def main_function():
while True:

print("Please Wait: Getting the course list from tutorialbar.com...")
print("Page: " + str(page) + ", Loop run count: " + str(loop_run_count))
print("Page: " + str(page) + ", Loop run count: " +
str(loop_run_count))

url = "https://www.tutorialbar.com/all-courses/" + "page/" + str(page) + "/"
url = "https://www.tutorialbar.com/all-courses/" + "page/" + str(
page) + "/"
courses = getTutorialBarLinks(url)

udemyLinks = gatherUdemyCourseLinks(courses)
Expand All @@ -149,7 +162,8 @@ def main_function():
page = page + 1
loop_run_count = loop_run_count + 1

print("Moving on to the next page of the course list on tutorialbar.com")
print(
"Moving on to the next page of the course list on tutorialbar.com")


main_function()
89 changes: 52 additions & 37 deletions udemy_enroller_chromium.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,47 @@
# 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
from bs4 import BeautifulSoup

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.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.utils import ChromeType
import time

yaml = YAML()
with open('settings.yaml') as f:
with open("settings.yaml") as f:
settings = yaml.load(f)

email, password, zipcode = settings['udemy']['email'], settings['udemy']['password'], settings['udemy']['zipcode']
email, password = settings["udemy"]["email"], settings["udemy"]["password"]

driver = webdriver.Chrome(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install())
# 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.maximize_window() # Maximizes the browser window since Udemy has a responsive design and the code only works
driver = webdriver.Chrome(
ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).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
)
response = requests.get(url=url)

soup = BeautifulSoup(response.content, 'html.parser')
soup = BeautifulSoup(response.content, "html.parser")

linkForUdemy = soup.find('span', class_="rh_button_wrapper").find('a').get('href')
linkForUdemy = soup.find("span",
class_="rh_button_wrapper").find("a").get("href")

return linkForUdemy

Expand All @@ -55,20 +61,18 @@ def gatherUdemyCourseLinks(courses):


def getTutorialBarLinks(url):
response = requests.get(
url=url
)
response = requests.get(url=url)

soup = BeautifulSoup(response.content, 'html.parser')
soup = BeautifulSoup(response.content, "html.parser")

links = soup.find('div', class_="rh-post-wrapper").find_all('a')
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'))
courses.append(links[x].get("href"))
x = x + 3

return courses
Expand All @@ -91,29 +95,37 @@ def redeemUdemyCourse(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']"))
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 = 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"))
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)

# 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)
# 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
# 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
'//*[@class="udemy pageloaded"]/div[1]/div[2]/div/div/div/div[2]/form/div[2]/div/div[4]/button'
) # Udemy
udemyEnroll.click()


Expand All @@ -125,9 +137,11 @@ def main_function():
while True:

print("Please Wait: Getting the course list from tutorialbar.com...")
print("Page: " + str(page) + ", Loop run count: " + str(loop_run_count))
print("Page: " + str(page) + ", Loop run count: " +
str(loop_run_count))

url = "https://www.tutorialbar.com/all-courses/" + "page/" + str(page) + "/"
url = "https://www.tutorialbar.com/all-courses/" + "page/" + str(
page) + "/"
courses = getTutorialBarLinks(url)

udemyLinks = gatherUdemyCourseLinks(courses)
Expand All @@ -150,7 +164,8 @@ def main_function():
page = page + 1
loop_run_count = loop_run_count + 1

print("Moving on to the next page of the course list on tutorialbar.com")
print(
"Moving on to the next page of the course list on tutorialbar.com")


main_function()
Loading

0 comments on commit c66af23

Please sign in to comment.