-
Notifications
You must be signed in to change notification settings - Fork 13
/
process.py
160 lines (124 loc) · 4.23 KB
/
process.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
import logging
import os
import re
import sys
import xml
import csv
import subprocess
import nltk
sys.path.append("lib")
import pymongo
from BeautifulSoup import BeautifulSoup
logging.basicConfig(level=logging.DEBUG, format="%(levelname)-8s %(message)s")
mongo_conn = pymongo.Connection('localhost', 27017)
db = mongo_conn['wikileaks']
class Cable():
raw = ""
attrs = {}
def __init__(self,raw):
logging.info('Cable()')
self.raw = raw
def __getitem__(self,name):
if name == 'raw':
return self.raw
if name in self.attrs:
return self.attrs[name]
else:
return None
def __setitem__(self,name,value):
self.attrs[name] = value
def get(self):
return self.attrs
class CableGateMirror():
mirror_directory = 'data/cablegate/'
def __init__(self):
logging.info('CableGateMirror()')
self.update()
def update(self):
logging.info('CableGateMirror.update')
#subprocess.call(["httrack",'--update'],cwd=self.mirror_directory)
Processor()
class Processor():
data_directory = 'data/cablegate/cablegate.wikileaks.org/cable'
country_dictionary_path = 'data/countries.csv'
countries = []
country_frequency = nltk.probability.FreqDist()
file_regex = re.compile("\.html$")
counts = {
'files_to_process':0,
'files_processed':0,
'files_not_processed':0
}
def __init__(self):
logging.info('Processor()')
self.load_countries()
self.process()
def process(self):
logging.info('Processor.process')
self.read_files()
def load_countries(self):
logging.info('Processor.load_countries')
try:
file = open(self.country_dictionary_path,'r')
except OSError:
logging.warning('Processor.CANNOT OPEN FILE '+path)
return
countries = csv.reader(file, delimiter=',')
for row in countries:
self.countries.append(row[1].lower())
def read_files(self):
logging.info('Processor.read_files')
try:
for root, dirs, files in os.walk(self.data_directory):
for name in files:
if self.file_regex.search(name) is not None:
path = root+"/"+name
self.counts['files_to_process'] = self.counts['files_to_process'] + 1
self.read_file(path)
except OSError:
logging.info(str(OSError))
def read_file(self,path):
logging.info('Processor.read_file')
try:
file = open(path)
except OSError:
logging.warning('Processor.CANNOT OPEN FILE '+path)
self.counts['files_not_processed'] = self.counts['files_not_processed'] + 1
return
self.extract_content(file.read())
def extract_content(self,raw):
logging.info('Processor.extract_content')
soup = BeautifulSoup(raw)
cable_table = soup.find("table", { "class" : "cable" })
cable_id = cable_table.findAll('tr')[1].findAll('td')[0]\
.contents[1].contents[0]
if db.cables.find_one({'_id':cable_id}):
self.counts['files_not_processed'] = self.counts['files_not_processed'] + 1
logging.info('Processor.extract_content["CABLE ALREADY EXISTS"]')
self.print_counts()
return
cable = Cable(raw)
cable['_id'] = cable_id
cable['reference_id'] = cable_id
cable['date_time'] = cable_table.findAll('tr')[1].findAll('td')[1]\
.contents[1].contents[0]
cable['classification'] = cable_table.findAll('tr')[1].findAll('td')[2]\
.contents[1].contents[0]
cable['origin'] = cable_table.findAll('tr')[1].findAll('td')[3]\
.contents[1].contents[0]
cable['header'] = nltk.clean_html(str(soup.findAll(['pre'])[0]))
cable['body'] = nltk.clean_html(str(soup.findAll(['pre'])[1]))
db.cables.insert(cable.get())
self.counts['files_processed'] = self.counts['files_processed'] + 1
self.print_counts()
if (self.counts['files_processed'] + self.counts['files_not_processed'])\
== self.counts['files_to_process']:
self.dump_json()
def print_counts(self):
logging.info('Processor.print_counts')
logging.info(str(self.counts['files_to_process'])+" | "+\
str(self.counts['files_processed'])+" | "+\
str(self.counts['files_not_processed']))
def dump_json(self):
logging.info('Processor.dump_json')
CableGateMirror()