Add files via upload
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
import requests
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
# Configuration file path (update the path to where your mattermost_config.txt is located)
|
||||
config_file_path = '/home/motion/drives/local-2tb/sunrise-scripts/mattermost_config.txt'
|
||||
|
||||
# Function to read configuration from the config file
|
||||
def read_config(config_file_path):
|
||||
config = {}
|
||||
try:
|
||||
with open(config_file_path, 'r') as f:
|
||||
for line in f:
|
||||
# Ignore comments and empty lines
|
||||
if line.strip() and not line.startswith('#'):
|
||||
key, value = line.strip().split('=', 1)
|
||||
config[key.strip()] = value.strip()
|
||||
except FileNotFoundError:
|
||||
print(f"Error: The configuration file {config_file_path} was not found.")
|
||||
exit(1)
|
||||
except Exception as e:
|
||||
print(f"Error reading the configuration file: {e}")
|
||||
exit(1)
|
||||
return config
|
||||
|
||||
# Read configuration from the file
|
||||
config = read_config(config_file_path)
|
||||
|
||||
# Get the values from the configuration file
|
||||
mattermost_url = config.get('mattermost_url')
|
||||
access_token = config.get('access_token')
|
||||
channel_id = config.get('channel_id')
|
||||
|
||||
# Check if all required values are provided
|
||||
if not mattermost_url or not access_token or not channel_id:
|
||||
print("Error: Missing one or more configuration values.")
|
||||
exit(1)
|
||||
|
||||
# Get today's date
|
||||
today = datetime.now()
|
||||
today_date_str = today.strftime("%Y-%m-%d") # e.g., "2024-11-05"
|
||||
today_month_str = today.strftime("%Y-%m") # e.g., "2024-11"
|
||||
|
||||
# Set the full folder path for today's video
|
||||
base_video_folder = "/home/motion/drives/local-2tb/movies/sunrise" # Correct base folder for videos
|
||||
output_dir = os.path.join(base_video_folder, today_month_str, "sunrise-only") # Subfolder for final videos
|
||||
|
||||
# Check if the directory exists
|
||||
if not os.path.isdir(output_dir):
|
||||
print(f"Error: The directory {output_dir} does not exist. The video may not have been created yet.")
|
||||
exit(1)
|
||||
|
||||
# Find the sunrise video file for today (e.g., 2024-11-05-sunrise-10s.mp4)
|
||||
final_video_file = os.path.join(output_dir, f"{today_date_str}-sunrise-10s.mp4")
|
||||
|
||||
# Debugging: print the full path of the video file to make sure it's correct
|
||||
print(f"Looking for video file: {final_video_file}")
|
||||
|
||||
# Check if the video file exists
|
||||
if os.path.isfile(final_video_file):
|
||||
# Upload the video file
|
||||
with open(final_video_file, 'rb') as f:
|
||||
response = requests.post(
|
||||
f"{mattermost_url}/api/v4/files",
|
||||
headers={'Authorization': f'Bearer {access_token}'},
|
||||
files={'files': f},
|
||||
data={'channel_id': channel_id}
|
||||
)
|
||||
|
||||
if response.status_code == 201:
|
||||
print(f"Successfully uploaded {final_video_file}")
|
||||
|
||||
# Get the file ID from the response
|
||||
file_id = response.json().get('file_infos', [{}])[0].get('id')
|
||||
|
||||
# Send a message with the file reference
|
||||
message_data = {
|
||||
'channel_id': channel_id,
|
||||
'message': f"Here is a video of the sunrise for {today_date_str}",
|
||||
'file_ids': [file_id] # Attach the uploaded file
|
||||
}
|
||||
|
||||
message_response = requests.post(
|
||||
f"{mattermost_url}/api/v4/posts",
|
||||
headers={'Authorization': f'Bearer {access_token}'},
|
||||
json=message_data
|
||||
)
|
||||
|
||||
if message_response.status_code == 201:
|
||||
print("Message posted successfully.")
|
||||
else:
|
||||
print(f"Failed to post message: {message_response.text}")
|
||||
else:
|
||||
print(f"Failed to upload {final_video_file}: {response.text}")
|
||||
else:
|
||||
print(f"No video file found at {final_video_file}.")
|
||||
Reference in New Issue
Block a user