Current location

narf Source control manager Git

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOlivier Mehani <shtrom@ssji.net>2016-02-05 22:00:05 +1100
committerOlivier Mehani <shtrom@ssji.net>2016-02-05 22:56:29 +1100
commit66cf76614c6d91a673e129ae286e75595cc40e79 (patch)
treebd432f476269481f71c0d3b8db1752c6f2f0c86e /thetvdb_rename.py
parentf640d669d1ce644d7f5085b5fb8a1f71eb32c797 (diff)
Make thetvdb_rename.py more flexibile
* add extension, series, outdir from command line * cleaner logging * report number of renamed files if different from total
Diffstat (limited to 'thetvdb_rename.py')
-rwxr-xr-xthetvdb_rename.py35
1 files changed, 24 insertions, 11 deletions
diff --git a/thetvdb_rename.py b/thetvdb_rename.py
index e5d17a2..2ec60ba 100755
--- a/thetvdb_rename.py
+++ b/thetvdb_rename.py
@@ -4,21 +4,29 @@ import os, sys
import logging
import tvdb_api
import re
+# Need python-levenshtein for faster sequence matching
from fuzzywuzzy import fuzz
outdir="renamed"
series="doctor who"
ext="mkv"
-logging.basicConfig(level=logging.INFO)
+debuglevel=logging.INFO
+logging.basicConfig(level=debuglevel)
inparen=re.compile('.*\(([^\)]\+)\)')
if len(sys.argv) < 2:
- logging.error("usage %s SEASON" % sys.argv[0])
+ logging.error("usage %s SEASON [EXTENSION [SERIES [OUTDIR]]]" % sys.argv[0])
exit(1)
season = int(sys.argv[1])
+if len(sys.argv)>2:
+ ext = sys.argv[2]
+if len(sys.argv)>3:
+ series = sys.argv[3]
+if len(sys.argv)>4:
+ outdir = sys.argv[4]
t=tvdb_api.Tvdb()
s=t[series][season]
@@ -30,10 +38,11 @@ try:
except OSError:
pass
+count=0
for f in os.listdir("."):
e = None
title = None
- if f != outdir:
+ if f != outdir and f.split(".")[-1] == ext:
e = []
logging.debug("considering '%s'..." % f)
title = f.split("- ")[-1].rstrip(".%s" % ext)
@@ -41,13 +50,13 @@ for f in os.listdir("."):
try:
e.extend(s.search(title))
except tvdb_exceptions.tvdb_episodenotfound:
- logging.debug("%s not found" % title)
+ logging.debug("%s not matched" % title)
title_noep = title.lower().replace("episode ","")
logging.debug("considering '%s'..." % title_noep)
try:
e.extend(s.search(title_noep))
except tvdb_exceptions.tvdb_episodenotfound:
- logging.debug("%s not found" % title_noep)
+ logging.debug("%s not matched" % title_noep)
for ss in reversed(re.split("[-()]", title)):
if len(ss.strip()) > 0:
@@ -55,14 +64,14 @@ for f in os.listdir("."):
try:
e.extend(s.search(ss))
except tvdb_exceptions.tvdb_episodenotfound:
- logging.debug("%s not found" % ss)
+ logging.debug("%s not matched" % ss)
continue
logging.debug(e)
em = {fuzz.ratio(title, em['episodename']): em for em in e}
if len(em) < 1:
- logging.warning("not found: %s" % (f))
+ logging.warning("couldn't match %s" % (f))
else:
e = em[max(em.keys())]
@@ -75,13 +84,17 @@ for f in os.listdir("."):
title,
ext
)
- print("S%02dE%02d: %s" % (
-
+ logging.info("%s -> S%02dE%02d " % (
+ f,
season,
- int(e['episodenumber']),
- f
+ int(e['episodenumber'])
))
+
+ count += 1
try:
os.link(f, d)
except OSError, e:
logging.warning("%s: %s" % (d, e.strerror))
+
+if count != len(s):
+ logging.warning("processed %d files but expected %d" % (count, len(s)))