summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYves Fischer <yvesf-git@xapek.org>2014-12-25 21:08:12 +0100
committerYves Fischer <yvesf-git@xapek.org>2014-12-25 21:08:12 +0100
commit68dfe22251cb9e641d3db3fe2eb3c7ff46997fcd (patch)
tree5be538e028a7cd52a93f0ea9aa97dc25a0634333
parent79ba03a5a84a4ac5c526cb69f1337b2fdc876c54 (diff)
downloadscripts-68dfe22251cb9e641d3db3fe2eb3c7ff46997fcd.tar.gz
scripts-68dfe22251cb9e641d3db3fe2eb3c7ff46997fcd.zip
rename support
-rwxr-xr-ximdb-lookup/imdbinfo.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/imdb-lookup/imdbinfo.py b/imdb-lookup/imdbinfo.py
index fe4bd60..15942f4 100755
--- a/imdb-lookup/imdbinfo.py
+++ b/imdb-lookup/imdbinfo.py
@@ -8,6 +8,7 @@ import re
import json
import time
import argparse
+import locale
import math
import shutil
import logging
@@ -102,6 +103,11 @@ class TMDBCache(object):
raise Exception("Failed to query movie-id {id}: {reason}".format(
id=movie_id, reason=str(e)))
+ def tmdb_search(self, query):
+ results = tmdb.Search().movie(query=query)["results"]
+ results = map(lambda i: self.infos(str(i['id'])), results)
+ return list(results)
+
def alternative_title(self, movie_id, locale):
"""Returns the title in selected locale or original title otherwise"""
try:
@@ -418,6 +424,65 @@ class HelpAction(argparse._HelpAction):
print(formatter.format_help())
parser.exit(0)
+def do_rename(args):
+ def filter_filename(filename):
+ """remove possibly everything except the movie name"""
+ stopwords = [
+ "dvd", "ac3", "r5", "unrated", "ts", "720p", "md",
+ "ts", "ld", "bdrip", "tvrip", "dvdrip", "dvdscr", "uncut",
+ "german", "english", "telesync", "20[0-1][0-9]|19[0-9][0-9]",
+ "x264", "hdtv", "ws"
+ ]
+ def findword(word):
+ return re.findall(u"(.*?)\.?" + word + u"[\.\- ]", filename, re.IGNORECASE)
+ matches = [i for i in map(findword, stopwords) if i!=[]]
+ matches.sort()
+ if len(matches) > 0:
+ name = matches[0][0]
+ else:
+ name = filename
+ return name.replace(u".", u" ")
+
+ def substitute_filename(filename):
+ """remove potentially harmful characters from filename"""
+ return re.sub(u"[^a-zA-Z0-9()\- _#]", u"_", filename)
+
+ from pprint import pprint
+ countrycode = locale.getdefaultlocale()[0][3:5]
+ db = Protector(TMDBCache())
+ search_term = filter_filename(args.file[0])
+ print("Search for: {}".format(search_term))
+ results = db.tmdb_search(search_term)
+ results.sort(key=lambda i:i["popularity"],reverse=True)
+ results = filter(lambda i: i["imdb_id"] and i["release_date"], results)
+ results = list(results)
+
+ for (i,result) in zip(range(len(results)), results):
+ result["i"] = i+1
+ result["year"] = result["release_date"].split("-")[0]
+ print("{i} {title} ({year}) #{imdb_id}".format(**result))
+ result["alternative_title"] = db.alternative_title(result["imdb_id"], locale=countrycode)
+ if result["alternative_title"] != result["title"]:
+ print(" a.k.a.: {}".format(result["alternative_title"]))
+
+ select = None
+ while not select:
+ try:
+ select = int(input("Choose: "))
+ except ValueError:
+ pass
+
+ filename = "{alternative_title} ({year}) #{imdb_id}".format(**results[select-1])
+ filename = substitute_filename(filename)
+
+ if os.path.exists(args.file[0]):
+ choice = input("Move '{}' -> '{}' ? [y/N]: ".format(args.file[0], filename)).lower()
+ if choice == "y":
+ os.mkdir(filename)
+ shutil.move(args.file[0], filename)
+ else:
+ print("mkdir '{}'".format(filename))
+ os.mkdir(filename)
def do_test(args):
import doctest
@@ -472,6 +537,12 @@ if __name__ == "__main__":
parser_index.add_argument("files", action="append", nargs="+",
help="Files containing distinct movie-ids")
+ parser_rename = subparsers.add_parser("search", add_help=False,
+ help="Search supported rename")
+ parser_rename.set_defaults(func=do_rename)
+ parser_rename.add_argument("file", metavar="PATH", nargs=1,
+ help="File or folder to rename into tagged-imdb format")
+
parser_test = subparsers.add_parser("test", add_help=False,
help="Run testsuite")
parser_test.set_defaults(func=do_test)