Skip to content

Commit

Permalink
add tools to clean orphaned files and export music library
Browse files Browse the repository at this point in the history
  • Loading branch information
double16 committed Sep 3, 2024
1 parent 6317144 commit 73ad82f
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ RUN chmod 0644 /etc/logrotate.d/dvr &&\
ln -s /usr/local/share/dvrprocess/smart-comcut.py /usr/local/bin/ &&\
ln -s /usr/local/share/dvrprocess/tvshow-summary.py /usr/local/bin/ &&\
ln -s /usr/local/share/dvrprocess/tvshow-suspicious.py /usr/local/bin/ &&\
ln -s /usr/local/share/dvrprocess/clean_orphaned_files.py /usr/local/bin/ &&\
chmod +x /usr/local/bin/* /usr/sbin/sendmail /usr/bin/hwaccel-drivers* && \
ln -s /usr/bin/hwaccel-drivers-wrapper /etc/cron.daily/1hwaccel-drivers &&\
systemctl enable cron &&\
Expand Down
51 changes: 51 additions & 0 deletions dvrprocess/clean_orphaned_files.py
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)
48 changes: 48 additions & 0 deletions dvrprocess/export_music_library.py
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()

0 comments on commit 73ad82f

Please sign in to comment.