summaryrefslogtreecommitdiff
path: root/bin/movietool
blob: ce16861c2e6876f8447d21f18b82af8087d65b86 (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
#!/usr/bin/env python3
# coding: utf-8
# python3 works as well
from movietool import *

if __name__ == "__main__":
    tmdb.API_KEY = read_key()

    parser = argparse.ArgumentParser(add_help=False)
    parser.add_argument("--help", action=HelpAction, help="Display full help")
    parser.add_argument("--log-level", action='store', type=int,
                        help="Set log level (CRITICAL=50," +
                        "ERROR=40,WARNING=30,INFO=20,DEBUG=10,NOTSET=0")
    parser.add_argument("-h", action=argparse._HelpAction,
                        help="Display short help")

    subparsers = parser.add_subparsers()
    parser_aka = subparsers.add_parser("aka", add_help=False,
                                       help="Print alternative titles")
    parser_aka.add_argument("--lang", help="Language code (default 'DE')")
    parser_aka.set_defaults(func=do_aka)
    parser_aka.add_argument("files", action="append", nargs="+",
                            help="Files containing distinct movie-ids")

    parser_data = subparsers.add_parser("data", add_help=False,
                                        help="Print all available data")
    parser_data.set_defaults(func=do_data)
    parser_data.add_argument("files", action="append", nargs="+",
                             help="Files containing distinct movie-ids")

    parser_year = subparsers.add_parser("year", add_help=False,
                                        help="Print only the release year")
    parser_year.set_defaults(func=do_year)
    parser_year.add_argument("files", action="append", nargs="+",
                             help="Files containing distinct movie-ids")

    parser_prune = subparsers.add_parser("prune", add_help=False,
                                         help="Delete cache entries")
    parser_prune.set_defaults(func=do_prune)
    parser_prune.add_argument("files", action="append", nargs="+",
                              help="Files containing distinct movie-ids")

    parser_rating = subparsers.add_parser("rating", add_help=False,
                                          help="Print movie ratings")
    parser_rating.set_defaults(func=do_rating)
    parser_rating.add_argument("files", action="append", nargs="+",
                               help="Files containing distinct movie-ids")

    parser_index = subparsers.add_parser("index", add_help=False,
                                         help="Generate index.html file")
    parser_index.set_defaults(func=do_index)
    parser_index.add_argument("files", action="append", nargs="+",
                              help="Files containing distinct movie-ids")

    parser_rename = subparsers.add_parser("rename", add_help=False,
                              help="Search supported rename")
    parser_rename.set_defaults(func=do_rename)
    parser_rename.add_argument("file", metavar="PATH", nargs="*",
                              help="File or folder to rename into tagged-imdb format")

    parser_test = subparsers.add_parser("test", add_help=False,
                                        help="Run testsuite")
    parser_test.set_defaults(func=do_test)

    args = parser.parse_args(sys.argv[1:])
    if "log_level" in args:
        logging.basicConfig(level=args.log_level)

    if "files" in args:
        ids = args.files[0]
        ids = map(lambda filename:
                  (lambda x: (filename, x.groups()[0]) if x else None)
                  (re.match(".*#(tt[0-9]{7}).*", filename)),
                  ids)
        args.func(args, list(filter(lambda i: i is not None, ids)))
    elif "func" in args:
        args.func(args)
    else:
        HelpAction("")(parser, None, None)