refactored tg parser

This commit is contained in:
Dantenerosas 2023-10-17 01:59:28 +03:00 committed by nikili0n
parent c680850936
commit d3ebf18cd7
4 changed files with 31 additions and 23 deletions

View File

@ -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.Yappy.yappy_parser import YappyParser
from src.parsers.base_parser import BaseParser from src.parsers.base_parser import BaseParser
from src.parsers.parser_mapping import get_parser from src.parsers.parser_mapping import get_parser
from src.parsers.Telegram.telegram_media_downloader.telegram_parser import TelegramParser
class MasterService: class MasterService:
@ -66,7 +67,9 @@ class MasterService:
@staticmethod @staticmethod
def video_download(video_params: dict): def video_download(video_params: dict):
downloader: BaseParser | YappyParser | MyMailParser = MasterService.get_parser(video_params) downloader: BaseParser | YappyParser | MyMailParser | TelegramParser = MasterService.get_parser(video_params)
match downloader:
case TelegramParser():
if _check_config(): if _check_config():
tg_client = pyrogram.Client( tg_client = pyrogram.Client(
"media_downloader", "media_downloader",
@ -78,8 +81,13 @@ class MasterService:
app.pre_run() app.pre_run()
app.is_running = True app.is_running = True
tg_client.start() tg_client.start()
result = downloader.video_download(tg_client) result = downloader.video_download(client=tg_client)
return result return result
case _:
result = downloader.video_download()
return result
@staticmethod @staticmethod
def get_parser(params: dict): def get_parser(params: dict):

View File

@ -1,9 +1,9 @@
api_hash: cb06da2bf01e15627434223242b6446d api_hash: cb06da2bf01e15627434223242b6446d
api_id: 21648766 api_id: 21648766
chat: chat:
- chat_id: landigos - chat_id: -1001966291562
download_filter: id >= 6949 && id < 6950 download_filter: id == 2048
last_read_message_id: 6949 last_read_message_id: 2048
file_formats: file_formats:
video: video:
- all - all
@ -12,7 +12,7 @@ media_types:
# in linux please use / # in linux please use /
# save_path: E:\github\telegram_media_downloader # save_path: E:\github\telegram_media_downloader
disable_syslog: [] disable_syslog: []
save_path: /Users/garickbadalov/PycharmProjects/video_downloader_service/downloads/Telegram/ save_path: downloads/Telegram/
language: RU language: RU
web_host: 0.0.0.0 web_host: 0.0.0.0
web_port: 51256 web_port: 51256

View File

@ -14,14 +14,15 @@ from src.parsers.base_parser import BaseParser
class TelegramParser(BaseParser): class TelegramParser(BaseParser):
def video_download(self, client: Client = None): def video_download(self, client: Client = None):
url_parse_result = urlparse(self.params["link"]) 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")): if os.path.exists(os.path.join(os.getcwd() + f"/downloads/Telegram/{message_id}.mp4")):
raise FileAlreadyExistException(message=f"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"), with open(os.path.join(os.path.abspath(""), "src/parsers/Telegram/telegram_media_downloader/config.yaml"),
mode="r+", encoding="utf-8") as f: mode="r+", encoding="utf-8") as f:
config = YAML().load(f.read()) config = YAML().load(f.read())
config["chat"][0]['download_filter'] = f"id >= {message_id} && id < {int(message_id) + 1}" config["chat"][0]['download_filter'] = f"id == {message_id}"
config["chat"][0]['chat_id'] = channel 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) 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"), with open(os.path.join(os.path.abspath(""), "src/parsers/Telegram/telegram_media_downloader/config.yaml"),

View File

@ -1,11 +1,10 @@
import json import json
import os import os
from ast import literal_eval
import uvicorn import uvicorn
import logging
from aio_pika import connect, Message, DeliveryMode from aio_pika import connect, Message, DeliveryMode
from fastapi import FastAPI, Request, Depends from fastapi import FastAPI, Request, Depends
import logging
from starlette.middleware.cors import CORSMiddleware from starlette.middleware.cors import CORSMiddleware
from starlette.responses import JSONResponse, FileResponse, StreamingResponse from starlette.responses import JSONResponse, FileResponse, StreamingResponse
from starlette.templating import Jinja2Templates from starlette.templating import Jinja2Templates