Files
escripts/podfetch
Eugene Amos 65714a45d8 qbittorrent
> updating wiki
2023-11-29 00:36:50 -08:00
..
2023-07-30 15:32:48 -07:00
2023-07-30 15:32:48 -07:00
2023-08-09 13:32:07 -07:00
2023-11-29 00:36:50 -08:00

PodFetch Folder Monitor

This will monitor a folder and when a new file is created. It will change the .mp3 name to the name of the parent folder.
This script uses Python and a library in Python called watchdog

Table of Contents
  1. PIP
  2. Folder Structure
  3. Script
  4. Run
  5. Testing
  6. Automation

01. PIP

You will need pip installed on your linux mint system. PIP is the package manager for Python.

  • See if PIP is already installed. Open a terminal and type: pip -V

  • If not, install it. python get-pip.py

  • Once installed, install watchdog pip install watchdog

Note: You may need to use pip3

02. Folder Structure

Create a new folder called scripts in your home folder. This is where all your scripts will go. You can do this is two ways.

  1. In your file manager go to ~/scripts then right click on an open space and select Open in terminal
  2. In your terminal type: cd ~/scripts

When done it should look like this: /home/[username]/scripts

03. Script

podcast_folder_monitor.py
#!/usr/bin/python3

import os
import time
import shutil

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

# Replace this with the folder path you want to monitor
folder_to_monitor = "/some/folder/to/monitor"  

class MyHandler(FileSystemEventHandler):
    def on_created(self, event):
        if event.is_directory:
            folder_path = event.src_path
            self.rename_mp3_files(folder_path)

    def rename_mp3_files(self, folder_path):
        folder_name = os.path.basename(folder_path)
        for filename in os.listdir(folder_path):
            if filename.lower().endswith(".mp3"):
                mp3_path = os.path.join(folder_path, filename)
                new_mp3_path = os.path.join(folder_path, folder_name + ".mp3")
                os.rename(mp3_path, new_mp3_path)
                print(f"Renamed {filename} to {folder_name}.mp3")

if __name__ == "__main__":
    event_handler = MyHandler()
    observer = Observer()
    observer.schedule(event_handler, folder_to_monitor, recursive=True)
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()

    observer.join()

Make sure to replace /some/folder/to/monitor with the actual path of the folder you want to monitor.

Note: leave the quotation marks

Save this script as podcast_folder_monitor.py in the scripts folder. Next we need to change the permissions of this file so that the system can read it.

  • Right click on podcast_folder_monitor.py then Properties.
  • Click on Permissions tab at top.
  • Make sure Owner & Group reflect your username with Read and Write access to all.
  • Check the Execute box to allow executing the file as a program.
  • Close out the window.

04. Run

Now we need to test and run the script.

Make sure you terminal is open to the location of the podcast_folder_monitor.py file. Should be ~/scripts

Normally to run a Python script will would use python3 podcast_folder_monitor.py

Note: Will still work.

But because we have shebang set in the script we can just use ./podcast_folder_monitor.py

You should now see the blinking curser in the terminal indicating that the script is running and monitoring the folder for a new file.

testing

05. Testing

To test the script copy and paste one of your podcast folder that includes the podcast.mp3 file back into the folder. The script should notice the change and rename the .mp3 to the name of the parent folder.

testing

If everything worked out okay we can move to the next step.

06. Automation

We can make the script auto run in the background. Just like Sonarr, Radarr, etc...

To get started we need to crate a new file in the systemd location.

  • Go to /etc/systemd/system
  • Right click in a empty space and select Open as Root. Root access is needed to create or edit in this directory.
  • Create a new file called podfetch.service
  • Use this code inside the podfetch.service file.
podfetch.service
[Unit]
Description=Podcast Folder Monitor Service
Wants=network-online.target
After=network-online.target

# Change [username] with your own.
[Service]
ExecStart=/usr/bin/python3 /home/[username]/scripts/podcast_folder_monitor.py
WorkingDirectory=/home/[username]/scripts
Restart=always
User=[username]

[Install]
WantedBy=multi-user.target

Note: Make sure you change [username] with your own..

Now we need to enable the service file.

# Reload the systemctl daemon
sudo systemctl daemon-reload

# Enable the podfetch service
sudo systemctl enable podfetch.service

# Starting the podfetch service
sudo systemctl start podfetch

# Checking status
sudo systemctl status podfetch

Congratulation your service is auto-running and listing for new podcast folders