Skip to content

Commit

Permalink
Small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
doctor-henry committed Nov 23, 2021
1 parent 8062c17 commit ef53709
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions scribd_dl.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from json import JSONDecodeError
from time import time, sleep

from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support import expected_conditions as EC
from downloader import download_file
from selenium.webdriver.chrome.options import Options

from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

from selenium.webdriver.common.action_chains import ActionChains
from args_parser import ArgsParser
from downloader import download_file

args = ArgsParser()

Expand All @@ -19,7 +19,7 @@ def print_if_verbose(val):
print(val)


WAITING_TIMEOUT = 120
WAITING_TIMEOUT = 180
chrome_options = Options()
driver_user_agent = ('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36')
Expand All @@ -34,6 +34,11 @@ def print_if_verbose(val):
driver = Chrome("./chromedriver", options=chrome_options)
except Exception:
driver = Chrome("chromedriver.exe", options=chrome_options)
actions = ActionChains(driver)


def click_on_el(el: WebElement):
actions.move_to_element(el).click().perform()


try:
Expand All @@ -43,22 +48,23 @@ def print_if_verbose(val):
EC.element_to_be_clickable((By.CSS_SELECTOR, 'a.header_login_btn'))
)
print_if_verbose('logging in...')
login_btn_el.click()
click_on_el(login_btn_el)
login_email_btn_el = WebDriverWait(driver, WAITING_TIMEOUT).until(
EC.element_to_be_clickable(
(By.CSS_SELECTOR, 'a[data-e2e=email-button]'))
)
login_email_btn_el.click()
driver.find_element_by_css_selector('.sign_in .login_or_email.email input').send_keys(
click_on_el(login_email_btn_el)
driver.find_element(By.CSS_SELECTOR, '.sign_in .login_or_email.email input').send_keys(
args.acc_username)
sleep(2)
driver.find_element_by_css_selector('.sign_in .wrapper__password_input input').send_keys(
driver.find_element(By.CSS_SELECTOR, '.sign_in .wrapper__password_input input').send_keys(
args.acc_password)
sleep(3)
driver.find_element_by_css_selector('.sign_in button[type=submit]').click()
# driver.find_element_by_css_selector('.sign_in button[type=submit]').click()
click_on_el(driver.find_element(By.CSS_SELECTOR, '.sign_in button[type=submit]'))


# driver.find_element_by_css_selector('.sign_in button[type=submit]').click()

def logged_in_check():
_start_time = time()
logged_in = False
Expand All @@ -77,6 +83,8 @@ def logged_in_check():
print("Run script again with '--display' parameter added. Because reCAPTCHA "
"solving needed")
exit(1)


logged_in_check()

books_url_list = args.get_books_url()
Expand All @@ -92,36 +100,36 @@ def logged_in_check():
(By.CSS_SELECTOR, 'div[data-e2e=primary-actions]>a[data-e2e=listen-button]'))
)
listen_btn_link = listen_btn_el.get_attribute('href')
book_name = driver.find_element_by_css_selector('h1[data-e2e=desktop-content-title]').text
book_name = driver.find_element(By.CSS_SELECTOR, 'h1[data-e2e=desktop-content-title]').text
driver.get(listen_btn_link)
chapters_menu_el = WebDriverWait(driver, WAITING_TIMEOUT).until(
EC.element_to_be_clickable(
(By.CSS_SELECTOR, '.header .menu_icon_container a.menu_icon'))
)
chapters_name = []
chapters_audio_url = []
chapters_menu_el.click()
click_on_el(chapters_menu_el)


def get_chapters_list_el():
def get_chapters_list_el() -> list[WebElement]:
_chapters_list_el = []
while not _chapters_list_el:
_chapters_list_el = driver.find_elements_by_css_selector(
'.header .menu_icon_container '
'.button_menu.bottom '
'.button_menu_items_container '
'ul.button_menu_items>li>a')
_chapters_list_el = driver.find_elements(By.CSS_SELECTOR,
'.header .menu_icon_container '
'.button_menu.bottom '
'.button_menu_items_container '
'ul.button_menu_items>li>a')
sleep(.1)
return _chapters_list_el


chapters_list_el = get_chapters_list_el()

for chapter_el in chapters_list_el:
chapters_name.append(chapter_el.find_element_by_css_selector('span.track').text)
chapters_name.append(chapter_el.find_element(By.CSS_SELECTOR, 'span.track').text)
for i, chapter_name in enumerate(chapters_name):
chapters_list_el = get_chapters_list_el()
chapters_list_el[i].click()
click_on_el(chapters_list_el[i])

start_time = time()
added_audio = False
Expand All @@ -134,7 +142,7 @@ def get_chapters_list_el():
chapters_audio_url.append(audio_file_url)
added_audio = True
sleep(.1)
chapters_menu_el.click()
click_on_el(chapters_menu_el)
driver.get(book_url)
print('collected these chapters:')
for i in range(len(chapters_name)):
Expand Down

0 comments on commit ef53709

Please sign in to comment.