From ad89d518c9c1bd5640d2aec5640bae6befd2ea31 Mon Sep 17 00:00:00 2001 From: garickbadalov Date: Tue, 17 Oct 2023 01:59:28 +0300 Subject: [PATCH] refactored tg parser --- src/core/master_service.py | 36 +++++++++++-------- .../telegram_media_downloader/config.yaml | 8 ++--- .../telegram_parser.py | 7 ++-- src/web/main.py | 3 +- 4 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/core/master_service.py b/src/core/master_service.py index 0e28756..5abbc79 100644 --- a/src/core/master_service.py +++ b/src/core/master_service.py @@ -18,6 +18,7 @@ from src.parsers.Telegram.telegram_media_downloader.media_downloader import app, from src.parsers.Yappy.yappy_parser import YappyParser from src.parsers.base_parser import BaseParser from src.parsers.parser_mapping import get_parser +from src.parsers.Telegram.telegram_media_downloader.telegram_parser import TelegramParser class MasterService: @@ -66,20 +67,27 @@ class MasterService: @staticmethod def video_download(video_params: dict): - downloader: BaseParser | YappyParser | MyMailParser = MasterService.get_parser(video_params) - if _check_config(): - tg_client = pyrogram.Client( - "media_downloader", - api_id=app.api_id, - api_hash=app.api_hash, - proxy=app.proxy, - workdir=app.session_file_path, - ) - app.pre_run() - app.is_running = True - tg_client.start() - result = downloader.video_download(tg_client) - return result + downloader: BaseParser | YappyParser | MyMailParser | TelegramParser = MasterService.get_parser(video_params) + match downloader: + case TelegramParser(): + if _check_config(): + tg_client = pyrogram.Client( + "media_downloader", + api_id=app.api_id, + api_hash=app.api_hash, + proxy=app.proxy, + workdir=app.session_file_path, + ) + app.pre_run() + app.is_running = True + tg_client.start() + result = downloader.video_download(client=tg_client) + return result + case _: + result = downloader.video_download() + return result + + @staticmethod def get_parser(params: dict): diff --git a/src/parsers/Telegram/telegram_media_downloader/config.yaml b/src/parsers/Telegram/telegram_media_downloader/config.yaml index 0958a7a..a3947a5 100644 --- a/src/parsers/Telegram/telegram_media_downloader/config.yaml +++ b/src/parsers/Telegram/telegram_media_downloader/config.yaml @@ -1,9 +1,9 @@ api_hash: cb06da2bf01e15627434223242b6446d api_id: 21648766 chat: -- chat_id: landigos - download_filter: id >= 6949 && id < 6950 - last_read_message_id: 6949 +- chat_id: -1001966291562 + download_filter: id == 2048 + last_read_message_id: 2048 file_formats: video: - all @@ -12,7 +12,7 @@ media_types: # in linux please use / # save_path: E:\github\telegram_media_downloader disable_syslog: [] -save_path: /Users/garickbadalov/PycharmProjects/video_downloader_service/downloads/Telegram/ +save_path: downloads/Telegram/ language: RU web_host: 0.0.0.0 web_port: 51256 diff --git a/src/parsers/Telegram/telegram_media_downloader/telegram_parser.py b/src/parsers/Telegram/telegram_media_downloader/telegram_parser.py index e5bf8dd..1aef684 100644 --- a/src/parsers/Telegram/telegram_media_downloader/telegram_parser.py +++ b/src/parsers/Telegram/telegram_media_downloader/telegram_parser.py @@ -14,14 +14,15 @@ from src.parsers.base_parser import BaseParser class TelegramParser(BaseParser): def video_download(self, client: Client = None): url_parse_result = urlparse(self.params["link"]) - channel, message_id = url_parse_result.path[1:].split('/') + channel, message_id = url_parse_result.path[1:].split('/') if "/c/" not in url_parse_result.path else \ + url_parse_result.path[3:].split('/') if os.path.exists(os.path.join(os.getcwd() + f"/downloads/Telegram/{message_id}.mp4")): raise FileAlreadyExistException(message=f"Telegram/{message_id}.mp4") with open(os.path.join(os.path.abspath(""), "src/parsers/Telegram/telegram_media_downloader/config.yaml"), mode="r+", encoding="utf-8") as f: config = YAML().load(f.read()) - config["chat"][0]['download_filter'] = f"id >= {message_id} && id < {int(message_id) + 1}" - config["chat"][0]['chat_id'] = channel + config["chat"][0]['download_filter'] = f"id == {message_id}" + config["chat"][0]['chat_id'] = channel if "/c/" not in url_parse_result.path else int(f"-100{channel}") config["chat"][0]['last_read_message_id'] = int(message_id) with open(os.path.join(os.path.abspath(""), "src/parsers/Telegram/telegram_media_downloader/config.yaml"), diff --git a/src/web/main.py b/src/web/main.py index d5dc204..abb62d5 100644 --- a/src/web/main.py +++ b/src/web/main.py @@ -1,11 +1,10 @@ import json import os -from ast import literal_eval import uvicorn +import logging from aio_pika import connect, Message, DeliveryMode from fastapi import FastAPI, Request, Depends -import logging from starlette.middleware.cors import CORSMiddleware from starlette.responses import JSONResponse, FileResponse, StreamingResponse from starlette.templating import Jinja2Templates