forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrebook.py
executable file
·43 lines (28 loc) · 1.08 KB
/
grebook.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
#!/usr/bin/env python3
from __future__ import print_function
import sys, os
import subprocess
import re
def grep_ebook(pat, filename):
# unzip only html and content files to stdin
p1 = subprocess.Popen(['unzip', '-p', filename,
'*.htm*', '*.xml', '*.opf'],
shell=False, stdout=subprocess.PIPE)
output = p1.communicate()[0]
p1 = re.sub('[<][^>]{1,200}?[>]', '', output)
# keep some context around matches
p3 = subprocess.Popen(['grep', '-Piaso', '.{0,30}%s.{0,30}' % pat],
shell=False, stdin=output, stdout=subprocess.PIPE)
# color the matches (doesn't work for me)
p4 = subprocess.Popen(['grep', '-Pi', '--color', pat],
shell=False, stdin=p3.stdout, stdout=subprocess.PIPE)
p3.stdout.close()
output = p4.communicate()[0]
return output
if __name__ == '__main__':
for book in sys.argv[2:]:
output = grep_ebook(sys.argv[1], book)
if output:
print('=====', book)
print(output)
print()