Current location

narf Source control manager Git

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOlivier Mehani <shtrom@ssji.net>2021-12-26 13:56:32 +1100
committerOlivier Mehani <shtrom@ssji.net>2021-12-26 13:57:10 +1100
commitab5af9c5490623d39c9ac68e8487aebc99c10ef0 (patch)
treefb9de28deccd74f51e0063275b099ebd13d3cdb5
parent737e999f81d97c4f05dc65e82dc7569dfcb1e43d (diff)
[thetvdb_rename] match any seasonHEADmaster
Signed-off-by: Olivier Mehani <shtrom@ssji.net>
-rwxr-xr-xthetvdb_rename.py109
1 files changed, 65 insertions, 44 deletions
diff --git a/thetvdb_rename.py b/thetvdb_rename.py
index ff9db9e..cfe54ff 100755
--- a/thetvdb_rename.py
+++ b/thetvdb_rename.py
@@ -2,75 +2,95 @@
# pip install tvnamer fuzzywuzzy
-import os, sys
+import os
+import sys
import logging
-import tvdb_api # use tvnamer
+import tvdb_api # use tvnamer
import re
import html
# Need python-levenshtein for faster sequence matching
from fuzzywuzzy import fuzz
-outdir="renamed"
-ext="mkv"
-TVNAMER_THETVDB_APIKEY='fb51f9b848ffac9750bada89ecba0225' # https://github.com/dbr/tvnamer/blob/24a9642f261611499a026f95b4fa5496de553e45/tvnamer/main.py#L55
+outdir = "renamed"
+ext = "mp4"
+# https://github.com/dbr/tvnamer/blob/24a9642f261611499a026f95b4fa5496de553e45/tvnamer/main.py#L55
+TVNAMER_THETVDB_APIKEY = 'fb51f9b848ffac9750bada89ecba0225'
-debuglevel=logging.INFO
-debuglevel=logging.DEBUG
+debuglevel = logging.INFO
+# debuglevel = logging.DEBUG
logging.basicConfig(level=debuglevel)
-inparen=re.compile('.*\(([^\)]\+)\)')
+inparen = re.compile('.*\(([^\)]\+)\)')
if len(sys.argv) < 2:
- logging.error("usage %s SERIES SEASON [EXTENSION [OUTDIR]]" % sys.argv[0])
+ logging.error(
+ "usage %s SERIES [EXTENSION=mp4 [OUTDIR=renamed]]" % sys.argv[0])
exit(1)
series = sys.argv[1]
-season = int(sys.argv[2])
-if len(sys.argv)>3:
- ext = sys.argv[3]
-if len(sys.argv)>4:
- outdir = sys.argv[4]
-
-t=tvdb_api.Tvdb(apikey=TVNAMER_THETVDB_APIKEY)
-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
-
-count=0
-eps = {e['episodeName']: e for e in s.values()}
+if len(sys.argv) > 2:
+ ext = sys.argv[2]
+if len(sys.argv) > 3:
+ outdir = sys.argv[3]
+
+t = tvdb_api.Tvdb(apikey=TVNAMER_THETVDB_APIKEY)
+series = t[series]
+episodes = [e
+ for s in series.values()
+ for e in s.values()
+ ]
+
+logging.info("got %s with %s seasons and %d episodes" %
+ (series.data['seriesName'], len(series), len(episodes)))
+
+
+def mkdir_p(outdir):
+ try:
+ os.mkdir(outdir)
+ logging.debug("created output folder %s" % outdir)
+ except FileExistsError:
+ pass
+ except OSError as e:
+ logging.error("creating output folder %s" % e)
+
+
+mkdir_p(outdir)
+
+count = 0
+eps = {e['episodeName']: e for e in episodes}
for f in os.listdir("."):
title = f.split("- ")[-1].rstrip(".%s" % ext)
if f != outdir and f.split(".")[-1] == ext:
em = {fuzz.ratio(title, t): t for t in eps.keys()}
logging.debug("found %s", em)
- maxt = em[max(em.keys())]
+ maxem = max(em.keys())
+ maxt = em[maxem]
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,
- ))
+ seasonDir = "%s/Season %02d/" % (outdir,
+ maxe['airedSeason'],
+ )
+ mkdir_p(seasonDir)
+ d = "%s/%s - S%02dE%02d - %s.%s" % (seasonDir,
+ maxe.season.show['seriesName'],
+ maxe['airedSeason'],
+ episodeNumber,
+ clean_title,
+ ext
+ )
+ logging.info("%s -> S%02dE%02d %s (%d)" % (
+ f,
+ maxe['airedSeason'],
+ episodeNumber,
+ clean_title,
+ maxem,
+ ))
count += 1
try:
@@ -78,5 +98,6 @@ for f in os.listdir("."):
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)))
+if count != len(episodes):
+ logging.warning("processed %d files but expected %d" %
+ (count, len(episodes)))