Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support multi media forward #116

Merged
merged 2 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/docker-publish-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
TARGET: [macos, linux-amd64, win64]
TARGET: [win64] #[macos, linux-amd64, win64]
env:
DISTPATH: tdl-${{ needs.create_release.outputs.VERSION }}-${{ matrix.TARGET }}
steps:
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@ save_path: D:\telegram_media_downloader
file_path_prefix:
- chat_title
- media_datetime
disable_syslog:
- INFO
upload_drive:
# required
enable_upload_file: true
Expand Down Expand Up @@ -212,7 +210,6 @@ web_login_secret: 123
- `chat_title` - Channel or group title, it will be chat id if not exist title.
- `media_datetime` - Media date, also see pyrogram.types.Message.date.strftime("%Y_%m").
- `media_type` - Media type, also see `media_types`.
- **disable_syslog** - You can choose which types of logs to disable,see `logging._nameToLevel`.
- **upload_drive** - You can upload file to cloud drive.
- `enable_upload_file` - Enable upload file, default `false`.
- `remote_dir` - Where you upload, like `drive_id/drive_name`.
Expand All @@ -231,6 +228,8 @@ web_login_secret: 123
- **web_port** - Web port
- **language** - Application language, the default is English (`EN`), optional `ZH`(Chinese),`RU`,`UA`
- **web_login_secret** - Web page login password, if not configured, no login is required to access the web page
- **log_level** - see `logging._nameToLevel`.
- **forward_limit** - Limit the number of forwards per minute, the default is 33, please do not modify this parameter by default.


## Execution
Expand Down
6 changes: 2 additions & 4 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,6 @@ save_path: D:\telegram_media_downloader
file_path_prefix:
- chat_title
- media_datetime
disable_syslog:
- INFO
upload_drive:
enable_upload_file: true
remote_dir: drive:/telegram
Expand Down Expand Up @@ -208,7 +206,6 @@ web_login_secret: 123
- `chat_title` - 聊天频道或者群组标题, 如果找不到标题则为配置文件中的`chat_id`
- `media_datetime` - 资源的发布时间
- `media_type` - 资源类型,类型查阅 `media_types`
- **disable_syslog** - 您可以选择要禁用的日志类型,请参阅 `logging._nameToLevel`
- **upload_drive** - 您可以将文件上传到云盘
- `enable_upload_file` - [必填]启用上传文件,默认为`false`
- `remote_dir` - [必填]你上传的地方
Expand All @@ -227,7 +224,8 @@ web_login_secret: 123
- **web_port** - web界面端口
- **language** - 应用语言,默认为英文(`EN`),可选`ZH`(中文),`RU`,`UA`
- **web_login_secret** - 网页登录密码,如果不配置则访问网页不需要登录

- **log_level** - 默认日志等级,请参阅 `logging._nameToLevel`
- **forward_limit** - 限制每分钟转发次数,默认为33,默认请不要修改该参数
## 执行

```sh
Expand Down
54 changes: 36 additions & 18 deletions media_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from module.get_chat_history_v2 import get_chat_history_v2
from module.language import _t
from module.pyrogram_extension import (
HookClient,
fetch_message,
get_extension,
record_download_status,
Expand Down Expand Up @@ -44,13 +45,6 @@
APPLICATION_NAME = "media_downloader"
app = Application(CONFIG_NAME, DATA_FILE_NAME, APPLICATION_NAME)

logger.add(
os.path.join(app.log_file_path, "tdl.log"),
rotation="10 MB",
retention="10 days",
level="DEBUG",
)

queue: asyncio.Queue = asyncio.Queue()
RETRY_TIME_OUT = 3

Expand Down Expand Up @@ -261,13 +255,11 @@ async def _get_media_meta(
async def add_download_task(
message: pyrogram.types.Message,
node: TaskNode,
chat_download_config: ChatDownloadConfig = None,
):
"""Add Download task"""
if message.empty:
return False
if chat_download_config:
chat_download_config.download_status[message.id] = DownloadStatus.Downloading
node.download_status[message.id] = DownloadStatus.Downloading
await queue.put((message, node))
node.total_task += 1
return True
Expand All @@ -283,7 +275,9 @@ async def download_task(
)

if not node.bot:
app.set_download_id(node.chat_id, message.id, download_status)
app.set_download_id(node, message.id, download_status)

node.download_status[message.id] = download_status

file_size = os.path.getsize(file_name) if file_name else 0

Expand All @@ -293,8 +287,8 @@ async def download_task(
app,
node,
message,
file_name,
download_status,
file_name,
)

# rclone upload
Expand Down Expand Up @@ -474,6 +468,12 @@ def _check_config() -> bool:
print_meta(logger)
try:
_load_config()
logger.add(
os.path.join(app.log_file_path, "tdl.log"),
rotation="10 MB",
retention="10 days",
level=app.log_level,
)
except Exception as e:
logger.exception(f"load config error: {e}")
return False
Expand Down Expand Up @@ -510,17 +510,21 @@ async def download_chat_task(
client,
node.chat_id,
limit=node.limit,
max_id=node.end_offset_id,
offset_id=chat_download_config.last_read_message_id,
reverse=True,
)

chat_download_config.node = node

if chat_download_config.ids_to_retry:
logger.info(f"{_t('Downloading files failed during last run')}...")
skipped_messages: list = await client.get_messages( # type: ignore
chat_id=node.chat_id, message_ids=chat_download_config.ids_to_retry
)

for message in skipped_messages:
await add_download_task(message, node, chat_download_config)
await add_download_task(message, node)

async for message in messages_iter: # type: ignore
meta_data = MetaData()
Expand All @@ -533,8 +537,21 @@ async def download_chat_task(
caption = app.get_caption_name(node.chat_id, message.media_group_id)
set_meta_data(meta_data, message, caption)

if not app.need_skip_message(chat_download_config, message.id, meta_data):
await add_download_task(message, node, chat_download_config)
if app.need_skip_message(chat_download_config, message.id):
continue

if app.exec_filter(chat_download_config, meta_data):
await add_download_task(message, node)
else:
node.download_status[message.id] = DownloadStatus.SkipDownload
await upload_telegram_chat(
client,
node.upload_user,
app,
node,
message,
DownloadStatus.SkipDownload,
)

chat_download_config.need_check = True
chat_download_config.total_task = node.total_task
Expand All @@ -544,9 +561,9 @@ async def download_chat_task(
async def download_all_chat(client: pyrogram.Client):
"""Download All chat"""
for key, value in app.chat_download_config.items():
node = TaskNode(chat_id=key)
value.node = TaskNode(chat_id=key)
try:
await download_chat_task(client, value, node)
await download_chat_task(client, value, value.node)
except Exception as e:
logger.warning(f"Download {key} error: {e}")
finally:
Expand Down Expand Up @@ -590,12 +607,13 @@ async def stop_server(client: pyrogram.Client):
def main():
"""Main function of the downloader."""
tasks = []
client = pyrogram.Client(
client = HookClient(
"media_downloader",
api_id=app.api_id,
api_hash=app.api_hash,
proxy=app.proxy,
workdir=app.session_file_path,
start_timeout=app.start_timeout,
)
try:
app.pre_run()
Expand Down
Loading