Browse Source

Update 'pluralsight.py'

master
parent
commit
5240c6859f
1 changed files with 58 additions and 23 deletions
  1. 58
    23
      pluralsight.py

+ 58
- 23
pluralsight.py View File

@@ -15,6 +15,7 @@ import requests
import youtube_dl
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver import FirefoxProfile
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support import expected_conditions
@@ -26,7 +27,7 @@ from selenium.webdriver.support.wait import WebDriverWait
# True/False to determine whether selenium instances will be visible or not (headless)
HIDE_SELENIUM_INSTANCES = False

# Denotes Time.Sleep() duration in seconds
# Denotes Maximum seconds selenium will wait for an element to load
SELENIUM_TIMEOUT_IN_SECONDS = 30

# Maximum parallel course download count
@@ -45,7 +46,7 @@ IS_DELAY_BETWEEN_VIDEO = True
MIN_VIDEO_DOWNLOAD_DELAY = 10

# Minimum number of seconds to wait between consecutive video downloads
MAX_VIDEO_DOWNLOAD_DELAY = 30
MAX_VIDEO_DOWNLOAD_DELAY = 20

# Check if current OS/platform is Windows
IS_WINDOWS = sys.platform.startswith("win")
@@ -148,13 +149,22 @@ class Pluralsight:
This class handles the registration and verification of new Pluralsight accounts
"""

def __init__(self, email: str, password: str, is_headless: bool = True):
def __init__(self, email: str, password: str, is_headless: bool = True, proxy_tuple: Tuple[str, str] = None):
profile = FirefoxProfile()

if proxy_tuple is not None:
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", proxy_tuple[0])
profile.set_preference("network.proxy.http_port", int(proxy_tuple[1]))
profile.set_preference("network.proxy.ssl", proxy_tuple[0])
profile.set_preference("network.proxy.ssl_port", int(proxy_tuple[1]))

if is_headless:
options = Options()
options.add_argument("--headless")
self.driver = webdriver.Firefox(options=options)
self.driver = webdriver.Firefox(options=options, firefox_profile=profile)
else:
self.driver = webdriver.Firefox()
self.driver = webdriver.Firefox(firefox_profile=profile)

self.email = email
self.password = password
@@ -279,7 +289,7 @@ def get_password(min_length: int = 25, max_length: int = 50) -> str:
# endregion


def create_pluralsight_account() -> Dict[str, str]:
def create_pluralsight_account(proxy_tuple: Tuple[str, str] = None) -> Dict[str, str]:
"""
Creates new Pluralsight account and returns the email/password as a dictionary

@@ -292,7 +302,8 @@ def create_pluralsight_account() -> Dict[str, str]:

with Pluralsight(email=disposable_email.email_address,
password=password,
is_headless=HIDE_SELENIUM_INSTANCES) as ps:
is_headless=HIDE_SELENIUM_INSTANCES,
proxy_tuple=proxy_tuple) as ps:
ps.register()

verification_link = disposable_email.get_verification_link()
@@ -311,6 +322,9 @@ def get_paths_and_independent_courses(paths_file: str, courses_file: str) -> Tup
pluralsight_paths_dicts_list = json.loads(json_string)
pluralsight_paths_list = [PluralsightPath(**paths_dict) for paths_dict in pluralsight_paths_dicts_list]

if not os.path.isfile(courses_file):
return pluralsight_paths_list, []

if courses_file.startswith("http"):
courses_list_string = requests.get(courses_file).text
else:
@@ -344,7 +358,11 @@ def is_downloaded(course_link: str) -> bool:
return course_link in downloaded_courses_list


