CrawlerCodePythonTools 1.5.4
Tools for Python
License: MIT

.. image:: https://img.shields.io/badge/license-MIT-green
   :target: https://pypi.org/project/CrawlerCodePythonTools

CrawlerCodePythonTools - Documentation
======================================

Installation
============

::

    pip install CrawlerCodePythonTools

Config
======

.. code:: python

    from pythontools.core import config

    cfg = config.Config(path="", default_config={"config_data_1": "value_123"})
    cfgData = cfg.getConfig()

    # get config value
    print(cfgData["config_data_1"])

    # chang value and save config
    cfgData["config_data_1"] = "value_456"
    cfg.saveConfig()

Logger
======

.. code:: python

    from pythontools.core import logger

    # path to logs directory (optional)
    logger.init_log("logs")

    # set time format (optional)
    logger.set_time_format(date=True, time=True)

    # print color test
    logger.log("Â§1Blue Â§9Light-Blue Â§3Cyan Â§bLight-Cyan Â§4Red Â§cLight-Red Â§6Yellow Â§eLight-Yellow Â§2Green Â§aLight-Green Â§5Magenta Â§dLight-Magenta Â§fWhite Â§7Light-Gray Â§8Gray Â§0Black")


CronJob
=======

.. code:: python

    from pythontools.core import cronjob, logger
..

    Interval-based CronJob

.. code:: python

    def cron_interval_based():
        logger.log("Interval-based CronJob done.")

    # register CronJob
    cronjob.register_cron_job(cronjob.CronJob("cron_interval_based", 10, cron_interval_based))
..

    Time-based CronJob

.. code:: python

    def cron_time_based():
        logger.log("Time-based CronJob done.")

    # register CronJob
    #cronjob.register_cron_job(cronjob.CronJob("cron_time_based", "20", cron_time_based))  # 20 => 20h
    cronjob.register_cron_job(cronjob.CronJob("cron_time_based", "20:10", cron_time_based))  # 20:10 => 20h 10m
    #cronjob.register_cron_job(cronjob.CronJob("cron_time_based", "20:10:30", cron_time_based))  # 20:10:30 => 20h 10m 30s
..

    Weekday-based CronJob

.. code:: python

    def cron_weekday_based():
        logger.log("today is friday!")
        logger.log("Weekday-based CronJob done.")

    # register CronJob
    cronjob.register_cron_job(cronjob.CronJob("cron_weekday_based", "friday", cron_weekday_based))
..

Server and Client (sockets)
===========================

    Server

.. code:: python

    from pythontools.sockets import server
    from threading import Thread

    SERVER = server.Server(password="PASSWORD")

    def ON_CLIENT_CONNECT(client):
        # send a message to client on connect by clientSocket
        SERVER.sendTo(client["clientSocket"], {"METHOD": "HELLO"})

    def ON_CLIENT_DISCONNECT(client):
        pass

    def ON_RECEIVE(client, data):
        METHOD = data["METHOD"]

    SERVER.ON_CLIENT_CONNECT(ON_CLIENT_CONNECT)
    SERVER.ON_CLIENT_DISCONNECT(ON_CLIENT_DISCONNECT)
    SERVER.ON_RECEIVE(ON_RECEIVE)

    Thread(target=SERVER.start, args=["HOST-IP", 15749]).start()

    # send a message to client by clientID
    SERVER.sendToClient("MY_CLIENT_ID", {"METHOD": "TEST", "mydata": "123"})
..

    Client

.. code:: python

    from pythontools.sockets import client
    from threading import Thread

    CLIENT = client.Client(password="PASSWORD", clientID="MY_CLIENT_ID", clientType="CLIENT")

    def ON_CONNECT():
        pass

    def ON_RECEIVE(data):
        METHOD = data["METHOD"]
        # recipe the test message
        if METHOD == "TEST":
            print("test:", data["mydata"])

    CLIENT.ON_CONNECT(ON_CONNECT)
    CLIENT.ON_RECEIVE(ON_RECEIVE)

    Thread(target=CLIENT.connect, args=["HOST-IP", 15749]).start()


WebBot
===========

    Download chromedriver or geckodriver
        - https://chromedriver.chromium.org/downloads
        - https://github.com/mozilla/geckodriver/releases

.. code:: python

    from pythontools.webbot import webbot

    # Google Chrome
    browser = webbot.WebBot().Chrome(chromedriver="chromedriver.exe")
    # Firefox
    browser = webbot.WebBot().Firefox(geckodriver="geckodriver.exe")
    browser.get("https://www.google.com/")

    browser.input('//*[@id="tsf"]/div[2]/div[1]/div[1]/div/div[2]/input', "what is python?")
    browser.click('//*[@id="tsf"]/div[2]/div[1]/div[2]/div[2]/div[2]/center/input[1]')

    time.sleep(10)

    browser.close()