From 069be28598c12b567616aeefa4e42cea160b53d2 Mon Sep 17 00:00:00 2001 From: David Wicker Date: Wed, 21 Oct 2020 15:54:44 +0200 Subject: [PATCH] Configure Web Server Compatibility Update to v1.4 --- instaclient/client/instaclient.py | 37 ++++++++++++++++++------------- setup.py | 4 ++-- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/instaclient/client/instaclient.py b/instaclient/client/instaclient.py index 531f551..05ac519 100644 --- a/instaclient/client/instaclient.py +++ b/instaclient/client/instaclient.py @@ -5,7 +5,7 @@ from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import NoSuchElementException, TimeoutException -import time +import time, os from instaclient.utilities.utilities import * from instaclient.errors import * @@ -15,31 +15,38 @@ class InstaClient: CHROMEDRIVER=1 - def __init__(self, username: str=None, password: str=None, driver: int=CHROMEDRIVER): + LOCAHOST=1 + WEB_SERVER=2 + def __init__(self, driver_type: int=CHROMEDRIVER, host=None): """ Creates an instance of instaclient class. Args: - You can omit the username and password if you prefer to insert them in the client.login() method: - username:str: The username of the user - password:str: The password of the user - - driver_location:str: Path of the driver file (must be in the root folder of your project) - wait_time:int: (Seconds) time to wait before raising driver exception + driver_type:int: + InstaClient.CHROMEDRIVER for a Chrome instance + host:int: + InstaClient.LOCALHOST for testing / running locally + InstaClient.WEB_SERVER for production / running on a web server Attributes: driver: Instance of the Selenium Webdriver (defaults to Chrome) logged_in:bool: Boolean whether current user is logged in or not. Defaults to False """ - - self.username = username - self.password = password - try: - if driver == self.CHROMEDRIVER: - self.driver = webdriver.Chrome('instaclient/drivers/chromedriver.exe') + if driver_type == self.CHROMEDRIVER: + if host in (None, self.LOCAHOST): + # Running on web server + chrome_options = webdriver.ChromeOptions() + chrome_options.binary_location = os.environ.get("GOOGLE_CHROME_BIN") + chrome_options.add_argument("--headless") + chrome_options.add_argument("--disable-dev-shm-usage") + chrome_options.add_argument("--no-sandbox") + self.driver = webdriver.Chrome(executable_path=os.environ.get("CHROMEDRIVER_PATH"), chrome_options=chrome_options) + else: + # Running locally + self.driver = webdriver.Chrome('instaclient/drivers/chromedriver.exe') else: - raise InvaildDriverError(driver) + raise InvaildDriverError(driver_type) self.driver.maximize_window() except Exception as error: raise error diff --git a/setup.py b/setup.py index 34befd2..0320211 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name = 'instaclient', # How you named your package folder (MyLib) packages = find_packages(exclude=['tests']), # Chose the same as "name" - version = '1.3', # Start with a small number and increase it with every change you make + version = '1.4', # Start with a small number and increase it with every change you make license='MIT', # Chose a license from here: https://help.github.com/articles/licensing-a-repository description = 'Instagram client built with Python 3.8 and the Selenium package.', long_description=README, @@ -19,7 +19,7 @@ author = 'David Wicker', # Type in your name author_email = 'davidwickerhf@gmail.com', # Type in your E-Mail url = 'https://github.com/wickerdevs/py-instaclient', # Provide either the link to your github or to your website - download_url = 'https://github.com/wickerdevs/py-instaclient/archive/v1.3.tar.gz', # I explain this later on + download_url = 'https://github.com/wickerdevs/py-instaclient/archive/v1.4.tar.gz', # I explain this later on keywords = ['INSTAGRAM', 'BOT', 'INSTAGRAM BOT', 'INSTAGRAM CLIENT'], # Keywords that define your package best install_requires=[ # I get to this in a second 'selenium',