Skip to content

Commit

Permalink
Added support for generating file sharing link (e.g. dropbox folder) …
Browse files Browse the repository at this point in the history
…as part of finalise
  • Loading branch information
beveradb committed Sep 17, 2024
1 parent 60d347e commit d0276aa
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
44 changes: 28 additions & 16 deletions karaoke_prep/karaoke_finalise/karaoke_finalise.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from google.auth.transport.requests import Request
from googleapiclient.http import MediaFileUpload
import subprocess
import time


class KaraokeFinalise:
Expand All @@ -29,6 +30,7 @@ def __init__(
enable_txt=False,
brand_prefix=None,
organised_dir=None,
organised_dir_rclone_root=None,
public_share_dir=None,
youtube_client_secrets_file=None,
youtube_description_file=None,
Expand Down Expand Up @@ -69,6 +71,8 @@ def __init__(

self.brand_prefix = brand_prefix
self.organised_dir = organised_dir
self.organised_dir_rclone_root = organised_dir_rclone_root

self.public_share_dir = public_share_dir
self.youtube_client_secrets_file = youtube_client_secrets_file
self.youtube_description_file = youtube_description_file
Expand Down Expand Up @@ -107,7 +111,9 @@ def __init__(

self.youtube_url = None
self.brand_code = None
self.new_brand_code_dir = None
self.new_brand_code_dir_path = None
self.brand_code_dir_sharing_link = None

def check_input_files_exist(self, base_name, with_vocals_file, instrumental_audio_file):
self.logger.info(f"Checking required input files exist...")
Expand Down Expand Up @@ -638,7 +644,8 @@ def create_txt_zip_file(self, input_files, output_files):
def move_files_to_brand_code_folder(self, brand_code, artist, title, output_files):
self.logger.info(f"Moving files to new brand-prefixed directory...")

self.new_brand_code_dir_path = os.path.join(self.organised_dir, f"{brand_code} - {artist} - {title}")
self.new_brand_code_dir = f"{brand_code} - {artist} - {title}"
self.new_brand_code_dir_path = os.path.join(self.organised_dir, self.new_brand_code_dir)

self.prompt_user_confirmation_or_raise_exception(
f"Move files to new brand-prefixed directory {self.new_brand_code_dir_path} and delete current dir?",
Expand Down Expand Up @@ -715,23 +722,30 @@ def post_discord_notification(self):
discord_message = f"New upload: {self.youtube_url}"
self.post_discord_message(discord_message, self.discord_webhook_url)

def get_dropbox_sharing_link(self):
self.logger.info(f"Getting Dropbox sharing link for new brand code directory...")
def generate_organised_folder_sharing_link(self):
self.logger.info(f"Getting Organised Folder sharing link for new brand code directory...")

rclone_dest = f"{self.organised_dir_rclone_root}/{self.new_brand_code_dir}"
rclone_link_cmd = f"rclone link {shlex.quote(rclone_dest)}"

if self.dry_run:
self.logger.info(f"DRY RUN: Would get Dropbox sharing link for: {self.new_brand_code_dir_path}")
return "https://www.dropbox.com/sh/dryrun_link"
self.logger.info(f"DRY RUN: Would get sharing link with: {rclone_link_cmd}")
return "https://file-sharing-service.com/example"

rclone_link_cmd = f"rclone link '{self.rclone_destination}/{os.path.basename(self.new_brand_code_dir_path)}'"
# Add a 5-second delay to allow dropbox to index the folder before generating a link
self.logger.info("Waiting 5 seconds before generating link...")
time.sleep(5)

try:
self.logger.info(f"Running command: {rclone_link_cmd}")
result = subprocess.run(rclone_link_cmd, shell=True, check=True, capture_output=True, text=True)
dropbox_link = result.stdout.strip()
self.logger.info(f"Got Dropbox sharing link: {dropbox_link}")
return dropbox_link
self.brand_code_dir_sharing_link = result.stdout.strip()
self.logger.info(f"Got organised folder sharing link: {self.brand_code_dir_sharing_link}")
except subprocess.CalledProcessError as e:
self.logger.error(f"Failed to get Dropbox sharing link: {e}")
return None
self.logger.error(f"Failed to get organised folder sharing link. Exit code: {e.returncode}")
self.logger.error(f"Command output (stdout): {e.stdout}")
self.logger.error(f"Command output (stderr): {e.stderr}")
self.logger.error(f"Full exception: {e}")

def execute_optional_features(self, artist, title, base_name, input_files, output_files):
self.logger.info(f"Executing optional features...")
Expand Down Expand Up @@ -761,6 +775,8 @@ def execute_optional_features(self, artist, title, base_name, input_files, outpu

self.move_files_to_brand_code_folder(self.brand_code, artist, title, output_files)

self.generate_organised_folder_sharing_link()

def process(self):
if self.dry_run:
self.logger.warning("Dry run enabled. No actions will be performed.")
Expand All @@ -786,10 +802,6 @@ def process(self):

self.execute_optional_features(artist, title, base_name, input_files, output_files)

dropbox_sharing_link = None
if self.folder_organisation_enabled and self.public_share_rclone_enabled:
dropbox_sharing_link = self.get_dropbox_sharing_link()

result = {
"artist": artist,
"title": title,
Expand All @@ -799,7 +811,7 @@ def process(self):
"youtube_url": self.youtube_url,
"brand_code": self.brand_code,
"new_brand_code_dir_path": self.new_brand_code_dir_path,
"dropbox_sharing_link": dropbox_sharing_link,
"brand_code_dir_sharing_link": self.brand_code_dir_sharing_link,
}

if self.enable_cdg:
Expand Down
9 changes: 8 additions & 1 deletion karaoke_prep/utils/finalise_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ def main():
help="Optional: Target directory where the processed folder will be moved after finalisation. Example: --organised_dir='/path/to/Tracks-Organized'",
)

parser.add_argument(
"--organised_dir_rclone_root",
default=None,
help="Optional: Rclone path which maps to your organised_dir, to generate a sharing link after adding files to it. Example: --organised_dir_rclone_root='andrewdropbox:Media/Karaoke/Tracks-Organized'",
)

parser.add_argument(
"--public_share_dir",
default=None,
Expand Down Expand Up @@ -118,6 +124,7 @@ def main():
enable_txt=args.enable_txt,
brand_prefix=args.brand_prefix,
organised_dir=args.organised_dir,
organised_dir_rclone_root=args.organised_dir_rclone_root,
public_share_dir=args.public_share_dir,
youtube_client_secrets_file=args.youtube_client_secrets_file,
youtube_description_file=args.youtube_description_file,
Expand Down Expand Up @@ -148,7 +155,7 @@ def main():
logger.info(f" New Brand Code Directory: {track['new_brand_code_dir_path']}")

logger.info(f" YouTube URL: {track['youtube_url']}")
logger.info(f" Dropbox Sharing Link: {track['dropbox_sharing_link']}")
logger.info(f" Folder Sharing Link: {track['brand_code_dir_sharing_link']}")


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "karaoke-prep"
version = "0.23.1"
version = "0.23.2"
description = "Prepare for karaoke video creation, by downloading audio and lyrics for a specified song or playlist from youtube and separating audio stems. After syncing, finalise the video with a title screen!"
authors = ["Andrew Beveridge <andrew@beveridge.uk>"]
license = "MIT"
Expand Down

0 comments on commit d0276aa

Please sign in to comment.