Skip to content

Commit

Permalink
Merge pull request #85 from zazer0/feat/add-login-saving
Browse files Browse the repository at this point in the history
Implement login caching for Chrome
  • Loading branch information
soraxas authored Sep 16, 2024
2 parents afba7d6 + 401c367 commit 4aee542
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ target/
# appp specific folders
bin/
default_out_path/
_browser_user_data_dir/
38 changes: 34 additions & 4 deletions echo360/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from .course import EchoCloudCourse
from .echo_exceptions import EchoLoginError
from .utils import naive_versiontuple
from .utils import naive_versiontuple, PERSISTENT_SESSION_FOLDER


from pick import pick
Expand All @@ -22,13 +22,21 @@


def build_chrome_driver(
use_local_binary, selenium_version_ge_4100, setup_credential, user_agent, log_path
use_local_binary,
selenium_version_ge_4100,
setup_credential,
user_agent,
log_path,
persistent_session,
):
from selenium.webdriver.chrome.options import Options

opts = Options()
if not setup_credential:
opts.add_argument("--headless")
if persistent_session:
folder_path = PERSISTENT_SESSION_FOLDER # default current dir
opts.add_argument("--user-data-dir={}".format(folder_path))
opts.add_argument("--window-size=1920x1080")
opts.add_argument("user-agent={}".format(user_agent))

Expand Down Expand Up @@ -62,8 +70,18 @@ def build_chrome_driver(


def build_firefox_driver(
use_local_binary, selenium_version_ge_4100, setup_credential, user_agent, log_path
use_local_binary,
selenium_version_ge_4100,
setup_credential,
user_agent,
log_path,
persistent_session,
):
if persistent_session:
raise NotImplementedError(
"Save-login not implemented for Firefox! Feel free to make a PR for it..."
)

profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", user_agent)
kwargs = dict()
Expand Down Expand Up @@ -95,8 +113,18 @@ def build_firefox_driver(


def build_phantomjs_driver(
use_local_binary, selenium_version_ge_4100, setup_credential, user_agent, log_path
use_local_binary,
selenium_version_ge_4100,
setup_credential,
user_agent,
log_path,
persistent_session,
):
if persistent_session:
raise NotImplementedError(
"Save-login not implemented for Firefox! Feel free to make a PR for it..."
)

dcap = dict()
dcap.update(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = (
Expand Down Expand Up @@ -128,6 +156,7 @@ def __init__(
use_local_binary=False,
webdriver_to_use="phantomjs",
interactive_mode=False,
persistent_session=False,
):
self._course = course
root_path = os.path.dirname(os.path.abspath(sys.modules["__main__"].__file__))
Expand Down Expand Up @@ -175,6 +204,7 @@ def __init__(
setup_credential=setup_credential,
user_agent=self._useragent,
log_path=log_path,
persistent_session=persistent_session,
)

self.setup_credential = setup_credential
Expand Down
15 changes: 15 additions & 0 deletions echo360/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from .echo_exceptions import EchoLoginError
from .downloader import EchoDownloader
from .course import EchoCourse, EchoCloudCourse
from .utils import PERSISTENT_SESSION_FOLDER

_DEFAULT_BEFORE_DATE = datetime(2900, 1, 1).date()
_DEFAULT_AFTER_DATE = datetime(1100, 1, 1).date()
Expand Down Expand Up @@ -94,6 +95,17 @@ def handle_args():
any website to obtain credentials needed before proceeding. \
(implies using chrome-driver)",
)
parser.add_argument(
"--persistent-session",
"-P",
action="store_true",
default=False,
dest="persistent_session",
help="Starts a persistent session (helps to store credentials). Session uses \
'{}' folder, and currently only supports chrome driver.".format(
PERSISTENT_SESSION_FOLDER
),
)
parser.add_argument(
"--download-phantomjs-binary",
action="store_true",
Expand Down Expand Up @@ -231,6 +243,7 @@ def handle_args():
not args["auto"],
args["alternative_feeds"],
args["echo360cloud"],
args["persistent_session"],
)


Expand All @@ -251,6 +264,7 @@ def main():
manual,
alternative_feeds,
usingEcho360Cloud,
persistent_session,
) = handle_args()

setup_logging(enable_degbug)
Expand Down Expand Up @@ -344,6 +358,7 @@ def cmd_exists(x):
use_local_binary=use_local_binary,
webdriver_to_use=webdriver_to_use,
interactive_mode=interactive_mode,
persistent_session=persistent_session,
)

_LOGGER.debug(
Expand Down
3 changes: 3 additions & 0 deletions echo360/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ def naive_versiontuple(v):
Expects naive_versiontuple('xx.yy.zz') < naive_versiontuple('aa.bb.cc').
"""
return tuple(map(int, (v.split("."))))


PERSISTENT_SESSION_FOLDER = "_browser_persistent_session"

0 comments on commit 4aee542

Please sign in to comment.