1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
#!/usr/bin/env python3
# pip install tvnamer fuzzywuzzy
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
outdir="renamed"
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('.*\(([^\)]\+)\)')
if len(sys.argv) < 2:
logging.error("usage %s SERIES SEASON [EXTENSION [OUTDIR]]" % 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()}
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())]
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:
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)))
|