Skip to content

Instantly share code, notes, and snippets.

@baydam
Last active April 6, 2022 03:22
Show Gist options
  • Save baydam/e78c0cc1e55c77bcf58b to your computer and use it in GitHub Desktop.
Save baydam/e78c0cc1e55c77bcf58b to your computer and use it in GitHub Desktop.
This script will be execute when medias (movies and tv shows) download are complete. He move the movies to movies folders, create new folder in tv show directory with a name of show and copy it in.
#!/usr/bin/python
import os, re, shutil
from os.path import join, expanduser, dirname, basename
# Defines global variables
media_extensions = ['avi', 'mkv', 'mp4'] # Extensions for many videos file
tv_show_directory = expanduser("/mnt/D40E9CD00E9CACCC/Series/") # Tv Show path
movies_directory = expanduser("/mnt/D40E9CD00E9CACCC/Movies/") # Movies path
download_directory = expanduser("~/Downloads/") # Path for downloaded files
def parseFileName(filename):
# parse filename to determine what kind of media file is
# if is not Tv Show
if not re.search("S0", basename(filename)):
# Most of the movies file are ripped and these name contain "Rip"
# This need to be improved later
if re.search("(\w+rip|[720]{3})", basename(filename), re.I):
# If the file is a movie file
moveToMoviesFolder(filename)
else:
# If the file is Tv Show episode
moveToTvShowsFolder(filename)
def moveToMoviesFolder(movies):
try:
# Move the movie from download path to movies path
shutil.move(movies, movies_directory)
# Return to clean download directory
cleanDownloadFolder(dirname(movies) + "/")
except OSError:
print("Impossible de deplacer le fichier")
def moveToTvShowsFolder(episodes):
# This line below tranlate "[www.Cpasbien.pe] Under.The.Dome.S02E12.FRENCH.HDTV.XviD-RNT"
folder = re.findall("[\w'.]+S\d\d", basename(episodes))[0].replace(".", " ").title()
# To "Under The Dome S02"
folder_path = join(tv_show_directory, folder)
# And create directory
# If folder named "Under The Dome S02" doesn't exist
if not os.path.exists(folder_path):
os.mkdir(folder_path, 0775)
try:
# Move the episode from download path to Tv Show path
shutil.move(episodes, folder_path)
# Return to clean download directory
cleanDownloadFolder(dirname(episodes) + "/")
except OSError:
print("Impossible de deplacer le fichier")
def cleanDownloadFolder(path):
# Verify if folder that contain media file before it's moved
# Is different of the download folder
if path != download_directory:
try:
# Remove folder and all of those content
shutil.rmtree(path)
except OSError:
print("Impossible de supprimer le dossier")
def main():
# Browser in download directory recursively
for root, dirs, files in os.walk(download_directory):
# To get list of files
for name in files:
# If file is movies or tv show episodes
extension = os.path.splitext(name)[1][1:].strip()
if extension in media_extensions:
parseFileName(join(root, name))
if __name__ == "__main__":
main()
@baydam
Copy link
Author

baydam commented Apr 28, 2015

Ça a l'air simple! Merci encore tu me donne tellement d'idées là

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment