Current location

narf Source control manager Git

summaryrefslogtreecommitdiff
blob: cfe54fff378b1da9a8c892ac9535dc61a844b60c (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
101
102
103
#!/usr/bin/env python3

# pip install tvnamer fuzzywuzzy

import os
import 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 = "mp4"
# https://github.com/dbr/tvnamer/blob/24a9642f261611499a026f95b4fa5496de553e45/tvnamer/main.py#L55
TVNAMER_THETVDB_APIKEY = 'fb51f9b848ffac9750bada89ecba0225'

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

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

if len(sys.argv) < 2:
    logging.error(
        "usage %s SERIES [EXTENSION=mp4 [OUTDIR=renamed]]" % sys.argv[0])
    exit(1)

series = sys.argv[1]
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)

        maxem = max(em.keys())
        maxt = em[maxem]
        maxe = eps[maxt]
        clean_title = maxe['episodeName']

        episodeNumber = maxe['dvdEpisodeNumber']
        if not episodeNumber:
            episodeNumber = maxe['airedEpisodeNumber']

        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:
            os.link(f, d)
        except OSError as e:
            logging.warning("%s: %s" % (d, e.strerror))

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