Browse Source

Update 'pluralsight.py'

master
parent
commit
c5615cff29
1 changed files with 64 additions and 22 deletions
  1. 64
    22
      pluralsight.py

+ 64
- 22
pluralsight.py View File

@@ -3,6 +3,7 @@ import os
import random
import re
import secrets
import socket
import string
import sys
import time
@@ -11,6 +12,7 @@ from multiprocessing.pool import Pool
from pathlib import Path
from typing import List, Tuple, Dict

import click
import requests
import youtube_dl
from bs4 import BeautifulSoup
@@ -149,15 +151,15 @@ class Pluralsight:
This class handles the registration and verification of new Pluralsight accounts
"""

def __init__(self, email: str, password: str, is_headless: bool = True, proxy_tuple: Tuple[str, str] = None):
def __init__(self, email: str, password: str, is_headless: bool = True, proxy_tuple: Tuple[str, int] = 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.http_port", proxy_tuple[1])
profile.set_preference("network.proxy.ssl", proxy_tuple[0])
profile.set_preference("network.proxy.ssl_port", int(proxy_tuple[1]))
profile.set_preference("network.proxy.ssl_port", proxy_tuple[1])

if is_headless:
options = Options()
@@ -250,7 +252,7 @@ class Pluralsight:
# endregion


# region Utility Functions
# region Static Utility Functions


def get_name() -> Tuple[str, str]:
@@ -289,7 +291,7 @@ def get_password(min_length: int = 25, max_length: int = 50) -> str:
# endregion


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

@@ -358,11 +360,13 @@ def is_downloaded(course_link: str) -> bool:
return course_link in downloaded_courses_list


# region Downloading related Functions

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

@@ -416,8 +420,7 @@ def download_batch_course(course_links_list: List[str],
username: str,
password: str,
save_directory_path: str,
proxy_tuple: Tuple[str, str] = None) -> bool:

proxy_tuple: Tuple[str, int] = None) -> bool:
download_single_course_function = partial(download_single_course,
username=username,
password=password,
@@ -432,7 +435,7 @@ def download_batch_course(course_links_list: List[str],

def download_pluralsight_path(pluralsight_paths_list: List[PluralsightPath],
path_id: int,
proxy_tuple: Tuple[str, str] = None) -> bool:
proxy_tuple: Tuple[str, int] = None) -> bool:
try:
retry_count = 0
while retry_count < MAX_ACCOUNT_PER_IP:
@@ -457,7 +460,7 @@ def download_pluralsight_path(pluralsight_paths_list: List[PluralsightPath],
return False


def download_independent_courses(course_list: List[str], proxy_tuple:Tuple[str, str] = None) -> bool:
def download_independent_courses(course_list: List[str], proxy_tuple: Tuple[str, int] = None) -> bool:
try:
retry_count = 0
while retry_count < MAX_ACCOUNT_PER_IP:
@@ -477,26 +480,65 @@ def download_independent_courses(course_list: List[str], proxy_tuple:Tuple[str,
print(e)
return False

# endregion


# region Click Callback Functions

def click_validate_proxy(ctx, param, value) -> Tuple[str, int]:
"""
Click callback validation to determine if a given proxy is valid or in correct format (IP:Port)

Args:
ctx: NA
param: NA
value: Callback caller's data

Returns:
Tuple[str, int]: Proxy in a Tuple containing IP as str and port as int
"""

try:
if value is None:
return None

ip, port = value.split(":")

try:
socket.inet_aton(ip)
except socket.error:
raise ValueError

def main():
if not 1 <= int(port) <= 65535:
raise ValueError

return ip, int(port)
except ValueError:
raise click.BadParameter("Proxy needs to be in IP:Port format (example: 127.0.0.1:8000)")

# endregion


@click.command()
@click.option("-i", "--path-id", "path_id", type=int, help="Path ID")
@click.option("-p", "--proxy", "proxy_tuple", default=None, callback=click_validate_proxy, help="Proxy in IP:Port Format")
def main(path_id: int, proxy_tuple: Tuple[str, int]):
try:
paths_list, individual_courses_list = get_paths_and_independent_courses(PATHS_JSON_LOCATION,
INDEPENDENT_COURSES_FILE_LOCATION)

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

if len(sys.argv) == 2:
if path_id is None:
download_independent_courses(individual_courses_list, proxy_tuple)
else:
if 1 <= int(sys.argv[2]) <= len(paths_list):
result = download_pluralsight_path(paths_list, int(sys.argv[2]), proxy_tuple)
if not 1 <= path_id <= len(paths_list):
raise Exception("Path ID is invalid or is out of range")

if result:
print("SUCCESS")
else:
print("ERROR OCCURRED!!")
result = download_pluralsight_path(paths_list, int(sys.argv[2]), proxy_tuple)

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


Loading…
Cancel
Save