Current location

narf Source control manager Git

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorshtrom <shtrom@1991c358-8f32-0410-a49a-990740bdf4c2>2014-12-27 11:48:57 +0000
committershtrom <shtrom@1991c358-8f32-0410-a49a-990740bdf4c2>2014-12-27 11:48:57 +0000
commitec4e7bd388ac688ec6b2b1b7d057d23072d4b1c4 (patch)
treeb2fab5bd0fdeebb172b2c4bb3bd1a587ddf9dc13 /thetvdb_rename.py
parenta3cae2567300b0f2e5d58ae5945e744e64d5928b (diff)
[scripts] Add TheTV DB renamer.
git-svn-id: svn+ssh://scm.narf.ssji.net/svn/shtrom/scripts@2145 1991c358-8f32-0410-a49a-990740bdf4c2
Diffstat (limited to 'thetvdb_rename.py')
-rwxr-xr-xthetvdb_rename.py87
1 files changed, 87 insertions, 0 deletions
diff --git a/thetvdb_rename.py b/thetvdb_rename.py
new file mode 100755
index 0000000..e5d17a2
--- /dev/null
+++ b/thetvdb_rename.py
@@ -0,0 +1,87 @@
+#!/usr/bin/env python
+
+import os, sys
+import logging
+import tvdb_api
+import re
+from fuzzywuzzy import fuzz
+
+outdir="renamed"
+series="doctor who"
+ext="mkv"
+
+logging.basicConfig(level=logging.INFO)
+
+inparen=re.compile('.*\(([^\)]\+)\)')
+
+if len(sys.argv) < 2:
+ logging.error("usage %s SEASON" % sys.argv[0])
+ exit(1)
+
+season = int(sys.argv[1])
+
+t=tvdb_api.Tvdb()
+s=t[series][season]
+logging.info("got %s season %s with %d episodes" % (s.show, season, len(s)))
+
+try:
+ os.mkdir(outdir)
+ logging.debug("created output folder %s" % outdir)
+except OSError:
+ pass
+
+for f in os.listdir("."):
+ e = None
+ title = None
+ if f != outdir:
+ e = []
+ logging.debug("considering '%s'..." % f)
+ title = f.split("- ")[-1].rstrip(".%s" % ext)
+ logging.debug("considering '%s'..." % title)
+ try:
+ e.extend(s.search(title))
+ except tvdb_exceptions.tvdb_episodenotfound:
+ logging.debug("%s not found" % 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)
+
+ for ss in reversed(re.split("[-()]", title)):
+ if len(ss.strip()) > 0:
+ logging.debug("considering '%s'..." % ss)
+ try:
+ e.extend(s.search(ss))
+ except tvdb_exceptions.tvdb_episodenotfound:
+ logging.debug("%s not found" % 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))
+ else:
+ e = em[max(em.keys())]
+
+ logging.debug("found %s", e['episodename'])
+
+ d = "%s/%s - S%02dE%02d - %s.%s" % (outdir,
+ e.season.show['seriesname'],
+ season,
+ int(e['episodenumber']),
+ title,
+ ext
+ )
+ print("S%02dE%02d: %s" % (
+
+ season,
+ int(e['episodenumber']),
+ f
+ ))
+ try:
+ os.link(f, d)
+ except OSError, e:
+ logging.warning("%s: %s" % (d, e.strerror))