Skip to content

Commit

Permalink
Restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
zdon0 committed Apr 13, 2022
1 parent 6538edf commit be39e11
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 25 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/database.json
/token
/service/database.json
/service/token
File renamed without changes.
File renamed without changes.
30 changes: 14 additions & 16 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
from aiogram import Bot, Dispatcher, types, executor
from aiogram.utils.exceptions import RetryAfter

import chat
import markups
import shein
import sportmaster
from chat import markups, functions
from parse import shein, sportmaster

logging.basicConfig(level=logging.INFO)
with open('token', 'r') as file:
with open('service/token', 'r') as file:
bot = Bot(token=file.readline().strip())
dp = Dispatcher(bot)

Expand All @@ -27,7 +25,7 @@
prices = {'3K', '5K', '10K', '>10K'}

try:
with open('database.json', 'r', encoding='utf-8') as file:
with open('service/database.json', 'r', encoding='utf-8') as file:
data = json.load(file)

for key in list(data.keys()):
Expand Down Expand Up @@ -97,7 +95,7 @@ async def backpack(message: types.Message):
data[message.chat.id]["ans"] = ans
data[message.chat.id]['i'] = 0
data[message.chat.id]["stop"] = False
await chat.changer(data[message.chat.id]["ans"], data[message.chat.id]["filters"])
await functions.changer(data[message.chat.id]["ans"], data[message.chat.id]["filters"])


@dp.message_handler(content_types='text', text="Список желаний")
Expand All @@ -107,7 +105,7 @@ async def show_like(message: types.Message):
return
loop = asyncio.get_event_loop()
ans = await message.answer(text="Загрузка")
waiter = loop.create_task(chat.waiter(ans, text="Загрузка"))
waiter = loop.create_task(functions.waiter(ans, text="Загрузка"))

bags = list(data[message.chat.id]["like"])
sportmaster_bags = list(filter(lambda x: "sport" in x[0], bags))
Expand Down Expand Up @@ -149,7 +147,7 @@ async def call_handler_colors(call: types.CallbackQuery):
else:
data[call.from_user.id]['filters']['colors'].add(call.data)

await chat.changer(data[call.from_user.id]["ans"], data[call.from_user.id]["filters"])
await functions.changer(data[call.from_user.id]["ans"], data[call.from_user.id]["filters"])


@dp.callback_query_handler(lambda call: call.data in chars)
Expand All @@ -159,7 +157,7 @@ async def call_handler_chars(call: types.CallbackQuery):
else:
data[call.from_user.id]['filters']['types'].add(call.data)

await chat.changer(data[call.from_user.id]["ans"], data[call.from_user.id]["filters"])
await functions.changer(data[call.from_user.id]["ans"], data[call.from_user.id]["filters"])


@dp.callback_query_handler(lambda call: call.data in volumes_1)
Expand All @@ -169,7 +167,7 @@ async def call_handler_volumes1(call: types.CallbackQuery):
else:
data[call.from_user.id]['filters']['volume_1'] = call.data

await chat.changer(data[call.from_user.id]["ans"], data[call.from_user.id]["filters"])
await functions.changer(data[call.from_user.id]["ans"], data[call.from_user.id]["filters"])


@dp.callback_query_handler(lambda call: call.data in volumes_2)
Expand All @@ -179,7 +177,7 @@ async def call_handler_volumes2(call: types.CallbackQuery):
else:
data[call.from_user.id]['filters']['volume_2'] = call.data

await chat.changer(data[call.from_user.id]["ans"], data[call.from_user.id]["filters"])
await functions.changer(data[call.from_user.id]["ans"], data[call.from_user.id]["filters"])


@dp.callback_query_handler(lambda call: call.data in prices)
Expand All @@ -189,7 +187,7 @@ async def call_handler_prices(call: types.CallbackQuery):
else:
data[call.from_user.id]['filters']['price'] = call.data

await chat.changer(data[call.from_user.id]["ans"], data[call.from_user.id]["filters"])
await functions.changer(data[call.from_user.id]["ans"], data[call.from_user.id]["filters"])


@dp.callback_query_handler(lambda call: call.data == "next")
Expand All @@ -209,7 +207,7 @@ async def call_handler_back(call: types.CallbackQuery):
@dp.callback_query_handler(lambda call: call.data == "parse")
async def call_handler_parse(call: types.CallbackQuery):
loop = asyncio.get_event_loop()
waiter = loop.create_task(chat.waiter(call.message))
waiter = loop.create_task(functions.waiter(call.message))

filters = dict()
for key in data[call.from_user.id]["filters"].keys():
Expand All @@ -227,7 +225,7 @@ async def call_handler_parse(call: types.CallbackQuery):
filters["price"] = ">10K"

if filters != data[call.from_user.id]["filters"]:
await chat.changer(data[call.from_user.id]['ans'], filters)
await functions.changer(data[call.from_user.id]['ans'], filters)

tasks = [asyncio.ensure_future(f) for f in [sportmaster.parser(loop, filters),
shein.parser(loop, filters)]]
Expand Down Expand Up @@ -290,7 +288,7 @@ def main():
executor.start_polling(dp)
for key in data.keys():
data[key]['ans'] = None
with open('database.json', 'w', encoding='utf-8') as file:
with open('service/database.json', 'w', encoding='utf-8') as file:
json.dump(data, file, ensure_ascii=False, indent=4, default=lambda x: list(x))
# asyncio.run(bot.send_document(chat_id=-1001745130102, document=types.InputFile("./database.json")))

Expand Down
6 changes: 3 additions & 3 deletions shein.py → parse/shein.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import re
import aiohttp
import asyncio
from bs4 import BeautifulSoup
import json
import re
from random import randint

import aiohttp
from bs4 import BeautifulSoup

connector = {
"values": {
Expand Down
8 changes: 4 additions & 4 deletions sportmaster.py → parse/sportmaster.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import re
import json
from bs4 import BeautifulSoup
import aiohttp
import asyncio
import json
import re

import aiohttp
from bs4 import BeautifulSoup

headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36 OPR/84.0.4316.31'}
Expand Down
1 change: 1 addition & 0 deletions bot.bat → service/bot.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@echo off
cd ..
CALL venv\Scripts\activate.bat
python main.py
File renamed without changes.

0 comments on commit be39e11

Please sign in to comment.