-
Notifications
You must be signed in to change notification settings - Fork 7
/
comicbookmaker.py
executable file
·135 lines (126 loc) · 4.02 KB
/
comicbookmaker.py
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#! /usr/bin/python3
# vim: set expandtab tabstop=4 shiftwidth=4 :
"""Module to retrieve webcomics and create ebooks"""
import book
import argparse
import logging
import random
from comics import COMICS_DICT
def get_file_content_until_tag(path, tag):
"""Get content from a filepath up to a given tag.
If tag is not is the file, an exception is thrown."""
content = []
with open(path) as f:
for l in f:
content.append(l)
if tag == l.strip():
return content
raise ValueError('Could not find "%s" in file "%s"' % (tag, path))
def add_new_lines_after_tag(path, new_lines, tag):
"""Add lines to file from a given tag.
All content until tag is kept, all content after is lost."""
content = get_file_content_until_tag(path, tag)
with open(path, "w") as f:
f.write("".join(content + new_lines))
def main():
"""Main function"""
logger = logging.getLogger()
arg_to_method = {
"list": "print_name",
"update": "update",
"info": "info",
"check": "check_everything_is_ok",
"fix": "try_to_get_missing_resources",
"reset_new": "reset_new",
"delete_last": "delete_last",
"delete_all": "delete_all",
}
comic_names = sorted(COMICS_DICT.keys(), key=lambda s: s.lower())
parser = argparse.ArgumentParser(
description="Downloads webcomics and generates ebooks for offline reading"
)
parser.add_argument(
"--comic",
"-c",
action="append",
help=("comics to be considered (default: ALL) - categories can be used"),
choices=comic_names,
default=[],
)
parser.add_argument(
"--excluded",
"-e",
action="append",
help=("comics to be excluded - categories can be used"),
choices=comic_names,
default=[],
)
parser.add_argument(
"--action",
"-a",
action="append",
help=("actions required"),
choices=list(arg_to_method) + ["book", "gitignore", "readme"],
default=[],
)
parser.add_argument(
"--loglevel",
"-l",
type=int,
action="store",
help=("log level (as per the Python logging module)"),
default=logging.CRITICAL,
)
parser.add_argument(
"--reverse",
"-r",
action="store_true",
help=("process comics in reversed order"),
)
parser.add_argument(
"--random",
"-R",
action="store_true",
help=("process comics in random order"),
)
args = parser.parse_args()
logger.setLevel(args.loglevel)
# Apply default value
if not args.comic:
args.comic = ["ALL"]
if not args.action:
args.action = ["update"]
comic_classes = set()
for name in args.comic:
for klass in COMICS_DICT[name]:
comic_classes.add(klass)
for name in args.excluded:
for klass in COMICS_DICT[name]:
comic_classes.discard(klass)
comic_classes = list(comic_classes)
if args.random:
random.shuffle(comic_classes)
else:
comic_classes.sort(key=lambda c: c.name.lower(), reverse=args.reverse)
logging.debug("Starting")
for action in args.action:
method_name = arg_to_method.get(action)
if method_name is not None:
for com in comic_classes:
if args.random:
print(com.name)
getattr(com, method_name)()
elif action == "book":
book.make_book(comic_classes)
elif action == "gitignore":
path = ".gitignore"
new_content = [com.gitignore() for com in comic_classes]
add_new_lines_after_tag(path, new_content, "# Generated folders")
elif action == "readme":
path = "README.md"
new_content = [com.readme() for com in comic_classes]
add_new_lines_after_tag(path, new_content, "----------------")
else:
print("Unknown action : %s" % action)
if __name__ == "__main__":
main()