Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathprocess.py
More file actions
Latest commit
160 lines (124 loc) · 4.23 KB
/
Copy pathprocess.py
File metadata and controls
160 lines (124 loc) · 4.23 KB
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
importlogging
importos
importre
importsys
importxml
importcsv
importsubprocess
importnltk
sys.path.append("lib")
importpymongo
fromBeautifulSoupimportBeautifulSoup
logging.basicConfig(level=logging.DEBUG, format="%(levelname)-8s %(message)s")
mongo_conn=pymongo.Connection('localhost', 27017)
db=mongo_conn['wikileaks']
classCable():
raw=""
attrs= {}
def__init__(self,raw):
logging.info('Cable()')
self.raw=raw
def__getitem__(self,name):
ifname=='raw':
returnself.raw
ifnameinself.attrs:
returnself.attrs[name]
else:
returnNone
def__setitem__(self,name,value):
self.attrs[name] =value
defget(self):
returnself.attrs
classCableGateMirror():
mirror_directory='data/cablegate/'
def__init__(self):
logging.info('CableGateMirror()')
self.update()
defupdate(self):
logging.info('CableGateMirror.update')
#subprocess.call(["httrack",'--update'],cwd=self.mirror_directory)
Processor()
classProcessor():
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()
defprocess(self):
logging.info('Processor.process')
self.read_files()
defload_countries(self):
logging.info('Processor.load_countries')
try:
file=open(self.country_dictionary_path,'r')
exceptOSError:
logging.warning('Processor.CANNOT OPEN FILE '+path)
return
countries=csv.reader(file, delimiter=',')
forrowincountries:
self.countries.append(row[1].lower())
defread_files(self):
logging.info('Processor.read_files')
try:
forroot, dirs, filesinos.walk(self.data_directory):
fornameinfiles:
ifself.file_regex.search(name) isnotNone:
path=root+"/"+name
self.counts['files_to_process'] =self.counts['files_to_process'] +1
self.read_file(path)
exceptOSError:
logging.info(str(OSError))
defread_file(self,path):
logging.info('Processor.read_file')
try:
file=open(path)
exceptOSError:
logging.warning('Processor.CANNOT OPEN FILE '+path)
self.counts['files_not_processed'] =self.counts['files_not_processed'] +1
return
self.extract_content(file.read())
defextract_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]
ifdb.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()
defprint_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']))
defdump_json(self):
logging.info('Processor.dump_json')
CableGateMirror()