-
Notifications
You must be signed in to change notification settings - Fork 0
/
fb2_zipper.py
153 lines (116 loc) · 4.41 KB
/
fb2_zipper.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
import sys
import zipfile
import argparse
from xml.dom import minidom
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--folder", dest="folder", help="folder for search books")
parser.add_argument("-b", "--book", dest="book", help="handle one book")
parser.add_argument("-r", "--remove", action="store_true", help="remove old books")
args = parser.parse_args()
if args.book != None:
folder_hndl(args.book, args.remove)
elif args.folder != None:
folder_hndl(args.folder, args.remove)
else:
print("One of folowing argument is required: --book, --folder", file=sys.stderr)
def folder_hndl(path, remove=False):
if os.path.isfile(path):
flag = False
print("\tprocessing file: %s" % path)
if path[-4:] == ".fb2":
flag = fb2_hndl(path)
elif path[-8:] == ".fb2.zip":
flag = fb2_zip_hndl(path)
if remove and flag:
print("\t\tremoved file")
os.remove(path)
else:
print("\t\tnot removed file")
else:
print("scan folder: %s" % path)
file_list = os.listdir(path)
for file in file_list:
file_path = os.path.join(path, file)
folder_hndl(file_path, remove)
print("\n")
def fb2_hndl(file_path):
status = True
try:
dom = minidom.parse(file_path)
except:
print("\t\tERROR: Failed to parse file")
status = False
else:
dir_path = os.path.split(file_path)[0]
book_name = fb2_get_book_name(dom)
if(book_name != None):
book_path = os.path.join(dir_path, book_name) + ".zip"
if book_path != file_path:
zf = zipfile.ZipFile(book_path, 'w', zipfile.ZIP_DEFLATED)
zf.writestr(book_name, dom.toxml())
zf.close()
status = True
else:
print("\t\tcorrect file already exists")
status = False
else:
status = False
return status
def fb2_zip_hndl(file_path):
status = True
if zipfile.is_zipfile(file_path):
zf = zipfile.ZipFile(file_path, 'r')
dir_path = os.path.split(file_path)[0]
for fname in zf.namelist():
if fname[-4:] == ".fb2":
print("\t\tfind fb2 file: %s" % fname)
book_zf = zf.open(fname, 'r')
try:
dom = minidom.parse(book_zf)
except:
print("\t\tERROR: Failed to parse file")
status = False
else:
book_name = fb2_get_book_name(dom)
if(book_name != None):
book_path = os.path.join(dir_path, book_name) + ".zip"
if book_path != file_path:
nzf = zipfile.ZipFile(book_path, 'w', zipfile.ZIP_DEFLATED)
nzf.writestr(book_name, dom.toxml())
nzf.close()
status = True
else:
print("\t\tcorrect file already exists")
status = False
else:
status = False
zf.close()
else:
status = False
return status
def fb2_get_book_name(dom):
title_info_node = dom.getElementsByTagName("title-info")
author_node = title_info_node[0].getElementsByTagName("author")
first_name_node = author_node[0].getElementsByTagName("first-name")
last_name_node = author_node[0].getElementsByTagName("last-name")
book_title_node = title_info_node[0].getElementsByTagName("book-title")
book_name = xml_get_text(book_title_node[0].firstChild)
first_name = xml_get_text(first_name_node[0].firstChild)
last_name = xml_get_text(last_name_node[0].firstChild)
if book_name != None and first_name != None and last_name != None:
tr_table = book_name.maketrans("\/?|:", ".....")
book_name = book_name.translate(tr_table,)
return first_name + " " + last_name + " - " + book_name + ".fb2"
else:
return None
def xml_get_text(node):
if node is None:
return None
else:
return node.nodeValue
if __name__ == "__main__":
main()