Skip to content

Commit

Permalink
Update - files downloaded will now save and sorted in directory
Browse files Browse the repository at this point in the history
  • Loading branch information
HappyGroupHub committed Jan 25, 2023
1 parent 08b3afb commit 5ebb6dc
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Config File
config.yml

# Downloaded Files
downloads

# IDE Files and Environment
.idea
__pycache__
Expand Down
2 changes: 1 addition & 1 deletion discord_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions line_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions line_notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
24 changes: 17 additions & 7 deletions utilities.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 5ebb6dc

Please sign in to comment.