From 7400db1700cc7a1acbcad5f41da84b64ae377d4c Mon Sep 17 00:00:00 2001 From: Santiago Ramirez Date: Tue, 19 Dec 2023 13:54:42 +0000 Subject: [PATCH] first step migrating to golang --- .gitignore | 8 +- .vscode/extensions.json | 3 +- Dockerfile | 4 +- bot/__init__.py | 115 -------------------- bot/config.py | 31 ------ go.mod | 11 ++ {bot => kreacher}/__main__.py | 0 {bot => kreacher}/callbacks/config.py | 0 {bot => kreacher}/callbacks/misc.py | 0 {bot => kreacher}/callbacks/streams.py | 0 kreacher/clients.go | 20 ++++ {bot => kreacher}/commands/actives.py | 0 {bot => kreacher}/commands/config.py | 0 {bot => kreacher}/commands/help.py | 0 {bot => kreacher}/commands/join.py | 0 {bot => kreacher}/commands/leave.py | 0 {bot => kreacher}/commands/logs.py | 0 {bot => kreacher}/commands/ping.py | 0 {bot => kreacher}/commands/play_book.py | 0 {bot => kreacher}/commands/play_song.py | 0 {bot => kreacher}/commands/play_video.py | 0 {bot => kreacher}/commands/speedtest.py | 0 {bot => kreacher}/commands/start.py | 0 {bot => kreacher}/commands/streaming.py | 0 kreacher/config.go | 57 ++++++++++ {bot => kreacher}/db/tables.py | 0 {bot => kreacher}/decorators/permissions.py | 0 {bot => kreacher}/decorators/sides.py | 0 {bot => kreacher}/helpers/bash.py | 0 {bot => kreacher}/helpers/db.py | 0 {bot => kreacher}/helpers/nums.py | 0 {bot => kreacher}/helpers/progress.py | 0 {bot => kreacher}/helpers/queues.py | 0 {bot => kreacher}/helpers/translator.py | 0 {bot => kreacher}/helpers/tts.py | 0 {bot => kreacher}/helpers/user_info.py | 0 {bot => kreacher}/helpers/yt.py | 0 {bot => kreacher}/scrapers/images.py | 0 {bot => kreacher}/setup.py | 0 {bot => kreacher}/tasks/on_joined.py | 0 {bot => kreacher}/tasks/vcs.py | 0 {bot => kreacher}/utils/driver.py | 0 {bot => kreacher}/utils/piper.py | 0 43 files changed, 94 insertions(+), 155 deletions(-) delete mode 100755 bot/__init__.py delete mode 100755 bot/config.py create mode 100644 go.mod rename {bot => kreacher}/__main__.py (100%) rename {bot => kreacher}/callbacks/config.py (100%) rename {bot => kreacher}/callbacks/misc.py (100%) rename {bot => kreacher}/callbacks/streams.py (100%) create mode 100755 kreacher/clients.go rename {bot => kreacher}/commands/actives.py (100%) rename {bot => kreacher}/commands/config.py (100%) rename {bot => kreacher}/commands/help.py (100%) rename {bot => kreacher}/commands/join.py (100%) rename {bot => kreacher}/commands/leave.py (100%) rename {bot => kreacher}/commands/logs.py (100%) rename {bot => kreacher}/commands/ping.py (100%) rename {bot => kreacher}/commands/play_book.py (100%) rename {bot => kreacher}/commands/play_song.py (100%) rename {bot => kreacher}/commands/play_video.py (100%) rename {bot => kreacher}/commands/speedtest.py (100%) rename {bot => kreacher}/commands/start.py (100%) rename {bot => kreacher}/commands/streaming.py (100%) create mode 100755 kreacher/config.go rename {bot => kreacher}/db/tables.py (100%) rename {bot => kreacher}/decorators/permissions.py (100%) rename {bot => kreacher}/decorators/sides.py (100%) rename {bot => kreacher}/helpers/bash.py (100%) rename {bot => kreacher}/helpers/db.py (100%) rename {bot => kreacher}/helpers/nums.py (100%) rename {bot => kreacher}/helpers/progress.py (100%) rename {bot => kreacher}/helpers/queues.py (100%) rename {bot => kreacher}/helpers/translator.py (100%) rename {bot => kreacher}/helpers/tts.py (100%) rename {bot => kreacher}/helpers/user_info.py (100%) rename {bot => kreacher}/helpers/yt.py (100%) rename {bot => kreacher}/scrapers/images.py (100%) rename {bot => kreacher}/setup.py (100%) rename {bot => kreacher}/tasks/on_joined.py (100%) rename {bot => kreacher}/tasks/vcs.py (100%) rename {bot => kreacher}/utils/driver.py (100%) rename {bot => kreacher}/utils/piper.py (100%) diff --git a/.gitignore b/.gitignore index ed1eed9..4ae96b3 100755 --- a/.gitignore +++ b/.gitignore @@ -1,14 +1,10 @@ *.env *.log *.rdb -*.pyc +*.sum *.lock *.session *.session-journal .codesandbox -.devcontainer - - - -.devcontainer +.devcontainer \ No newline at end of file diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 7203cb3..915544b 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -1,5 +1,6 @@ { "recommendations": [ - "golang.go" + "golang.go", + "ms-azuretools.vscode-docker" ] } \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index e26abd1..b715ed1 100755 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ -FROM python:3.11.6 +FROM golang:1.21.5 RUN apt-get update && apt-get upgrade -y RUN apt-get install ffmpeg tree -y -RUN pip install --upgrade pip +RUN go mod tidy COPY . /kreacher/ WORKDIR /kreacher/ RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb diff --git a/bot/__init__.py b/bot/__init__.py deleted file mode 100755 index 0fa438f..0000000 --- a/bot/__init__.py +++ /dev/null @@ -1,115 +0,0 @@ -import os -import logging -from redis import Redis -import sqlalchemy as db -from pyrogram import Client -from termcolor import colored -from datetime import datetime -from pytgcalls import GroupCallFactory - -from bot.config import config -from bot.utils.driver import get_driver - -# ------------------------------------------------------------------------------ - -VOICE_CHATS = {} - -# ------------------------------------------------------------------------------ - -_API_ID = config.API_ID -_API_HASH = config.API_HASH -_BOT_TOKEN = config.BOT_TOKEN -_SESSION_STRING = config.SESSION_STRING -_POSTGRES_DB = config.POSTGRES_DB -_POSTGRES_USER = config.POSTGRES_USER -_POSTGRES_PASSWORD = config.POSTGRES_PASSWORD -_POSTGRES_HOST = config.POSTGRES_HOST -_POSTGRES_PORT = config.POSTGRES_PORT -_REDIS_HOST = config.REDIS_HOST -_REDIS_PORT = config.REDIS_PORT -_REDIS_PASSWORD = config.REDIS_PASSWORD - -# ------------------------------------------------------------------------------ - -_log_file = os.path.join("/tmp", "kreacher.log") -if os.path.exists(_log_file) and os.stat(_log_file).st_size > 0: - with open(_log_file, "w") as f: - f.truncate(0) - print(f'{colored("[INFO]:", "blue")}: LOG FILE WAS FLUSHED SUCCESSFULLY') -elif not os.path.exists(_log_file): - try: - with open(_log_file, "w") as f: - f.write("") - print(f'{colored("[INFO]:", "blue")} LOG FILE CREATED') - except Exception as e: - logging.error(e) - -# ------------------------------------------------------------------------------ - -logging.basicConfig( - filename=_log_file, - format="[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s", - level=logging.INFO, -) - -# ------------------------------------------------------------------------------ - -kreacher = Client( - "bot.kreacher", - api_id=_API_ID, - api_hash=_API_HASH, - bot_token=_BOT_TOKEN, -) - -userbot = Client( - "userbot.assistant", - api_id=_API_ID, - api_hash=_API_HASH, - session_string=_SESSION_STRING, -) - -# ------------------------------------------------------------------------------ -""" -engine = db.create_engine( - f"postgresql://{_POSTGRES_USER}:{_POSTGRES_PASSWORD}@{_POSTGRES_HOST}:{_POSTGRES_PORT}/{_POSTGRES_DB}" -) -conn = engine.connect() -db_metadata = db.MetaData() -""" -# ------------------------------------------------------------------------------ - -r = Redis( - host=_REDIS_HOST, - port=_REDIS_PORT, - password=_REDIS_PASSWORD, -) - -# ------------------------------------------------------------------------------ - -tgcalls = GroupCallFactory(userbot, GroupCallFactory.MTPROTO_CLIENT_TYPE.PYROGRAM) - -# ------------------------------------------------------------------------------ - -kreacher.start() -userbot.start() -driver = get_driver() - -START_TIME = datetime.utcnow() -TIME_DURATION_UNITS = ( - ("week", 60 * 60 * 24 * 7), - ("day", 60 * 60 * 24), - ("hour", 60 * 60), - ("min", 60), - ("sec", 1), -) - - -async def execution_time(seconds): - if seconds == 0: - return "inf" - parts = [] - for unit, div in TIME_DURATION_UNITS: - amount, seconds = divmod(int(seconds), div) - if amount > 0: - parts.append("{}{}{}".format(amount, unit, "" if amount == 1 else "s")) - return ", ".join(parts) diff --git a/bot/config.py b/bot/config.py deleted file mode 100755 index 0d47779..0000000 --- a/bot/config.py +++ /dev/null @@ -1,31 +0,0 @@ -import os -from dotenv import load_dotenv - - -class Config(object): - def __init__(self): - cwd = os.path.dirname(os.path.abspath(__file__)) - path = os.path.join(cwd, "../.env") - load_dotenv(path) - - self.API_ID = os.getenv("API_ID") - self.API_HASH = os.getenv("API_HASH") - self.BOT_TOKEN = os.getenv("BOT_TOKEN") - self.BOT_USERNAME = os.getenv("BOT_USERNAME") - self.CHANNEL = os.getenv("CHANNEL") - self.ES_MOVIES_CHANNEL = os.getenv("ES_MOVIES_CHANNEL") - self.ES_SERIES_CHANNEL = os.getenv("ES_SERIES_CHANNEL") - self.MANAGEMENT_MODE = os.getenv("MANAGEMENT_MODE") - self.MANTAINER = os.getenv("MANTAINER") - self.SESSION_STRING = os.getenv("SESSION_STRING") - self.POSTGRES_DB = os.getenv("POSTGRES_DB") - self.POSTGRES_USER = os.getenv("POSTGRES_USER") - self.POSTGRES_PASSWORD = os.getenv("POSTGRES_PASSWORD") - self.POSTGRES_HOST = os.getenv("POSTGRES_HOST") - self.POSTGRES_PORT = os.getenv("POSTGRES_PORT") - self.REDIS_HOST = os.getenv("REDIS_HOST") - self.REDIS_PASSWORD = os.getenv("REDIS_PASSWORD") - self.REDIS_PORT = os.getenv("REDIS_PORT") - - -config = Config() diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7d82829 --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module github.com/SantiiRepair/kreacher-bot + +go 1.21.5 + +require ( + github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/joho/godotenv v1.5.1 // indirect + github.com/redis/go-redis/v9 v9.3.1 // indirect + gopkg.in/telebot.v3 v3.2.1 // indirect +) diff --git a/bot/__main__.py b/kreacher/__main__.py similarity index 100% rename from bot/__main__.py rename to kreacher/__main__.py diff --git a/bot/callbacks/config.py b/kreacher/callbacks/config.py similarity index 100% rename from bot/callbacks/config.py rename to kreacher/callbacks/config.py diff --git a/bot/callbacks/misc.py b/kreacher/callbacks/misc.py similarity index 100% rename from bot/callbacks/misc.py rename to kreacher/callbacks/misc.py diff --git a/bot/callbacks/streams.py b/kreacher/callbacks/streams.py similarity index 100% rename from bot/callbacks/streams.py rename to kreacher/callbacks/streams.py diff --git a/kreacher/clients.go b/kreacher/clients.go new file mode 100755 index 0000000..5a8366b --- /dev/null +++ b/kreacher/clients.go @@ -0,0 +1,20 @@ +package kreacher + +import ( + "time" + + "github.com/redis/go-redis/v9" + tele "gopkg.in/telebot.v3" +) + +var rdb = redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password set + DB: 0, // use default DB + Protocol: 3, // specify 2 for RESP 2 or 3 for RESP 3 +}) + +var kbot, _ = tele.NewBot(tele.Settings{ + Token: NewConfig().BotToken, + Poller: &tele.LongPoller{Timeout: 10 * time.Second}, +}) diff --git a/bot/commands/actives.py b/kreacher/commands/actives.py similarity index 100% rename from bot/commands/actives.py rename to kreacher/commands/actives.py diff --git a/bot/commands/config.py b/kreacher/commands/config.py similarity index 100% rename from bot/commands/config.py rename to kreacher/commands/config.py diff --git a/bot/commands/help.py b/kreacher/commands/help.py similarity index 100% rename from bot/commands/help.py rename to kreacher/commands/help.py diff --git a/bot/commands/join.py b/kreacher/commands/join.py similarity index 100% rename from bot/commands/join.py rename to kreacher/commands/join.py diff --git a/bot/commands/leave.py b/kreacher/commands/leave.py similarity index 100% rename from bot/commands/leave.py rename to kreacher/commands/leave.py diff --git a/bot/commands/logs.py b/kreacher/commands/logs.py similarity index 100% rename from bot/commands/logs.py rename to kreacher/commands/logs.py diff --git a/bot/commands/ping.py b/kreacher/commands/ping.py similarity index 100% rename from bot/commands/ping.py rename to kreacher/commands/ping.py diff --git a/bot/commands/play_book.py b/kreacher/commands/play_book.py similarity index 100% rename from bot/commands/play_book.py rename to kreacher/commands/play_book.py diff --git a/bot/commands/play_song.py b/kreacher/commands/play_song.py similarity index 100% rename from bot/commands/play_song.py rename to kreacher/commands/play_song.py diff --git a/bot/commands/play_video.py b/kreacher/commands/play_video.py similarity index 100% rename from bot/commands/play_video.py rename to kreacher/commands/play_video.py diff --git a/bot/commands/speedtest.py b/kreacher/commands/speedtest.py similarity index 100% rename from bot/commands/speedtest.py rename to kreacher/commands/speedtest.py diff --git a/bot/commands/start.py b/kreacher/commands/start.py similarity index 100% rename from bot/commands/start.py rename to kreacher/commands/start.py diff --git a/bot/commands/streaming.py b/kreacher/commands/streaming.py similarity index 100% rename from bot/commands/streaming.py rename to kreacher/commands/streaming.py diff --git a/kreacher/config.go b/kreacher/config.go new file mode 100755 index 0000000..c801442 --- /dev/null +++ b/kreacher/config.go @@ -0,0 +1,57 @@ +package kreacher + +import ( + "fmt" + "os" + + "github.com/joho/godotenv" +) + +type Config struct { + APIID string + APIHash string + BotToken string + BotUsername string + Channel string + ESMoviesChannel string + ESSeriesChannel string + ManagementMode string + Maintainer string + SessionString string + PostgresDB string + PostgresUser string + PostgresPassword string + PostgresHost string + PostgresPort string + RedisHost string + RedisPassword string + RedisPort string +} + +func NewConfig() *Config { + err := godotenv.Load("../.env") + if err != nil { + fmt.Println("No .env file found") + } + + return &Config{ + APIID: os.Getenv("API_ID"), + APIHash: os.Getenv("API_HASH"), + BotToken: os.Getenv("BOT_TOKEN"), + BotUsername: os.Getenv("BOT_USERNAME"), + Channel: os.Getenv("CHANNEL"), + ESMoviesChannel: os.Getenv("ES_MOVIES_CHANNEL"), + ESSeriesChannel: os.Getenv("ES_SERIES_CHANNEL"), + ManagementMode: os.Getenv("MANAGEMENT_MODE"), + Maintainer: os.Getenv("MANTAINER"), + SessionString: os.Getenv("SESSION_STRING"), + PostgresDB: os.Getenv("POSTGRES_DB"), + PostgresUser: os.Getenv("POSTGRES_USER"), + PostgresPassword: os.Getenv("POSTGRES_PASSWORD"), + PostgresHost: os.Getenv("POSTGRES_HOST"), + PostgresPort: os.Getenv("POSTGRES_PORT"), + RedisHost: os.Getenv("REDIS_HOST"), + RedisPassword: os.Getenv("REDIS_PASSWORD"), + RedisPort: os.Getenv("REDIS_PORT"), + } +} diff --git a/bot/db/tables.py b/kreacher/db/tables.py similarity index 100% rename from bot/db/tables.py rename to kreacher/db/tables.py diff --git a/bot/decorators/permissions.py b/kreacher/decorators/permissions.py similarity index 100% rename from bot/decorators/permissions.py rename to kreacher/decorators/permissions.py diff --git a/bot/decorators/sides.py b/kreacher/decorators/sides.py similarity index 100% rename from bot/decorators/sides.py rename to kreacher/decorators/sides.py diff --git a/bot/helpers/bash.py b/kreacher/helpers/bash.py similarity index 100% rename from bot/helpers/bash.py rename to kreacher/helpers/bash.py diff --git a/bot/helpers/db.py b/kreacher/helpers/db.py similarity index 100% rename from bot/helpers/db.py rename to kreacher/helpers/db.py diff --git a/bot/helpers/nums.py b/kreacher/helpers/nums.py similarity index 100% rename from bot/helpers/nums.py rename to kreacher/helpers/nums.py diff --git a/bot/helpers/progress.py b/kreacher/helpers/progress.py similarity index 100% rename from bot/helpers/progress.py rename to kreacher/helpers/progress.py diff --git a/bot/helpers/queues.py b/kreacher/helpers/queues.py similarity index 100% rename from bot/helpers/queues.py rename to kreacher/helpers/queues.py diff --git a/bot/helpers/translator.py b/kreacher/helpers/translator.py similarity index 100% rename from bot/helpers/translator.py rename to kreacher/helpers/translator.py diff --git a/bot/helpers/tts.py b/kreacher/helpers/tts.py similarity index 100% rename from bot/helpers/tts.py rename to kreacher/helpers/tts.py diff --git a/bot/helpers/user_info.py b/kreacher/helpers/user_info.py similarity index 100% rename from bot/helpers/user_info.py rename to kreacher/helpers/user_info.py diff --git a/bot/helpers/yt.py b/kreacher/helpers/yt.py similarity index 100% rename from bot/helpers/yt.py rename to kreacher/helpers/yt.py diff --git a/bot/scrapers/images.py b/kreacher/scrapers/images.py similarity index 100% rename from bot/scrapers/images.py rename to kreacher/scrapers/images.py diff --git a/bot/setup.py b/kreacher/setup.py similarity index 100% rename from bot/setup.py rename to kreacher/setup.py diff --git a/bot/tasks/on_joined.py b/kreacher/tasks/on_joined.py similarity index 100% rename from bot/tasks/on_joined.py rename to kreacher/tasks/on_joined.py diff --git a/bot/tasks/vcs.py b/kreacher/tasks/vcs.py similarity index 100% rename from bot/tasks/vcs.py rename to kreacher/tasks/vcs.py diff --git a/bot/utils/driver.py b/kreacher/utils/driver.py similarity index 100% rename from bot/utils/driver.py rename to kreacher/utils/driver.py diff --git a/bot/utils/piper.py b/kreacher/utils/piper.py similarity index 100% rename from bot/utils/piper.py rename to kreacher/utils/piper.py