From 806bdfba176d3c203d101576fe42e51ca76e2177 Mon Sep 17 00:00:00 2001 From: JuntexZhuk Date: Mon, 20 Nov 2023 14:03:26 +0600 Subject: [PATCH 1/7] fix --- main.py | 2 +- models.py | 12 ++++++++++++ user_list.json | 15 +++++++++------ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index 6820a7a..5731435 100644 --- a/main.py +++ b/main.py @@ -16,4 +16,4 @@ async def main(): try: asyncio.run(main()) except KeyboardInterrupt: - print("main error") + print("Start error") diff --git a/models.py b/models.py index 7db887e..0818ab4 100644 --- a/models.py +++ b/models.py @@ -25,6 +25,7 @@ class Users(Base): created_date = Column(DateTime, default=datetime.datetime.utcnow) updated_at = Column(DateTime, default=datetime.datetime.utcnow) + @staticmethod def add_user(telegram_id: int, username: str): session = Session() user = session.query(Users).where(Users.telegram_id == telegram_id).first() @@ -34,11 +35,13 @@ def add_user(telegram_id: int, username: str): user = Users(telegram_id=telegram_id, username=username) Users.add_close(user, session) + @staticmethod def add_close(user, session): session.add(user) session.commit() session.close() + @staticmethod def add_admin(telegram_id: int, username: str): session = Session() user = session.query(Users).where(Users.telegram_id == telegram_id).first() @@ -48,6 +51,7 @@ def add_admin(telegram_id: int, username: str): user = Users(username=username, telegram_id=telegram_id, group_id=2) Users.add_close(user, session) + @staticmethod def change_access(telegram_id: int, group_id: int) -> object: session = Session() user = session.query(Users).where(Users.telegram_id == telegram_id).first() @@ -60,6 +64,7 @@ def change_access(telegram_id: int, group_id: int) -> object: else: return False + @staticmethod def check_vip(telegram_id: int) -> object: user = session.query(Users).where(Users.telegram_id == telegram_id).first() user_group = user.group_id @@ -68,6 +73,7 @@ def check_vip(telegram_id: int) -> object: else: return False + @staticmethod def check_owner(telegram_id: int) -> object: user = session.query(Users).where(Users.telegram_id == telegram_id).first() user_group = user.group_id @@ -76,6 +82,7 @@ def check_owner(telegram_id: int) -> object: else: return False + @staticmethod def check_admin(telegram_id: int) -> object: user = session.query(Users).where(Users.telegram_id == telegram_id).first() user_group = user.group_id @@ -84,6 +91,7 @@ def check_admin(telegram_id: int) -> object: else: return False + @staticmethod def get_id(telegram_id: int) -> object: user = session.query(Users).where(Users.telegram_id == telegram_id).first() user_id = user.id @@ -98,6 +106,7 @@ class Items(Base): hash_name = Column(String(length=60)) item_count = Column(Integer) + @staticmethod def add_item(telegram_id: int, hash_name: str, item_count: int): session = Session() user_id = Users.get_id(telegram_id) @@ -112,6 +121,7 @@ def add_item(telegram_id: int, hash_name: str, item_count: int): session.commit() session.close() + @staticmethod def user_items(telegram_id: int) -> object: session = Session() user_id = Users.get_id(telegram_id) @@ -124,6 +134,7 @@ def user_items(telegram_id: int) -> object: user_item.update(u) return user_item + @staticmethod def delete_item(telegram_id: int, hash_name: str): session = Session() user_id = Users.get_id(telegram_id) @@ -142,6 +153,7 @@ class LogBase(Base): function_name = Column(String(length=12)) time_used = Column(DateTime, default=datetime.datetime.utcnow) + @staticmethod def add(telegram_id: int, username: str, function_name: str): session = Session() func = LogBase(telegram_id=telegram_id, username=username, function_name=function_name) diff --git a/user_list.json b/user_list.json index 57d54d7..a52b205 100644 --- a/user_list.json +++ b/user_list.json @@ -11,12 +11,9 @@ ], "960622004": [ "Snakebite Case", - "Revolution Case" - ], - "736491563": [ - "Clutch Case", - "Danger Zone Case", - "CS20 Case" + "Revolution Case", + "Prisma Case", + "Falchion Case" ], "1312583149": [ "CS:GO Weapon Case", @@ -48,5 +45,11 @@ "Dreams & Nightmares Case", "Recoil Case", "Revolution Case" + ], + "736491563": [ + "Operation Broken Fang Case", + "Revolution Case", + "Prisma Case", + "Falchion Case" ] } \ No newline at end of file From 3e15806c828e9e246d86c8e00254d3b397c9e07b Mon Sep 17 00:00:00 2001 From: ZhuchkaTriplesix Date: Tue, 14 May 2024 19:00:34 +0300 Subject: [PATCH 2/7] refactor(modules rename) --- core/config.py | 31 ++++++++++++++++++++++++++++++ data/__init__.py | 3 --- models.py => db/models.py | 0 handlers/admin.py | 4 ++-- handlers/client.py | 6 +++--- handlers/vip.py | 4 ++-- {data => misc}/case_translation.py | 0 {data => misc}/json_support.py | 0 keyboards.py => misc/keyboards.py | 0 requirements.txt | 7 +++++++ 10 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 core/config.py delete mode 100644 data/__init__.py rename models.py => db/models.py (100%) rename {data => misc}/case_translation.py (100%) rename {data => misc}/json_support.py (100%) rename keyboards.py => misc/keyboards.py (100%) create mode 100644 requirements.txt diff --git a/core/config.py b/core/config.py new file mode 100644 index 0000000..5902813 --- /dev/null +++ b/core/config.py @@ -0,0 +1,31 @@ +from typing import Any, Dict, Optional +from dotenv import find_dotenv, load_dotenv +from pydantic_settings import BaseSettings, SettingsConfigDict +from pydantic import PostgresDsn, validator + +load_dotenv(find_dotenv(".env")) + + +class Settings(BaseSettings): + model_config = SettingsConfigDict(case_sensitive=True) + TOKEN: str + POSTGRES_SERVER: str + POSTGRES_USER: str + POSTGRES_PASSWORD: str + POSTGRES_DB: str + SQLALCHEMY_DATABASE_URI: PostgresDsn | None = None + + @validator("SQLALCHEMY_DATABASE_URI", pre=True) + def assemble_db_connection(cls, v: Optional[str], values: Dict[str, Any]) -> Any: + if isinstance(v, str): + return v + return PostgresDsn.build( + scheme="postgresql+pg8000", + username=values.get("POSTGRES_USER"), + password=values.get("POSTGRES_PASSWORD"), + host=values.get("POSTGRES_SERVER"), + path=values.get("POSTGRES_DB"), + ) + + +settings = Settings() diff --git a/data/__init__.py b/data/__init__.py deleted file mode 100644 index 070ac36..0000000 --- a/data/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -from data import json_support -from data import case_translation - diff --git a/models.py b/db/models.py similarity index 100% rename from models.py rename to db/models.py diff --git a/handlers/admin.py b/handlers/admin.py index 7fe98b2..a263fa0 100644 --- a/handlers/admin.py +++ b/handlers/admin.py @@ -1,11 +1,11 @@ from aiogram import F from aiogram.fsm.state import StatesGroup, State from aiogram.types import Message -import keyboards as kb +from misc import keyboards as kb from aiogram import Router from aiogram.fsm.context import FSMContext import sys -import models +from db import models router = Router() diff --git a/handlers/client.py b/handlers/client.py index a493be8..68bc716 100644 --- a/handlers/client.py +++ b/handlers/client.py @@ -1,10 +1,10 @@ from aiogram import F from aiogram.types import Message, CallbackQuery import steammarket as sm -from data import json_support, case_translation -import keyboards as kb +from misc import json_support, case_translation +from misc import keyboards as kb from aiogram import Router -import models +from db import models router = Router() json_data = "user_list.json" diff --git a/handlers/vip.py b/handlers/vip.py index bf6dc60..f9a396f 100644 --- a/handlers/vip.py +++ b/handlers/vip.py @@ -3,8 +3,8 @@ from aiogram.types import Message, CallbackQuery from aiogram import Router from aiogram.fsm.context import FSMContext -import models -import keyboards as kb +from db import models +from misc import keyboards as kb import steammarket as sm from data.config import sell_fee diff --git a/data/case_translation.py b/misc/case_translation.py similarity index 100% rename from data/case_translation.py rename to misc/case_translation.py diff --git a/data/json_support.py b/misc/json_support.py similarity index 100% rename from data/json_support.py rename to misc/json_support.py diff --git a/keyboards.py b/misc/keyboards.py similarity index 100% rename from keyboards.py rename to misc/keyboards.py diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..5e78ffb --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +aiogram~=3.6.0 +pg8000~=1.31.2 +SQLAlchemy~=2.0.30 +python-dotenv~=1.0.1 +pydantic-settings +pydantic~=2.7.1 +steammarket~=0.2.7 \ No newline at end of file From e02e9c374bb2386055502cf676817a2c8e9069a0 Mon Sep 17 00:00:00 2001 From: ZhuchkaTriplesix Date: Tue, 14 May 2024 19:01:46 +0300 Subject: [PATCH 3/7] refactor(modules rename) --- user_list.json | 55 -------------------------------------------------- 1 file changed, 55 deletions(-) delete mode 100644 user_list.json diff --git a/user_list.json b/user_list.json deleted file mode 100644 index a52b205..0000000 --- a/user_list.json +++ /dev/null @@ -1,55 +0,0 @@ -{ - "957268846": [ - "CS20 Case", - "Danger Zone Case", - "Clutch Case" - ], - "6643425631": [ - "Danger Zone Case", - "CS20 Case", - "Clutch Case" - ], - "960622004": [ - "Snakebite Case", - "Revolution Case", - "Prisma Case", - "Falchion Case" - ], - "1312583149": [ - "CS:GO Weapon Case", - "eSports 2013 Case", - "Operation Bravo Case", - "CS:GO Weapon Case 2", - "eSports 2013 Winter Case", - "Winter Offensive Weapon Case", - "Operation Phoenix Weapon Case", - "Huntsman Weapon Case", - "Operation Breakout Weapon Case", - "CS:GO Weapon Case 3", - "eSports 2014 Summer Case", - "Shadow Case", - "Revolver Case", - "Chroma 3 Case", - "Operation Wildfire Case", - "Gamma Case", - "Operation Hydra Case", - "Spectrum 2 Case", - "Clutch Case", - "Spectrum Case", - "Glove Case", - "Horizon Case", - "Prisma Case", - "Danger Zone Case", - "CS20 Case", - "Shattered Web Case", - "Dreams & Nightmares Case", - "Recoil Case", - "Revolution Case" - ], - "736491563": [ - "Operation Broken Fang Case", - "Revolution Case", - "Prisma Case", - "Falchion Case" - ] -} \ No newline at end of file From 478e09a0919c876c6cd36769165d6bbd4f2b2b2a Mon Sep 17 00:00:00 2001 From: ZhuchkaTriplesix Date: Tue, 14 May 2024 19:06:14 +0300 Subject: [PATCH 4/7] example --- .env.example | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..08848b2 --- /dev/null +++ b/.env.example @@ -0,0 +1,5 @@ +TOKEN=telegram.token +POSTGRES_SERVER=db.example.com +POSTGRES_USER=user +POSTGRES_PASSWORD=pass +POSTGRES_DB=app \ No newline at end of file From 52765b69c88077b78bc5d1112967469ed364f775 Mon Sep 17 00:00:00 2001 From: ZhuchkaTriplesix Date: Tue, 14 May 2024 19:09:32 +0300 Subject: [PATCH 5/7] config --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 05fe4d1..470ec7f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # Byte-compiled / optimized / DLL files users_time.json user_list.json +.env __pycache__/ *.py[cod] *$py.class From 92538acfd11b4f757f1b8697d1724deef1f10bd5 Mon Sep 17 00:00:00 2001 From: ZhuchkaTriplesix Date: Tue, 14 May 2024 19:12:50 +0300 Subject: [PATCH 6/7] docker --- .gitignore | 2 -- Dockerfile | 28 ++++++++++++++++++++++++++++ docker-compose.yaml | 16 ++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yaml diff --git a/.gitignore b/.gitignore index 470ec7f..44f8b7d 100644 --- a/.gitignore +++ b/.gitignore @@ -18,8 +18,6 @@ cases.json .idea/vcs.xml users.db .DS_Store -docker-compose.yaml -Dockerfile data/config.py # Distribution / packaging .Python diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d6a5724 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,28 @@ +FROM python:3.11 + +# Set environment variables +ENV PYTHONDONTWRITEBYTECODE 1 +ENV PYTHONUNBUFFERED 1 + +# Set working directory in the container +WORKDIR /price_bot + +# Install system dependencies +RUN apt-get update \ + && apt-get install -y --no-install-recommends \ + gcc \ + gettext \ + && rm -rf /var/lib/apt/lists/* + +# Install Python dependencies +COPY requirements.txt /code/ +RUN pip install --no-cache-dir -r requirements.txt + +# Copy the application code into the container +COPY . /code/ + +# Expose the port the application runs on +EXPOSE 8080 + +# Run the application +CMD ["python", "main.py"] \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..af08032 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,16 @@ +version: '3.8' + +services: + postgres: + image: postgres:latest + restart: always + + bot: + build: . + restart: always + depends_on: + - postgres + volumes: + - .:/price_bot + ports: + - "8080:8080" \ No newline at end of file From f6e4e37f3b7449a9683f3a0292d9b6697d823c2a Mon Sep 17 00:00:00 2001 From: ZhuchkaTriplesix Date: Tue, 14 May 2024 19:15:58 +0300 Subject: [PATCH 7/7] fix --- db/models.py | 4 ++-- main.py | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/db/models.py b/db/models.py index 0818ab4..e8db8f3 100644 --- a/db/models.py +++ b/db/models.py @@ -2,12 +2,12 @@ from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker, declarative_base, scoped_session import datetime -from data.config import host, user, password, db_name +from core.config import settings import pg8000 Base = declarative_base() -engine = create_engine(f'postgresql+pg8000://{user}:{password}@{host}/{db_name}', echo=False) +engine = create_engine(settings.SQLALCHEMY_DATABASE_URI, echo=False) Session = sessionmaker(bind=engine) session = scoped_session(Session) diff --git a/main.py b/main.py index 5731435..afbd833 100644 --- a/main.py +++ b/main.py @@ -1,12 +1,11 @@ import asyncio from aiogram import Bot, Dispatcher from handlers import client, admin, vip +from core.config import settings async def main(): - with open("token.txt", "r") as TOKEN: - bot_token = TOKEN.readline() - bot = Bot(bot_token) + bot = Bot(settings.TOKEN) dp = Dispatcher() dp.include_routers(admin.router, vip.router, client.router) # routers add await dp.start_polling(bot, skip_updates=True)