refactored tg parser

This commit is contained in:
garickbadalov 2023-10-17 01:59:28 +03:00
parent fce408310a
commit ad89d518c9
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.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):

View File

@ -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

View File

@ -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"),

View File

@ -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