Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Rename ripped directory from ARM based on search
import sys
import imdb
import click
from os import listdir, system, rename
from os.path import isfile, join, getctime, basename
from lxml.builder import E
from lxml import etree
moviePlayer = "totem"
rootDir = "/home/mcrowe/NAS/Movies/Ripped"
topActors = 3
ia = imdb.IMDb()
def getMovies(query):
movies = ia.search_movie(query)
return movies
def getDirectories(dir):
onlydirs = [f for f in listdir(dir) if not isfile(join(dir, f)) and f.find("(") == -1]
onlydirs.sort(key=lambda x: getctime(join(dir, x)))
return onlydirs
def getFiles(root, _dir):
dir = join(root, _dir)
onlyfiles = [f for f in listdir(dir) if isfile(join(dir, f)) and f.find("(") == -1]
result = {
"main": None,
"extras": []
for file in onlyfiles:
base = file.split(".")[0]
if base.endswith("00"):
result["main"] = file
if not result["main"]:
return None
return result
def getMovieFromImdb():
search = click.prompt("Movie Search", default="")
if search == "":
return None
movies = getMovies(search)
prompts = []
print("-1: New Search")
print("0: Skip")
for movie in movies[:25]:
new_title = f"%s (%d)" % (movie.get("title"), movie.get("year") or 0)
new_title = new_title.replace("/", "-")
cast_list = []
movie_details = None
if len(prompts) < 2:
movie_details = ia.get_movie(movie.movieID)
cast = movie_details.get('cast')
if cast:
for actor in cast[:topActors]:
print(f"%d: %s %s" % (len(prompts) + 1, new_title, ",".join(cast_list)))
option = {
"dir": dir,
"new_title": new_title,
"movie": movie,
"movie_details": movie_details
result = click.prompt("Select")
if result == "-1":
print("Searching for same directory as before")
return getMovieFromImdb()
index = int(result) - 1
if index >= 0 and index < len(prompts):
return prompts[index]
return None
def showMovie(dir, file):
command = f"%s \"%s/%s/%s\"" % (moviePlayer, rootDir, dir, file)
print("Running command: " + command)
def do_rename(old, new):
print(f"rename %s -> %s" % (old, new))
rename(join(rootDir, old), join(rootDir, new))
def process(dir, new_title, movie, movie_details):
do_rename(dir, new_title)
do_rename(join(new_title, files["main"]), join(new_title, new_title + ".mkv"))
extra = 1
for file in files["extras"]:
extra_name = f"%s - Extra #%d.mkv" % (new_title, extra)
extra += 1
do_rename(join(new_title, file), join(new_title, extra_name))
for dir in getDirectories(rootDir):
click.echo(f"Search Directory: %s" % dir)
files = getFiles(rootDir, dir)
if not files:
print("Skipping directory " + dir)
showMovie(dir, files["main"])
details = getMovieFromImdb()
if details:
process(details["dir"], details["new_title"], details["movie"], details["movie_details"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.