Current location

narf Source control manager Git

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOlivier Mehani <shtrom@ssji.net>2021-07-23 11:12:25 +1000
committerOlivier Mehani <shtrom@ssji.net>2021-07-23 11:12:25 +1000
commitd53430f66f377dbcaa3d0ac473a7692b41652ce9 (patch)
tree9eb1f5a65af091e835083fdcf3662104b2c95e44
parent189e5555667babb49d52e4f8bc821d7936be3dd4 (diff)
thetvdb_rename: python3 and more robust name matching
Signed-off-by: Olivier Mehani <shtrom@ssji.net>
-rwxr-xr-xthetvdb_rename.py89
1 files changed, 35 insertions, 54 deletions
diff --git a/thetvdb_rename.py b/thetvdb_rename.py
index f525391..ff9db9e 100755
--- a/thetvdb_rename.py
+++ b/thetvdb_rename.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
# pip install tvnamer fuzzywuzzy
@@ -6,6 +6,7 @@ import os, sys
import logging
import tvdb_api # use tvnamer
import re
+import html
# Need python-levenshtein for faster sequence matching
from fuzzywuzzy import fuzz
@@ -14,6 +15,7 @@ ext="mkv"
TVNAMER_THETVDB_APIKEY='fb51f9b848ffac9750bada89ecba0225' # https://github.com/dbr/tvnamer/blob/24a9642f261611499a026f95b4fa5496de553e45/tvnamer/main.py#L55
debuglevel=logging.INFO
+debuglevel=logging.DEBUG
logging.basicConfig(level=debuglevel)
inparen=re.compile('.*\(([^\)]\+)\)')
@@ -40,62 +42,41 @@ except OSError:
pass
count=0
+eps = {e['episodeName']: e for e in s.values()}
for f in os.listdir("."):
- e = None
- title = None
+ title = f.split("- ")[-1].rstrip(".%s" % ext)
if f != outdir and f.split(".")[-1] == ext:
- e = []
- logging.debug("considering '%s'..." % f)
- title = f.split("- ")[-1].rstrip(".%s" % ext)
- logging.debug("considering '%s'..." % title)
+ em = {fuzz.ratio(title, t): t for t in eps.keys()}
+ logging.debug("found %s", em)
+
+ maxt = em[max(em.keys())]
+ maxe = eps[maxt]
+ clean_title = maxe['episodeName']
+
+ logging.debug("found %s", clean_title)
+
+ episodeNumber = maxe['dvdEpisodeNumber']
+ if not episodeNumber:
+ episodeNumber = maxe['airedEpisodeNumber']
+
+ d = "%s/%s - S%02dE%02d - %s.%s" % (outdir,
+ maxe.season.show['seriesName'],
+ season,
+ episodeNumber,
+ clean_title,
+ ext
+ )
+ logging.info("%s -> S%02dE%02d " % (
+ f,
+ season,
+ episodeNumber,
+ ))
+
+ count += 1
try:
- e.extend(s.search(title))
- except tvdb_exceptions.tvdb_episodenotfound:
- 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 matched" % 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 matched" % ss)
- continue
-
- logging.debug(e)
-
- em = {fuzz.ratio(title, em['episodename']): em for em in e}
- if len(em) < 1:
- logging.warning("couldn't match %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
- )
- logging.info("%s -> S%02dE%02d " % (
- f,
- season,
- int(e['episodenumber'])
- ))
-
- count += 1
- try:
- os.link(f, d)
- except OSError as e:
- logging.warning("%s: %s" % (d, e.strerror))
+ os.link(f, d)
+ except OSError as e:
+ logging.warning("%s: %s" % (d, e.strerror))
if count != len(s):
logging.warning("processed %d files but expected %d" % (count, len(s)))