-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tools to clean orphaned files and export music library
- Loading branch information
Showing
3 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import os | ||
import argparse | ||
|
||
import common | ||
|
||
|
||
# Define the metadata extensions | ||
metadata_extensions = [".bak.edl", ".edl", ".mkv.ini", ".comskip.ini"] | ||
|
||
def find_orphaned_files(root_dir, dry_run=False): | ||
# Walk through all directories and files within the root directory | ||
for dirpath, dirnames, filenames in os.walk(root_dir): | ||
removed_files = False | ||
for filename in filenames: | ||
# Check if the file is a metadata file | ||
for ext in metadata_extensions: | ||
if filename.endswith(ext): | ||
# Construct the base filename by removing the metadata extension | ||
base_filename = filename[:-len(ext)] + ".mkv" | ||
if base_filename.endswith(".bak.mkv"): | ||
# special case of .bak.edl with .edl removed | ||
continue | ||
# Check if the corresponding .mkv file exists | ||
if not os.path.exists(os.path.join(dirpath, base_filename)): | ||
# If the .mkv file does not exist, it's an orphaned metadata file | ||
orphan_file = os.path.join(dirpath, filename) | ||
removed_files = True | ||
if dry_run: | ||
print(f"[DRY RUN] Would remove: {orphan_file}") | ||
else: | ||
os.remove(orphan_file) | ||
print(f"Removed: {orphan_file}") | ||
# After processing files, check if the directory is empty | ||
if not os.listdir(dirpath) and removed_files: | ||
if dry_run: | ||
print(f"[DRY RUN] Would remove empty directory: {dirpath}") | ||
else: | ||
os.rmdir(dirpath) | ||
print(f"Removed empty directory: {dirpath}") | ||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description="Remove orphaned metadata files.") | ||
parser.add_argument("--root_dir", help="The root directory of your media files.") | ||
parser.add_argument("--dry-run", action="store_true", help="Print actions without removing files.") | ||
args = parser.parse_args() | ||
|
||
if args.root_dir: | ||
find_orphaned_files(args.root_dir, dry_run=args.dry_run) | ||
else: | ||
for root_dir in common.get_media_roots(): | ||
find_orphaned_files(root_dir, dry_run=args.dry_run) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import json | ||
from plexapi.server import PlexServer | ||
import argparse | ||
|
||
import common | ||
|
||
|
||
def get_music_library(plex, token=None): | ||
music_data = [] | ||
|
||
# Fetch music library | ||
music_section = plex.library.section('Music') | ||
|
||
for album in music_section.albums(): | ||
artist_name = album.artist().title | ||
album_title = album.title | ||
album_year = album.year | ||
|
||
for track in album.tracks(): | ||
track_data = { | ||
"artist": artist_name, | ||
"album": album_title, | ||
"year": album_year, | ||
"title": track.title | ||
} | ||
music_data.append(track_data) | ||
|
||
return music_data | ||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description="Extract music library from Plex Media Server.") | ||
parser.add_argument("--token", type=str, help="Optional Plex API token") | ||
parser.add_argument("--filename", type=str, default="music_library.json", help="Output JSON filename") | ||
parser.add_argument("--url", type=str, default=common.get_plex_url(), help="Base URL of the Plex Media Server") | ||
args = parser.parse_args() | ||
|
||
plex = PlexServer(args.url, args.token) | ||
|
||
music_data = get_music_library(plex, args.token) | ||
|
||
# Write to the specified JSON file | ||
with open(args.filename, 'w') as f: | ||
json.dump(music_data, f, indent=4) | ||
|
||
print(f"Music library exported to {args.filename}") | ||
|
||
if __name__ == "__main__": | ||
main() |