-
Notifications
You must be signed in to change notification settings - Fork 23
/
keepToText.py
124 lines (98 loc) · 3.56 KB
/
keepToText.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
import sys, glob, os, shutil, zipfile, time
from HTMLParser import HTMLParser
from zipfile import ZipFile
class MyHTMLParser(HTMLParser):
def attrib_matches(self, tag, attrs):
return [pair for pair in attrs if
pair[0] == self.attrib and pair[1] == self.attribVal]
def handle_starttag(self, tag, attrs):
if tag == self.tag:
if self.attrib_matches(tag, attrs) and not self.nesting:
self.nesting = 1
elif self.nesting:
self.nesting += 1
elif tag == "br" and self.nesting:
self.outf.write("\n")
def handle_endtag(self, tag):
if tag == self.tag and self.nesting:
self.nesting -= 1
def handle_data(self, data):
if self.nesting:
self.outf.write(data.strip())
def __init__(self, outf, tag, attrib, attribVal):
HTMLParser.__init__(self)
self.outf = outf
self.tag = tag
self.attrib = attrib
self.attribVal = attribVal
self.nesting = 0
def msg(s):
print >> sys.stderr, s
sys.stderr.flush()
def htmlFileToText(inputPath, outputDir, tag, attrib, attribVal):
basename = os.path.basename(inputPath).replace(".html", ".txt")
outfname = os.path.join(outputDir, basename)
with open(inputPath, "r") as inf, open(outfname, "w") as outf:
html = inf.read()
parser = MyHTMLParser(outf, tag, attrib, attribVal)
parser.feed(html)
def htmlDirToText(inputDir, outputDir, tag, attrib, attribVal):
try_rmtree(outputDir)
try_mkdir(outputDir)
msg("Building text files in {0} ...".format(outputDir))
for path in glob.glob(os.path.join(inputDir, "*.html")):
htmlFileToText(path, outputDir, tag, attrib, attribVal)
msg("Done.")
def tryUntilDone(action, check):
ex = None
i = 1
while True:
try:
if check(): return
except Exception as e:
ex = e
if i == 20: break
try:
action()
except Exception as e:
ex = e
time.sleep(1)
i += 1
sys.exit(ex if ex != None else "Failed")
def try_rmtree(dir):
if os.path.isdir(dir): msg("Removing {0}".format(dir))
def act(): shutil.rmtree(dir)
def check(): return not os.path.isdir(dir)
tryUntilDone(act, check)
def try_mkdir(dir):
def act(): os.mkdir(dir)
def check(): return os.path.isdir(dir)
tryUntilDone(act, check)
def keepZipToText(zipFileName):
zipFileDir = os.path.dirname(zipFileName)
takeoutDir = os.path.join(zipFileDir, "Takeout")
outputDir=os.path.join(zipFileDir, "Text")
try_rmtree(takeoutDir)
if os.path.isfile(zipFileName):
msg("Extracting {0} ...".format(zipFileName))
try:
with ZipFile(zipFileName) as zipFile:
zipFile.extractall(zipFileDir)
except IOError as e:
sys.exit(e)
translatedKeepDirs = ["Keep", "Notizen"]
for dirName in translatedKeepDirs:
if os.path.isdir(takeoutDir+"/"+dirName): htmlDir = os.path.join(takeoutDir, dirName)
htmlDirToText(inputDir=htmlDir, outputDir=outputDir,
tag="div", attrib="class", attribVal="content")
def main():
try:
cmd, zipFile = sys.argv
except ValueError:
sys.exit("Usage: {0} zipFile".format(sys.argv[0]))
try:
keepZipToText(zipFile)
except WindowsError as e:
sys.exit(e)
if __name__ == "__main__":
main()