Current location

narf Source control manager Git

summaryrefslogtreecommitdiff
blob: e5d17a2ea3ab1d4661f71e43acdbd0f6dbc73057 (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
#!/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))