Current location

narf Source control manager Git

summaryrefslogtreecommitdiff
blob: 10cd2f2d931eb65e03246f26c72093286231bd1d (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python

import os, sys
import logging
import tvdb_api # use tvnamer
import re
# Need python-levenshtein for faster sequence matching
from fuzzywuzzy import fuzz

outdir="renamed"
series="doctor who"
ext="mkv"

debuglevel=logging.INFO
logging.basicConfig(level=debuglevel)

inparen=re.compile('.*\(([^\)]\+)\)')

if len(sys.argv) < 2:
    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]
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
for f in os.listdir("."):
    e = None
    title = None
    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)
        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))

if count != len(s):
    logging.warning("processed %d files but expected %d" % (count, len(s)))