From 71682581916e54dd6e993478e2edadb572db32b2 Mon Sep 17 00:00:00 2001
From: Eddy Hintze <eddy@hintze.co>
Date: Sun, 19 Jan 2020 22:31:43 -0500
Subject: [PATCH] Fixed sso & 2fa cookie gen bug Fixed #3 & #8. Added file type
 filtering Fixed #2

---
 CHANGELOG.md                                |  4 ++++
 humblebundle_downloader/cli.py              | 15 ++++++++++++++-
 humblebundle_downloader/download_library.py | 15 ++++++++++++++-
 humblebundle_downloader/generate_cookie.py  |  2 +-
 4 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2dc2e07..aaadd31 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
 # Change log
 
+### 0.0.8
+- gen-cookies now works with SSO feature and 2FA logins
+- Added `--include` & `--exclude` cli args to filter file types
+
 
 ### 0.0.7
 - Replace `:` with ` -` in filenames
diff --git a/humblebundle_downloader/cli.py b/humblebundle_downloader/cli.py
index 08d2f28..01e527a 100644
--- a/humblebundle_downloader/cli.py
+++ b/humblebundle_downloader/cli.py
@@ -53,6 +53,17 @@ def cli():
         action='store_true',
         help="Display progress bar for downloads",
     )
+    filter_ext = parser_download.add_mutually_exclusive_group()
+    filter_ext.add_argument(
+        '-e', '--exclude',
+        type=str, nargs='*',
+        help="File extensions to ignore when downloading files. Ex: -e pdf mobi"
+    )
+    filter_ext.add_argument(
+        '-i', '--include',
+        type=str, nargs='*',
+        help="Only download files with these extensions. Ex: -i pdf mobi"
+    )
 
     cli_args = parser.parse_args()
 
@@ -65,5 +76,7 @@ def cli():
         download_library(
             cli_args.cookie_file,
             cli_args.library_path,
-            progress_bar=cli_args.progress
+            progress_bar=cli_args.progress,
+            ext_include=cli_args.include,
+            ext_exclude=cli_args.exclude
         )
diff --git a/humblebundle_downloader/download_library.py b/humblebundle_downloader/download_library.py
index a4e5f19..993b9bc 100644
--- a/humblebundle_downloader/download_library.py
+++ b/humblebundle_downloader/download_library.py
@@ -17,7 +17,13 @@ def _clean_name(dirty_str):
     return "".join(clean).strip()
 
 
-def download_library(cookie_path, library_path, progress_bar=False):
+def download_library(cookie_path, library_path, progress_bar=False,
+                     ext_include=None, ext_exclude=None):
+    if ext_include is None:
+        ext_include = []
+    if ext_exclude is None:
+        ext_exclude = []
+
     # Load cookies
     with open(cookie_path, 'r') as f:
         account_cookies = f.read()
@@ -66,8 +72,15 @@ def download_library(cookie_path, library_path, progress_bar=False):
                     except KeyError:
                         logger.info("No url found for " + item_title)
                         continue
+
                     ext = url.split('?')[0].split('.')[-1]
                     file_title = item_title + "." + ext
+                    # Only get the file types we care about
+                    if ((ext_include and ext not in ext_include)
+                            or (ext_exclude and ext in ext_exclude)):
+                        logger.info("Skipping the file " + file_title)
+                        continue
+
                     filename = os.path.join(item_folder, file_title)
                     item_r = requests.get(url, stream=True)
                     logger.debug("Item request: {item_r}, Url: {url}"
diff --git a/humblebundle_downloader/generate_cookie.py b/humblebundle_downloader/generate_cookie.py
index 9ef5d5d..5ed8ac9 100644
--- a/humblebundle_downloader/generate_cookie.py
+++ b/humblebundle_downloader/generate_cookie.py
@@ -26,7 +26,7 @@ def generate_cookie(cookie_path):
 
     driver.get('https://www.humblebundle.com/login')
 
-    while '/login' in driver.current_url:
+    while '/home/library' not in driver.current_url:
         # Waiting for the user to login
         time.sleep(.25)