-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.py
171 lines (136 loc) · 5.87 KB
/
main.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from __future__ import (unicode_literals, division, absolute_import,
print_function)
__license__ = 'GPL v3'
__copyright__ = '2015, dloraine'
__docformat__ = 'restructuredtext en'
from functools import partial
from calibre.gui2 import error_dialog, info_dialog
from calibre_plugins.EmbedComicMetadata.config import prefs
from calibre_plugins.EmbedComicMetadata.languages.lang import _L
from calibre_plugins.EmbedComicMetadata.comicmetadata import ComicMetadata
import sys
python3 = sys.version_info[0] > 2
def import_to_calibre(ia, action):
def _import_to_calibre(metadata):
metadata.get_comic_metadata_from_file()
if action == "both" and metadata.comic_metadata:
metadata.import_comic_metadata_to_calibre(metadata.comic_metadata)
elif action == "cix" and metadata.cix_metadata:
metadata.import_comic_metadata_to_calibre(metadata.cix_metadata)
elif action == "cbi" and metadata.cbi_metadata:
metadata.import_comic_metadata_to_calibre(metadata.cbi_metadata)
else:
return False
return True
iterate_over_books(ia, _import_to_calibre,
_L["Updated Calibre Metadata"],
_L['Updated calibre metadata for {} book(s)'],
_L['The following books had no metadata: {}'],
prefs['convert_reading'])
def embed_into_comic(ia, action):
def _embed_into_comic(metadata):
if metadata.format != "cbz":
return False
metadata.overlay_metadata()
if action == "both" or action == "cix":
metadata.embed_cix_metadata()
if action == "both" or action == "cbi":
metadata.embed_cbi_metadata()
metadata.add_updated_comic_to_calibre()
return True
iterate_over_books(ia, _embed_into_comic,
_L["Updated comics"],
_L['Updated the metadata in the files of {} comics'],
_L['The following books were not updated: {}'])
def convert(ia):
iterate_over_books(ia, partial(convert_to_cbz, ia),
_L["Converted files"],
_L['Converted {} book(s) to cbz'],
_L['The following books were not converted: {}'],
False)
def embed_cover(ia):
def _embed_cover(metadata):
if metadata.format != "cbz":
return False
metadata.update_cover()
metadata.add_updated_comic_to_calibre()
return True
iterate_over_books(ia, _embed_cover,
_L["Updated Covers"],
_L['Embeded {} covers'],
_L['The following covers were not embeded: {}'])
def count_pages(ia):
def _count_pages(metadata):
if metadata.format != "cbz":
return False
return metadata.action_count_pages()
iterate_over_books(ia, _count_pages,
_L["Counted pages"],
_L['Counted pages in {} comics'],
_L['The following comics were not counted: {}'])
def get_image_size(ia):
def _get_image_size(metadata):
if metadata.format != "cbz":
return False
return metadata.action_picture_size()
iterate_over_books(ia, _get_image_size,
_L["Updated Calibre Metadata"],
_L['Updated calibre metadata for {} book(s)'],
_L['The following books were not updated: {}'])
def iterate_over_books(ia, func, title, ptext, notptext,
should_convert=None,
convtext=_L["The following comics were converted to cbz: {}"]):
'''
Iterates over all selected books. For each book, it checks if it should be
converted to cbz and then applies func to the book.
After all books are processed, gives a completion message.
'''
processed = []
not_processed = []
converted = []
if should_convert is None:
should_convert = prefs["convert_cbr"]
# iterate through the books
for book_id in get_selected_books(ia):
metadata = ComicMetadata(book_id, ia)
# sanity check
if metadata.format is None:
not_processed.append(metadata.info)
continue
if should_convert and convert_to_cbz(ia, metadata):
converted.append(metadata.info)
if func(metadata):
processed.append(metadata.info)
else:
not_processed.append(metadata.info)
# show a completion message
msg = ptext.format(len(processed))
if should_convert and len(converted) > 0:
msg += '\n' + convtext.format(lst2string(converted))
if len(not_processed) > 0:
msg += '\n' + notptext.format(lst2string(not_processed))
info_dialog(ia.gui, title, msg, show=True)
def get_selected_books(ia):
# Get currently selected books
rows = ia.gui.library_view.selectionModel().selectedRows()
if not rows or len(rows) == 0:
return error_dialog(ia.gui, _L['Cannot update metadata'],
_L['No books selected'], show=True)
# Map the rows to book ids
return map(ia.gui.library_view.model().id, rows)
def lst2string(lst):
if python3:
return "\n " + "\n ".join(lst)
return "\n " + "\n ".join(item.encode('utf-8') for item in lst)
def convert_to_cbz(ia, metadata):
if metadata.format == "cbr" or (metadata.format == "rar" and prefs['convert_archives']):
metadata.convert_cbr_to_cbz()
if prefs['delete_cbr']:
ia.gui.current_db.new_api.remove_formats({metadata.book_id: {"cbr", "rar"}})
return True
elif metadata.format == "zip" and prefs['convert_archives']:
metadata.convert_zip_to_cbz()
if prefs['delete_cbr']:
ia.gui.current_db.new_api.remove_formats({metadata.book_id: {"zip"}})
return True
return False