-
Notifications
You must be signed in to change notification settings - Fork 4
/
parse_archive.py
210 lines (173 loc) · 6.57 KB
/
parse_archive.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import optparse
import csv
import gzip
import time
import xml.parsers.expat
import requests
from bs4 import BeautifulSoup
META_DIR = 'data/meta/'
HTML_DIR = 'data/html/'
__version__ = 'r4 (2017/11/28)'
parsed_data = dict()
open_tag = ""
text_list = []
def parse_command_line(argv):
"""Command line options parser for the script
"""
usage = "Usage: %prog [options] <CSV input file>"
parser = optparse.OptionParser(usage=usage)
parser.add_option("-o", "--outfile", action="store",
type="string", dest="outfile", default="archive-out.csv",
help="Output CSV filename (default: 'archive-out.csv')")
parser.add_option("--meta", action="store",
type="string", dest="meta", default=META_DIR,
help="Meta files directory (default: '{:s}')".format(META_DIR))
parser.add_option("--html", action="store",
type="string", dest="html", default=HTML_DIR,
help="HTML files directory (default: '{:s}')".format(HTML_DIR))
parser.add_option("-s", "--skip", action="store",
type="int", dest="skip", default=0,
help="Skip rows (default: 0)")
return parser.parse_args(argv)
# 3 handler functions
def start_element(name, attrs):
global open_tag
#print('Start element:', name, attrs)
open_tag = name
def end_element(name):
global open_tag, text_list
#print('End element:', name)
if name == open_tag:
#print(open_tag, "==>", '\n'.join(text_list))
if open_tag not in parsed_data:
parsed_data[open_tag] = []
parsed_data[open_tag].append('\n'.join(text_list))
text_list = []
open_tag = ""
pass
def char_data(data):
#print('Character data:', repr(data))
if data.strip() != "":
text_list.append(data.strip())
pass
if __name__ == "__main__":
print("{:s} - {:s}\n".format(os.path.basename(sys.argv[0]), __version__))
(options, args) = parse_command_line(sys.argv)
if len(args) < 2:
print("Usage: {:s} [options] <CSV input file>".format(os.path.basename(sys.argv[0])))
sys.exit(-1)
print("Parse all meta files to extract all possible fields, please wait...")
count = 0
columns = set()
f = open(args[1])
reader = csv.DictReader(f)
for i, r in enumerate(reader):
if i < options.skip:
continue
count += 1
_id = r['identifier']
file_name = os.path.join(options.meta, _id + "_meta.xml")
if not os.path.isfile(file_name):
file_name += ".gz"
parsed_data = dict()
if os.path.isfile(file_name):
if file_name.endswith('.gz'):
fxml = gzip.open(file_name)
else:
fxml = open(file_name)
xmlstr = fxml.read()
fxml.close()
p = xml.parsers.expat.ParserCreate()
p.StartElementHandler = start_element
p.EndElementHandler = end_element
p.CharacterDataHandler = char_data
try:
p.Parse(xmlstr)
#for t in parsed_data:
# parsed_data[t] = '|'.join(parsed_data[t])
columns.update(list(parsed_data))
except:
print("WARN: Cannot parse {0}".format(file_name))
f.close()
print("Total: {:d}, Meta fields count: {:d}".format(count, len(columns)))
try:
if os.path.isfile(options.outfile):
o = open(options.outfile, encoding='utf-8', newline='')
reader = csv.DictReader(o)
columns = reader.fieldnames
o.close()
o = open(options.outfile, "at", encoding='utf-8', newline='')
writer = csv.DictWriter(o, fieldnames=sorted(list(columns)))
else:
o = open(options.outfile, "wt", encoding='utf-8', newline='')
writer = csv.DictWriter(o, fieldnames=sorted(list(columns)) + ['text'])
writer.writeheader()
except:
if os.path.isfile(options.outfile):
o = open(options.outfile)
reader = csv.DictReader(o)
columns = reader.fieldnames
o.close()
o = open(options.outfile, "ab")
writer = csv.DictWriter(o, fieldnames=sorted(list(columns)))
else:
o = open(options.outfile, "wb")
writer = csv.DictWriter(o, fieldnames=sorted(list(columns)) + ['text'])
writer.writeheader()
count = 0
f = open(args[1])
reader = csv.DictReader(f)
for i, r in enumerate(reader):
if i < options.skip:
continue
count += 1
_id = r['identifier']
print("#{:d}: {:s}".format(count, _id))
# Parse meta file to extract meta fields
file_name = os.path.join(options.meta, _id + "_meta.xml")
if not os.path.isfile(file_name):
file_name += ".gz"
parsed_data = dict()
if os.path.isfile(file_name):
if file_name.endswith('.gz'):
fxml = gzip.open(file_name)
else:
fxml = open(file_name)
xmlstr = fxml.read()
fxml.close()
p = xml.parsers.expat.ParserCreate()
p.StartElementHandler = start_element
p.EndElementHandler = end_element
p.CharacterDataHandler = char_data
try:
p.Parse(xmlstr)
for t in parsed_data:
parsed_data[t] = '|'.join(parsed_data[t]).encode('utf-8')
except:
print("WARN: Cannot parse '{0}'".format(file_name))
continue
# Parse HTML file for closed-caption
file_name = os.path.join(options.html, _id + ".html")
if not os.path.isfile(file_name):
file_name += ".gz"
if os.path.isfile(file_name):
if file_name.endswith('.gz'):
fhtml = gzip.open(file_name, 'rb')
else:
fhtml = open(file_name, 'rb')
htmlstr = fhtml.read()
fhtml.close()
soup = BeautifulSoup(htmlstr, 'html.parser')
htmlstr = soup.prettify()
soup = BeautifulSoup(htmlstr, 'html.parser')
text = ""
for a in soup.find_all('div', {'class': 'snipin nosel'}):
text += a.text.strip()
parsed_data['text'] = text.encode('utf-8')
writer.writerow(parsed_data)
f.close()
o.close()