diff --git a/.gitignore b/.gitignore index f9dba96..a045b88 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ # Config File config.yml +# Downloaded Files +downloads + # IDE Files and Environment .idea __pycache__ diff --git a/discord_bot.py b/discord_bot.py index c5dc265..ef9ea72 100644 --- a/discord_bot.py +++ b/discord_bot.py @@ -32,7 +32,7 @@ async def on_message(message): if attachment.filename.endswith(('.jpg', '.png', '.jpeg')): author = message.author.display_name message = message.content - image_file_path = utils.download_file_from_url(attachment.url, + image_file_path = utils.download_file_from_url(sub_num, attachment.url, attachment.filename) line_notify.send_image_message(sub_num, f"{author}: {message}", image_file_path) else: diff --git a/line_bot.py b/line_bot.py index f78e85b..6b4ef1d 100644 --- a/line_bot.py +++ b/line_bot.py @@ -73,7 +73,7 @@ def handle_image(event): author_image = line_bot_api.get_group_member_profile(event.source.group_id, event.source.user_id).picture_url source = line_bot_api.get_message_content(event.message.id) - file_path = utils.download_file_from_line(source, event.message.type) + file_path = utils.download_file_from_line(sub_num, source, event.message.type) discord_webhook = SyncWebhook.from_url(config.get(f'discord_channel_webhook_{sub_num}')) discord_webhook.send(file=File(file_path), username=f"{author} - (Line訊息)", avatar_url=author_image) @@ -92,7 +92,7 @@ def handle_video(event): author_image = line_bot_api.get_group_member_profile(event.source.group_id, event.source.user_id).picture_url source = line_bot_api.get_message_content(event.message.id) - file_path = utils.download_file_from_line(source, event.message.type) + file_path = utils.download_file_from_line(sub_num, source, event.message.type) discord_webhook = SyncWebhook.from_url(config.get(f'discord_channel_webhook_{sub_num}')) discord_webhook.send(file=File(file_path), username=f"{author} - (Line訊息)", avatar_url=author_image) diff --git a/line_notify.py b/line_notify.py index 6b24c39..3874658 100644 --- a/line_notify.py +++ b/line_notify.py @@ -30,7 +30,8 @@ def send_image_message(sub_num, message, image_path): token = config.get(f'line_notify_token_{sub_num}') headers = {"Authorization": "Bearer " + token} data = {'message': message} - with open(image_path, 'rb') as image: - files = {'imageFile': image} + with open(image_path, 'rb') as f: + image = f.read() + files = {'imageFile': image} requests.post("https://notify-api.line.me/api/notify", headers=headers, data=data, files=files, timeout=5) diff --git a/utilities.py b/utilities.py index 3d098da..59cc1a8 100644 --- a/utilities.py +++ b/utilities.py @@ -1,5 +1,6 @@ """This python file will handle some extra functions.""" import datetime +import os import sys from os.path import exists from typing import List @@ -99,26 +100,32 @@ def get_discord_webhook_bot_id(webhook_url): return int(webhook_url.split('/')[-2]) -def download_file_from_url(url, filename): +def download_file_from_url(sub_num, url, filename): """Download file from url. Use to download any files from discord. + :param int sub_num: Subscribed sync channels num. :param url: url of file :param filename: filename of file :return str: file path """ - file_path = './' + datetime.datetime.now().strftime('%Y%m%d%H%M%S%f') + '_' + filename - r = requests.get(url, allow_redirects=True) - open(file_path, 'wb').write(r.content) + r = requests.get(url, allow_redirects=True, timeout=5) + path = f'./downloads/{sub_num}' + if not os.path.exists(path): + os.makedirs(path) + file_path = f'{path}/{datetime.datetime.now().strftime("%Y%m%d%H%M%S%f")}_{filename}' + with open(file_path, 'wb') as fd: + fd.write(r.content) return file_path -def download_file_from_line(source, message_type, file_name=None): +def download_file_from_line(sub_num, source, message_type, file_name=None): """Get file binary and save them in PC. Use to download files from LINE. + :param int sub_num: Subscribed sync channels num. :param source: source of file that given by LINE :param message_type: message type from line :param file_name: file name of file @@ -128,8 +135,11 @@ def download_file_from_line(source, message_type, file_name=None): 'image': 'jpg', 'video': 'mp4', } - file_path = datetime.datetime.now().strftime('%Y%m%d%H%M%S%f') + '.' + file_type.get( - message_type) + path = f'./downloads/{sub_num}' + if not os.path.exists(path): + os.makedirs(path) + file_path = \ + f'{path}/{datetime.datetime.now().strftime("%Y%m%d%H%M%S%f")}.{file_type.get(message_type)}' with open(file_path, 'wb') as fd: for chunk in source.iter_content(): fd.write(chunk)