Created
April 3, 2015 19:47
-
-
Save tnajdek/fab017f5df1711007d7c to your computer and use it in GitHub Desktop.
This simple script will guess movie titles based on the file names and move them to collection dir
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# This simple script will guess movie titles based on the file names and move them to collection dir | |
# Useful when you have movies with names like Star.Wars.Phantom.Menace.mkv and you want them | |
# in your collection in folders like Star Wars: Episode VI - Return of the Jedi (1983) | |
# To get dependencies run `pip install IMDbPY==5.0 guessit==0.10.2` | |
from __future__ import print_function | |
import os | |
import argparse | |
import imdb | |
import shutil | |
from datetime import datetime | |
from guessit import guess_file_info, guess_movie_info | |
parser = argparse.ArgumentParser(description='Guess movie titles based on the file names and move them to collection dir') | |
parser.add_argument('source', | |
metavar='source', | |
type=str, | |
help="Path to source directory" | |
) | |
parser.add_argument('destination', | |
metavar='destination', | |
type=str, | |
help='Target directory where movies will me moved' | |
) | |
parser.add_argument('--older-than', | |
dest="older_than", | |
default=0, | |
help="Only move movie file/directory if it has been created more than these many hours" | |
) | |
args = parser.parse_args() | |
ia = imdb.IMDb() | |
max_age_hours = args.older_than * 3600 | |
def find_matching(guess, imdb_results): | |
if('year' in guess): | |
try: | |
return next(m for m in imdb_results if 'year' in m.data and m.data['year'] == guess['year']) | |
except StopIteration: | |
return imdb_results[0] | |
else: | |
return imdb_results[0] | |
sources = os.listdir(args.source) | |
for source in sources: | |
source = source.decode('utf-8') | |
print(source, end="...") | |
if(source[0] == '.'): | |
print("Skipping file starting with a dot") | |
continue | |
src_path = os.path.join(os.path.abspath(args.source), source) | |
mtime = os.path.getmtime(src_path) | |
delta = datetime.now() - datetime.fromtimestamp(int(mtime)) | |
if(delta.seconds >= max_age_hours): | |
try: | |
movie_guess = guess_file_info(source) | |
if('title' not in movie_guess): | |
movie_guess = guess_movie_info(source) | |
except Exception: | |
# guessit breaks on some with IndexError... | |
print(u"Sorry, can't figure this one out.") | |
continue | |
if('title' in movie_guess and 'series' not in movie_guess): | |
search_results = ia.search_movie(movie_guess['title']) | |
if(len(search_results)): | |
movie = find_matching(movie_guess, search_results) | |
if(movie.data['kind'] == 'movie'): | |
if(movie.data['title'] and movie.data['year']): | |
target_dir = u"{} ({})".format(movie.data['title'], movie.data['year']) | |
target_dir = os.path.join(os.path.abspath(args.destination), target_dir) | |
if(not os.path.exists(target_dir)): | |
if(os.path.isdir(src_path)): | |
shutil.move(src_path, target_dir) | |
else: | |
os.mkdir(target_dir) | |
shutil.move(src_path, target_dir) | |
print(u"Moved {} to {}".format(src_path, target_dir)) | |
else: | |
print(u"Skipping as target dir ({}) already exists!".format(target_dir)) | |
else: | |
print(u"Skipping as couldn't find matching movie (title/year) on imdb, got: {} ({})".format(movie.data['title'], movie.data['year'])) | |
else: | |
print(u"Skipping as it seems to be a {} and not a movie".format(movie.data['kind'])) | |
else: | |
print(u"Skipping as it doesn't seem to be on imdb") | |
else: | |
print(u"Skipping in source dir as it doesn't seem to be a movie") | |
else: | |
print(u"Skipping because of its mtime") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment