refactored tg parser

This commit is contained in:
Dantenerosas 2023-10-17 01:59:28 +03:00 committed by nikili0n
parent 2dd7ad12e7
commit c3cffc14b7
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,20 +67,27 @@ 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)
if _check_config(): match downloader:
tg_client = pyrogram.Client( case TelegramParser():
"media_downloader", if _check_config():
api_id=app.api_id, tg_client = pyrogram.Client(
api_hash=app.api_hash, "media_downloader",
proxy=app.proxy, api_id=app.api_id,
workdir=app.session_file_path, api_hash=app.api_hash,
) proxy=app.proxy,
app.pre_run() workdir=app.session_file_path,
app.is_running = True )
tg_client.start() app.pre_run()
result = downloader.video_download(tg_client) app.is_running = True
return result tg_client.start()
result = downloader.video_download(client=tg_client)
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