def download_single_course(course_link: str, username: str, password: str, save_directory_path: str) -> bool:
def download_single_course(course_link: str,
username: str,
password: str,
save_directory_path: str,
proxy_tuple: Tuple[str, str] = None) -> bool:
"""
Download the given course using the provided credential

@@ -353,6 +371,7 @@ def download_single_course(course_link: str, username: str, password: str, save_
username: Username (Email) of the Pluralsight account to be used for download
password: Password of the Pluralsight account to be used for download
save_directory_path: Absolute path of Root save directory
proxy_tuple: Proxy in (IP, Port) Tuple format

Returns: True/False bool value denoting the success status of the download
"""
@@ -366,6 +385,9 @@ def download_single_course(course_link: str, username: str, password: str, save_
ydl_options['username'] = username
ydl_options['password'] = password

if proxy_tuple is not None:
ydl_options['proxy'] = f"https://{proxy_tuple[0]}:{proxy_tuple[1]}"

if IS_WINDOWS:
ydl_options[
'outtmpl'] = f"{save_directory_path}\\%(playlist)s\\%(chapter_number)s - %(chapter)s\\%(playlist_index)s - %(title)s.%(ext)s"
@@ -390,10 +412,16 @@ def download_single_course(course_link: str, username: str, password: str, save_
return False


def download_batch_course(course_links_list: List[str], username: str, password: str, save_directory_path: str) -> bool:
def download_batch_course(course_links_list: List[str],
username: str,
password: str,
save_directory_path: str,
proxy_tuple: Tuple[str, str] = None) -> bool:

download_single_course_function = partial(download_single_course,
username=username,
password=password,
proxy_tuple=proxy_tuple,
save_directory_path=save_directory_path)

with Pool(MAX_PARALLEL_COURSE_DOWNLOAD_COUNT) as p:
@@ -402,11 +430,13 @@ def download_batch_course(course_links_list: List[str], username: str, password:
return False not in download_results


def download_pluralsight_path(pluralsight_paths_list: List[PluralsightPath], path_id: int) -> bool:
def download_pluralsight_path(pluralsight_paths_list: List[PluralsightPath],
path_id: int,
proxy_tuple: Tuple[str, str] = None) -> bool:
try:
retry_count = 0
while retry_count < MAX_ACCOUNT_PER_IP:
credential_dict = create_pluralsight_account()
credential_dict = create_pluralsight_account(proxy_tuple)

for pluralsight_path in pluralsight_paths_list:
if pluralsight_path.id == path_id:
@@ -415,7 +445,8 @@ def download_pluralsight_path(pluralsight_paths_list: List[PluralsightPath], pat
result = download_batch_course(course_links_list=pluralsight_path.course_links,
username=credential_dict['email'],
password=credential_dict['password'],
save_directory_path=save_directory_path)
save_directory_path=save_directory_path,
proxy_tuple=proxy_tuple)

if result:
return True
@@ -426,7 +457,7 @@ def download_pluralsight_path(pluralsight_paths_list: List[PluralsightPath], pat
return False


def download_independent_courses(course_list: List[str]) -> bool:
def download_independent_courses(course_list: List[str], proxy_tuple:Tuple[str, str] = None) -> bool:
try:
retry_count = 0
while retry_count < MAX_ACCOUNT_PER_IP:
@@ -435,7 +466,8 @@ def download_independent_courses(course_list: List[str]) -> bool:
result = download_batch_course(course_links_list=course_list,
username=credential_dict['email'],
password=credential_dict['password'],
save_directory_path=SAVE_COURSES_DIRECTORY_PATH)
save_directory_path=SAVE_COURSES_DIRECTORY_PATH,
proxy_tuple=proxy_tuple)

if result:
return True
@@ -451,17 +483,20 @@ def main():
paths_list, individual_courses_list = get_paths_and_independent_courses(PATHS_JSON_LOCATION,
INDEPENDENT_COURSES_FILE_LOCATION)

if len(sys.argv) == 1:
download_independent_courses(individual_courses_list)
proxy_tuple = None
if len(sys.argv[1].split(":")) == 2:
proxy_tuple = sys.argv[1].split(":")

if len(sys.argv) == 2:
download_independent_courses(individual_courses_list, proxy_tuple)
else:
for arg in sys.argv[1:]:
if 1 <= int(arg) <= len(paths_list):
result = download_pluralsight_path(paths_list, int(arg))
if 1 <= int(sys.argv[2]) <= len(paths_list):
result = download_pluralsight_path(paths_list, int(sys.argv[2]), proxy_tuple)

if result:
print("SUCCESS")
else:
print("ERROR OCCURRED!!")
if result:
print("SUCCESS")
else:
print("ERROR OCCURRED!!")
except Exception as exception:
print(exception)


Loading…
Cancel
Save