From b44039780c37f8c2f12bc975b3a358da3c44d54d Mon Sep 17 00:00:00 2001 From: Adam Saudagar Date: Sun, 18 Oct 2020 06:13:24 +0530 Subject: [PATCH] dynamicly download tesseract if not installed when full auto is launched --- fishy/engine/common/window.py | 4 +-- fishy/engine/fullautofisher/engine.py | 39 ++++++++++++++++++++------- fishy/gui/config_top.py | 7 +++++ fishy/helper/downloader.py | 31 +++++++++++++++++++++ 4 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 fishy/helper/downloader.py diff --git a/fishy/engine/common/window.py b/fishy/engine/common/window.py index 25ba3f3..5583f9e 100644 --- a/fishy/engine/common/window.py +++ b/fishy/engine/common/window.py @@ -47,9 +47,9 @@ class WindowClient: return None if not window_server.screen_ready(): - logging.info("waiting for screen...") + print("waiting for screen...") helper.wait_until(window_server.screen_ready) - logging.info("screen ready, continuing...") + print("screen ready, continuing...") temp_img = WindowServer.Screen diff --git a/fishy/engine/fullautofisher/engine.py b/fishy/engine/fullautofisher/engine.py index 24b6508..871edc5 100644 --- a/fishy/engine/fullautofisher/engine.py +++ b/fishy/engine/fullautofisher/engine.py @@ -1,5 +1,8 @@ import math +import os +import tempfile import uuid +from zipfile import ZipFile import cv2 import logging @@ -18,6 +21,7 @@ from pynput import keyboard, mouse from fishy.helper import hotkey, helper from fishy.helper.config import config +from fishy.helper.downloader import download_file_from_google_drive from fishy.helper.hotkey import Key mse = mouse.Controller() @@ -25,6 +29,26 @@ kb = keyboard.Controller() offset = 0 +def downlaoad_and_extract_tesseract(): + logging.info("Tesseract-OCR downlaoding, Please wait...") + + f = tempfile.NamedTemporaryFile(delete=False) + download_file_from_google_drive("16llzcBlaCsG9fm-rY2dD4Gvopnhm3XoE", f) + f.close() + + logging.info("Tesseract-OCR downloaded, now installing") + + addon_dir = os.path.join(os.environ["APPDATA"], "Tesseract-OCR") + with ZipFile(f.name, 'r') as z: + z.extractall(path=addon_dir) + + logging.info("Tesseract-OCR installed") + + +def is_tesseract_installed(): + return os.path.exists(os.path.join(os.environ["APPDATA"], "Tesseract-OCR")) + + def sign(x): return -1 if x < 0 else 1 @@ -108,24 +132,21 @@ class FullAuto(IEngine): def run(self): logging.info("Loading please wait...") + self.gui.bot_started(True) fishing_event.unsubscribe() self.fisher.toggle_start() - self.controls.change_state() self.window = WindowClient(color=cv2.COLOR_RGB2GRAY, show_name="Full auto debug") if self.crop is None: self.update_crop() self.window.crop = self.crop - self._tesseract_dir = config.get("tesseract_dir", None) - if self._tesseract_dir is None: - logging.warning("Can't start without Tesseract Directory") - self.gui.bot_started(False) - self.toggle_start() - return - - self.gui.bot_started(True) + self._tesseract_dir = os.path.join(os.environ["APPDATA"], "Tesseract-OCR") + if not is_tesseract_installed(): + logging.info("tesseract not found") + downlaoad_and_extract_tesseract() + self.controls.change_state() while self.start and WindowClient.running(): self.window.show(self.show, func=image_pre_process) if not self.show: diff --git a/fishy/gui/config_top.py b/fishy/gui/config_top.py index e2eb3d4..1482a45 100644 --- a/fishy/gui/config_top.py +++ b/fishy/gui/config_top.py @@ -1,4 +1,10 @@ +import logging +import os +import tempfile import typing +from zipfile import ZipFile + +from google.auth.transport import requests from fishy import web from fishy.gui.notification import _give_notification_link @@ -7,6 +13,7 @@ from tkinter import * from tkinter.ttk import * from fishy.helper.config import config +from fishy.helper.downloader import download_file_from_google_drive from fishy.helper.popup import PopUp if typing.TYPE_CHECKING: diff --git a/fishy/helper/downloader.py b/fishy/helper/downloader.py new file mode 100644 index 0000000..5c08fb7 --- /dev/null +++ b/fishy/helper/downloader.py @@ -0,0 +1,31 @@ +import requests + + +def download_file_from_google_drive(id, file): + URL = "https://docs.google.com/uc?export=download" + + session = requests.Session() + + response = session.get(URL, params={'id': id}, stream=True) + token = get_confirm_token(response) + + if token: + params = {'id': id, 'confirm': token} + response = session.get(URL, params=params, stream=True) + + save_response_content(response, file) + + +def get_confirm_token(response): + for key, value in response.cookies.items(): + if key.startswith('download_warning'): + return value + + return None + + +def save_response_content(response, f): + CHUNK_SIZE = 32768 + for chunk in response.iter_content(CHUNK_SIZE): + if chunk: # filter out keep-alive new chunks + f.write(chunk)