Skip to content

Commit

Permalink
fix: fix rclone error (#72)
Browse files Browse the repository at this point in the history
## 修复
* #69 

## 新功能
* 支持docker下rclone调用

## Fix
* #69 

## Feat
* Support rclone call under docker
  • Loading branch information
tangyoha authored May 15, 2023
1 parent c77bac6 commit 1baa88d
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 20 deletions.
13 changes: 11 additions & 2 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:
outputs:
compile_image_exists: ${{ steps.check-image.outputs.exists }}
requirements_modified: ${{ steps.check-requirements.outputs.modified }}
check-dockerfile: ${{ steps.check-dockerfile.outputs.dockerfile_modified }}

steps:
- name: Checkout
Expand All @@ -30,10 +31,17 @@ jobs:
- name: Check if requirements.txt has been modified
id: check-requirements
run: |
MODIFIED=$(git diff --name-only HEAD~1 HEAD | grep -w 'requirements.txt' && echo "true" || echo "false")
MODIFIED=$(git diff --name-only HEAD~1 HEAD | grep -w 'requirements.txt' > /dev/null && echo "true" || echo "false")
echo "modified=$MODIFIED" >> $GITHUB_ENV
echo "modified=$MODIFIED" >> $GITHUB_OUTPUT
- name: Check if Dockerfile has been modified
id: check-dockerfile
run: |
DOCKERFILE_MODIFIED=$(git diff --name-only HEAD~1 HEAD | grep -w 'Dockerfile' > /dev/null && echo "true" || echo "false")
echo "dockerfile_modified=$DOCKERFILE_MODIFIED" >> $GITHUB_ENV
echo "dockerfile_modified=$DOCKERFILE_MODIFIED" >> $GITHUB_OUTPUT
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

Expand All @@ -48,9 +56,10 @@ jobs:
run: |
echo ${{ steps.check-image.outputs.exists }}
echo ${{ steps.check-requirements.outputs.modified }}
echo ${{ steps.check-dockerfile.outputs.dockerfile_modified }}
- name: Build and push compile-image
if: ${{ !(steps.check-image.outputs.exists == 'true' && steps.check-requirements.outputs.modified == 'false') }}
if: ${{ !(steps.check-image.outputs.exists == 'true' && steps.check-requirements.outputs.modified == 'false' && steps.check-dockerfile.outputs.dockerfile_modified == 'false') }}
uses: docker/build-push-action@v4
with:
context: .
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,5 @@ local_test/
TODO.md
log/
temp/
config.yaml
data.yaml
12 changes: 11 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,21 @@ RUN apk add --no-cache --virtual .build-deps gcc musl-dev \
&& pip install --trusted-host pypi.python.org -r requirements.txt \
&& apk del .build-deps && rm -rf requirements.txt

# install rclone
RUN apk add --no-cache ca-certificates && \
wget https://downloads.rclone.org/rclone-current-linux-amd64.zip && \
unzip rclone-current-linux-amd64.zip && \
mv rclone-*-linux-amd64/rclone /app/rclone && \
rm -rf rclone-*-linux-amd64 && \
rm -rf rclone-current-linux-amd64.zip

FROM python:3.11.2-alpine As runtime-image

WORKDIR /app

COPY --from=tangyoha/telegram_media_downloader_compile:latest /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
COPY --from=tangyoha/telegram_media_downloader_compile:master_fix_rclone_error /app/rclone /app/rclone/rclone

COPY --from=tangyoha/telegram_media_downloader_compile:master_fix_rclone_error /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages

COPY config.yaml data.yaml setup.py media_downloader.py /app/
COPY module /app/module
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,4 @@ Help us keep Telegram Media Downloader open and inclusive. Please read and follo
<p>
<img alt="Code style: black" style="width:30%" src="./screenshot/alipay.JPG">
<img alt="Code style: black" style="width:30%" src="./screenshot/wechat.JPG">
</p>
</p>
2 changes: 1 addition & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,4 @@ proxy:
<p>
<img alt="Code style: black" style="width:30%" src="./screenshot/alipay.JPG">
<img alt="Code style: black" style="width:30%" src="./screenshot/wechat.JPG">
</p>
</p>
6 changes: 6 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ services:
ports:
# Here is what you need to edit
- "5000:5000"
#environment:
# - http_proxy=http://192.168.101.30:10811
# - https_proxy=http://192.168.101.30:10811
volumes:
# Here is what you need to edit
- "./downloads/:/app/downloads/"

# Rclone
- "$HOME/.config/rclone/:$HOME/.config/rclone/"

# The following is what you don't need to edit
- "./config.yaml:/app/config.yaml"
- "./data.yaml:/app/data.yaml"
Expand Down
20 changes: 12 additions & 8 deletions media_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,20 +287,21 @@ async def download_task(
download_status,
)

# rclone upload
if (
not node.upload_telegram_chat_id
and download_status is DownloadStatus.SuccessDownload
):
if await app.upload_file(file_name):
node.upload_success_count += 1

await report_bot_download_status(
node.bot,
node,
download_status,
file_size,
)

# rclone upload
if (
not node.upload_telegram_chat_id
and download_status is DownloadStatus.SuccessDownload
):
await app.upload_file(file_name)


# pylint: disable = R0915,R0914

Expand Down Expand Up @@ -480,7 +481,10 @@ async def worker(client: pyrogram.client.Client):
message = item[0]
node: TaskNode = item[1]

await download_task(client, message, node)
if node.client:
await download_task(node.client, message, node)
else:
await download_task(client, message, node)
except Exception as e:
logger.exception(f"{e}")

Expand Down
2 changes: 2 additions & 0 deletions module/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ def __init__(
self.failed_forward_task: int = 0
self.skip_forward_task: int = 0
self.is_running: bool = False
self.client = None
self.upload_success_count: int = 0

def is_finish(self):
"""If is finish"""
Expand Down
5 changes: 4 additions & 1 deletion module/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ async def direct_download(
chat_id: Union[str, int],
message: pyrogram.types.Message,
download_message: pyrogram.types.Message,
client: pyrogram.Client = None,
):
"""Direct Download"""

Expand All @@ -368,6 +369,8 @@ async def direct_download(
task_id=_bot.gen_task_id(),
)

node.client = client

_bot.add_task_node(node)

await _bot.add_download_task(
Expand All @@ -393,7 +396,7 @@ async def download_forward_media(
"""

if message.media and getattr(message, message.media.value):
await direct_download(_bot, message.from_user.id, message, message)
await direct_download(_bot, message.from_user.id, message, message, client)
return

await client.send_message(
Expand Down
31 changes: 25 additions & 6 deletions module/cloud_drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ def zip_file(local_file_path: str) -> str:
@staticmethod
async def rclone_upload_file(
drive_config: CloudDriveConfig, save_path: str, local_file_path: str
):
) -> bool:
"""Use Rclone upload file"""
upload_status: bool = False
try:
remote_dir = (
drive_config.remote_dir
Expand Down Expand Up @@ -125,19 +126,24 @@ async def rclone_upload_file(
os.remove(local_file_path)
if drive_config.before_upload_file_zip:
os.remove(zip_file_path)
upload_status = True

await proc.wait()
except Exception as e:
logger.error(f"{e.__class__} {e}")
return False

return upload_status

@staticmethod
def aligo_upload_file(
drive_config: CloudDriveConfig, save_path: str, local_file_path: str
):
"""aliyun upload file"""
upload_status: bool = False
if not drive_config.aligo:
logger.warning("please config aligo! see README.md")
return
return False

try:
remote_dir = (
Expand Down Expand Up @@ -174,13 +180,18 @@ def aligo_upload_file(
if drive_config.before_upload_file_zip:
os.remove(zip_file_path)

upload_status = True

except Exception as e:
logger.error(f"{e.__class__} {e}")
return False

return upload_status

@staticmethod
async def upload_file(
drive_config: CloudDriveConfig, save_path: str, local_file_path: str
):
) -> bool:
"""Upload file
Parameters
----------
Expand All @@ -192,13 +203,21 @@ async def upload_file(
local_file_path: str
Local file path
Returns
-------
bool
True or False
"""
if not drive_config.enable_upload_file:
return
return False

ret: bool = False
if drive_config.upload_adapter == "rclone":
await CloudDrive.rclone_upload_file(
ret = await CloudDrive.rclone_upload_file(
drive_config, save_path, local_file_path
)
elif drive_config.upload_adapter == "aligo":
CloudDrive.aligo_upload_file(drive_config, save_path, local_file_path)
ret = CloudDrive.aligo_upload_file(drive_config, save_path, local_file_path)

return ret
9 changes: 9 additions & 0 deletions module/pyrogram_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,14 @@ async def report_bot_status(
f"└─ ⏩ {_t('Skipped')}: {node.skip_forward_task}\n"
)

upload_msg_detail_str: str = ""

if node.upload_success_count:
upload_msg_detail_str = (
f"\n📥 {_t('Upload')}\n"
f"└─ ✅ {_t('Success')}: {node.upload_success_count}\n"
)

download_result_str = ""
download_result = get_download_result()
if node.chat_id in download_result:
Expand Down Expand Up @@ -441,6 +449,7 @@ async def report_bot_status(
f"├─ ❌ {_t('Failed')}: {node.failed_download_task}\n"
f"└─ ⏩ {_t('Skipped')}: {node.skip_download_task}\n"
f"{node.forward_msg_detail_str}"
f"{upload_msg_detail_str}"
f"{download_result_str}\n```"
)

Expand Down

0 comments on commit 1baa88d

Please sign in to comment.