Skip to content

Commit

Permalink
Fixing bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
SantiiRepair committed Sep 19, 2023
1 parent 526b32b commit 1e8e3a8
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 113 deletions.
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ RUN pip3 install -U pip
RUN pip3 install --upgrade pip
COPY . /kreacher/
WORKDIR /kreacher/
RUN make install
CMD ["bash","start.sh"]
RUN pip3 install --no-deps -U pytgcalls==3.0.0.dev24 tgcalls==3.0.0.dev6 && pip3 install -r requirements.txt
CMD ["bash","scripts/setup.sh"]
CMD ["bash","scripts/start.sh"]
10 changes: 1 addition & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
# create virtual enviroment ro run the bot
create_env:
virtualenv kreacher_env

# build in docker container
docker_build:
docker build . -t kreacher
Expand All @@ -20,7 +16,7 @@ format:

# lint all project looking for issues
lint:
pylint --disable=C0301,C0103 --recursive yes --jobs=4 .
pylint --recursive yes --jobs=4 .

# generate string session of your telegram account
session_string:
Expand All @@ -30,7 +26,3 @@ session_string:
run_bot:
python3 -m bot

# command to run bot in virtual enviroment
run_virtual env:
make_create env && make install && source kreacher_env/bin/activate && bash python3 -m kreacher

50 changes: 0 additions & 50 deletions bot/helpers/main.py

This file was deleted.

7 changes: 6 additions & 1 deletion bot/plugins/commands/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from bot.helpers.progress import progress
from bot.dbs.instances import VOICE_CHATS
from bot import assistant, kreacher, on_call
from bot.scrapers.images import ImageScraper
from bot.decorators.only_grps_chnns import only_grps_chnns
from bot.helpers.queues import (
clear_queue,
Expand Down Expand Up @@ -70,7 +71,11 @@ async def _(client: Client, message: Message):
await msg.edit(
f"**__Yeehaw, I found the {media['type']} you asked for...__**"
)
await sleep(2)
await sleep(1)
image_scraper = ImageScraper(search_key=f"{media['caption']} poster")
image_urls = image_scraper.find_image_urls()
image_scraper.save_images(image_urls)
await sleep(1)
await msg.edit("\U0001f4be **__Downloading...__**")
if media["type"] == "serie":
video = await assistant.download_media(
Expand Down
63 changes: 12 additions & 51 deletions bot/helpers/image_searcher.py → bot/scrapers/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException

# import helper libraries
import time
import urllib.request
from urllib.parse import urlparse
Expand All @@ -14,65 +12,28 @@
import io
from PIL import Image
import re

# custom patch libraries
import patch
from bot.utils.driver import get_driver


class GoogleImageScraper:
class ImageScraper:
def __init__(
self,
webdriver_path,
image_path,
search_key="cat",
search_key="kreacher",
number_of_images=1,
headless=True,
min_resolution=(0, 0),
max_resolution=(1920, 1080),
max_missed=10,
driver=get_driver,
):
# check parameter types
image_path = os.path.join(image_path, search_key)
if type(number_of_images) != int:
print("[Error] Number of images must be integer value.")
if not isinstance(number_of_images, int):
print("[Error]: Number of images must be integer value.")
return
if not os.path.exists(image_path):
print("[INFO] Image path not found. Creating a new folder.")
os.makedirs(image_path)

# check if chromedriver is installed
if not os.path.isfile(webdriver_path):
is_patched = patch.download_lastest_chromedriver()
if not is_patched:
exit(
"[ERR] Please update the chromedriver.exe in the webdriver folder according to your chrome version:https://chromedriver.chromium.org/downloads"
)

for i in range(1):
try:
# try going to www.google.com
options = Options()
if headless:
options.add_argument("--headless")
driver = webdriver.Chrome(webdriver_path, chrome_options=options)
driver.set_window_size(1400, 1050)
driver.get("https://www.google.com")
try:
WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.ID, "W0wltc"))
).click()
except Exception as e:
continue
except Exception as e:
# update chromedriver
pattern = "(\d+\.\d+\.\d+\.\d+)"
version = list(set(re.findall(pattern, str(e))))[0]
is_patched = patch.download_lastest_chromedriver(version)
if not is_patched:
exit(
"[ERR] Please update the chromedriver.exe in the webdriver folder according to your chrome version:https://chromedriver.chromium.org/downloads"
)

self.driver = driver
self.search_key = search_key
self.number_of_images = number_of_images
Expand All @@ -95,7 +56,7 @@ def find_image_urls(self):
image_urls = google_image_scraper.find_image_urls()
"""
print("[INFO] Gathering image links")
print("[INFO]: Gathering image links")
self.driver.get(self.url)
image_urls = []
count = 0
Expand Down Expand Up @@ -177,13 +138,13 @@ def find_image_urls(self):
)
element = self.driver.find_element(By.CLASS_NAME, "mye4qd")
element.click()
print("[INFO] Loading next page")
print("[INFO]: Loading next page")
time.sleep(3)
except Exception:
time.sleep(1)

self.driver.quit()
print("[INFO] Google search ended")
print("[INFO]: Google search ended")
return image_urls

def save_images(self, image_urls, keep_filenames):
Expand All @@ -197,7 +158,7 @@ def save_images(self, image_urls, keep_filenames):
google_image_scraper.save_images(image_urls)
"""
print("[INFO] Saving image, please wait...")
print("[INFO]: Saving image, please wait...")
for indx, image_url in enumerate(image_urls):
try:
print("[INFO] Image url:%s" % (image_url))
Expand Down Expand Up @@ -225,7 +186,7 @@ def save_images(self, image_urls, keep_filenames):

image_path = os.path.join(self.image_path, filename)
print(
f"[INFO] {self.search_key} \t {indx} \t Image saved at: {image_path}"
f"[INFO]: {self.search_key} \t {indx} \t Image saved at: {image_path}"
)
image_from_web.save(image_path)
except OSError:
Expand All @@ -248,5 +209,5 @@ def save_images(self, image_urls, keep_filenames):
pass
print("--------------------------------------------------")
print(
"[INFO] Downloads completed. Please note that some photos were not downloaded as they were not in the correct format (e.g. jpg, jpeg, png)"
"[INFO]: Downloads completed. Please note that some photos were not downloaded as they were not in the correct format (e.g. jpg, jpeg, png)"
)
19 changes: 19 additions & 0 deletions bot/utils/driver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import shutup
import platform
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

shutup.please()


def get_driver():
chrome_options = Options()
if platform.system() == "Linux":
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--window-size=1400,2100")
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-notifications")
driver = webdriver.Chrome(options=chrome_options)
return driver
5 changes: 5 additions & 0 deletions scripts/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome-stable_current_amd64.deb; sudo apt-get -fy install
rm -rf google-chrome-stable_current_amd64.deb; sudo apt-get update
File renamed without changes.

0 comments on commit 1e8e3a8

Please sign in to comment.