-
Notifications
You must be signed in to change notification settings - Fork 0
/
playlist_sync BARD.py
81 lines (64 loc) · 3.73 KB
/
playlist_sync BARD.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
import shutil
# Define paths using Docker environment variables
base_playlist_folder = os.environ.get('BASE_PLAYLIST_FOLDER', '/mnt/media/Music/00_Exported_Playlists/')
new_playlist_folder = os.environ.get('NEW_PLAYLIST_FOLDER', '/mnt/media/MusicBee/Music_Sync/')
new_music_folder = os.environ.get('NEW_MUSIC_FOLDER', '/mnt/media/MusicBee/Music_Sync/')
base_music_source = os.environ.get('BASE_MUSIC_SOURCE', '/mnt/media/Music/')
# Remove all contents within the folder
shutil.rmtree(new_music_folder)
print("Deleted Sync Folder and old Links.")
# Recreate an empty folder
os.mkdir(new_music_folder)
print("Recreated Sync Folder.")
import os
import shutil
# Define paths using Docker environment variables
base_playlist_folder = os.environ.get('BASE_PLAYLIST_FOLDER', '/mnt/media/Music/00_Exported_Playlists/')
new_playlist_folder = os.environ.get('NEW_PLAYLIST_FOLDER', '/mnt/media/MusicBee/Music_Sync/')
new_music_folder = os.environ.get('NEW_MUSIC_FOLDER', '/mnt/media/MusicBee/Music_Sync/')
base_music_source = os.environ.get('BASE_MUSIC_SOURCE', '/mnt/media/Music/')
# Remove all contents within the folder
shutil.rmtree(new_music_folder)
print("Deleted Sync Folder and old Links.")
# Recreate an empty folder
os.mkdir(new_music_folder)
print("Recreated Sync Folder.")
# Iterate through all playlists in the base folder and its subfolders
for root, dirs, files in os.walk(base_playlist_folder):
for file in files:
if file.endswith('.m3u'):
# Construct paths for the current playlist
base_playlist_path = os.path.join(root, file)
new_playlist_path = os.path.join(new_playlist_folder, file)
print("Constructed Playlist Path")
# Copy the playlist file with metadata preservation
os.makedirs(os.path.dirname(new_playlist_path), exist_ok=True)
shutil.copy2(base_playlist_path, new_playlist_path)
print("Copy Playlist to new Location.")
with open(base_playlist_path, 'r') as base_playlist_file:
# Read lines and process each track
for line in base_playlist_file:
# Strip leading and trailing whitespaces, and relative folder paths
track_path = line.strip("./").strip()
# Normalize the track path to handle case sensitivity
track_path = os.path.normcase(os.path.normpath(track_path))
# Construct the full path to the track using the base music source
full_track_path = os.path.join(base_music_source, track_path)
# Construct the new music path using the new music folder
new_music_path = os.path.join(new_music_folder, track_path)
# Create new paths if they don't exist
os.makedirs(os.path.dirname(new_music_path), exist_ok=True)
print(f"Created Folder: {new_music_path}")
try:
os.link(full_track_path, new_music_path)
print(f"Created Hardlink: {full_track_path}")
except OSError as error:
print(f"Error creating hardlink for {full_track_path}: {error}")
# Optionally, add more specific handling for different error types:
# if error.errno == errno.EEXIST:
# print("File already exists, skipping...")
# else:
# print("Unexpected error occurred, skipping...")
continue # Move to the next file in the loop
print("Playlists copied and tracks hard linked successfully.")