-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.ps1
316 lines (268 loc) · 12.1 KB
/
start.ps1
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
Import-Module ImportExcel
# Path to the root directory of the script
# Using CLI, Navigate to the root directory of the script and run the script "./start.ps1"
$CPSScriptRoot = $PSScriptRoot
# $CPSScriptRoot = "D:\Code\Repos\US-Mass-Shootings"
# Importing Functions
# Function to edit cases
$EditCases = Join-Path -Path $CPSScriptRoot -ChildPath "Functions" | Join-Path -ChildPath "Edit-Cases.ps1"
. $EditCases
# Function to get the Mother Jones Database
$GetMotherJonesDB = Join-Path -Path $CPSScriptRoot -ChildPath 'Functions' | Join-Path -ChildPath 'Get-MotherJonesDB.ps1'
. $GetMotherJonesDB
# Variables
$Date = Get-Date -Format "yyyyMMdd"
$Random = Get-Random
$ExportPath = Join-Path -Path $CPSScriptRoot -ChildPath 'Export'
$year = Get-Date -Format "yyyy"
$DatasetYear = "1982-$year"
# Import and Export FileName Variables
$ExportWebView = Join-Path -Path $ExportPath -ChildPath 'WebView.html'
$ExportCHEdition = Join-Path -Path $ExportPath -ChildPath "Codeholics - Mass Shootings Database $DatasetYear.csv"
$ExportedCHEditionDW = Join-Path -Path $CPSScriptRoot -ChildPath 'DataWorld' | Join-Path -ChildPath "Codeholics - Mass Shootings Database $DatasetYear.csv"
$ImportCSVPath = Join-Path -Path $ExportPath -ChildPath "Mother Jones - Mass Shootings Database $DatasetYear.csv"
# Log Variables
$LogPath = Join-Path -Path $CPSScriptRoot -ChildPath 'Logs'
$LogName = "$Date-$Random.log"
$LogFilePath = Join-Path -Path $LogPath -ChildPath $LogName
$Version = "2.0"
# Path to the replacements file for the Edit-Cases function
$replacements = Join-Path -Path $CPSScriptRoot -ChildPath "replacements.json"
# Start Logging
Start-Log -LogPath $LogPath -LogName $LogName -ScriptVersion $Version
# create Export folder if not exist
if (!(Test-Path $ExportPath)) {
New-Item -Path $ExportPath -ItemType Directory
Write-LogInfo -LogPath $LogFilePath -Message "[$(Get-Date)] Created Export Folder: [$ExportPath]" -ToScreen
}elseif (Test-Path $ExportPath) {
Remove-Item -Path $ExportPath -Recurse -Force
New-Item -Path $ExportPath -ItemType Directory
Write-LogInfo -LogPath $LogFilePath -Message "[$(Get-Date)] Export Folder: [$ExportPath] already exists" -ToScreen
}
# Get Mother Jones CSV and create a new copy without duplicate headers.
try {
Get-MotherJonesDB -Output $ExportPath
Write-LogInfo -LogPath $LogFilePath -Message "[$(Get-Date)] Mother Jones CSV copied to: [$ExportPath]" -ToScreen
}
Catch{
Write-LogError -LogPath $LogFilePath -Message "[$(Get-Date)] Mother Jones CSV copy failed" -ToScreen
}
# Check and confrim the new CSV is avaiable before continuing
try {
$Spreadsheet = (Import-csv -Path $ImportCSVPath)
Write-LogInfo -LogPath $LogFilePath -Message "[$(Get-Date)] Mother Jones CSV imported from: [$ImportCSVPath]" -ToScreen
}catch {
Write-LogError -LogPath $LogFilePath -Message "[$(Get-Date)] Mother Jones CSV import failed" -ToScreen
}
# Main loop that will correct the data from MJ to build CH edition
Write-LogInfo -LogPath $LogFilePath -Message "[$(Get-Date)] Starting column data correction loop" -ToScreen
# Formatting MJ Data for CH Edition
$Data = @()
foreach ($item in $Spreadsheet) {
# Initializations
$gender = $null
$type = $null
$mental_health_sources = $null
$age_of_shooter = $null
$weapon_details = $null
$latitude = $null
$longitude = $null
$year = $null
$changes = $null
$sources = $null
$sources_additional_age = $null
$total_victims = $null
$injured = $null
$fatalities = $null
$summary = $null
$date = $null
$location = $null
$case = $null
$mental_health_details = $null
$where_obtained = $null
$weapons_obtained_legally = $null
$weapon_type = $null
$race = $null
$location_2 = $null
$prior_signs_mental_health_issues = $null
$gender = $null
# Variable Assignments
$gender = $item.gender.length
$type = $item.type
$mental_health_sources = $item.mental_health_sources
$age_of_shooter = $item.age_of_shooter
$weapon_details = $item.weapon_details
$latitude = $item.latitude
$longitude = $item.longitude
$year = $item.year
$changes = $item.changes
$sources = $item.sources
$sources_additional_age = $item.sources_additional_age
$total_victims = $item.total_victims
$injured = $item.injured
$fatalities = $item.fatalities
$summary = $item.summary
$date = $item.date
$location = $item.location
$case = $item.case
$mental_health_details = $item.mental_health_details
$where_obtained = $item.where_obtained
$weapons_obtained_legally = $item.weapons_obtained_legally
$weapon_type = $item.weapon_type
$race = $item.race
$location_2 = $item.location_2
$prior_signs_mental_health_issues = $item.prior_signs_mental_health_issues
$gender = $item.gender
$changes = $item.changes
#############################################
# General formatting changes to the data
#############################################
# Splitting city out of location
$CityRaw = $location
$CityOnly = $CityRaw -replace ',.*',''
$City = $CityOnly.trim('')
# Splitting out state from location
$StateRaw = $location
$StateOnly = $StateRaw -replace '.*,',''
$state = $StateOnly.trim('')
# force first character of race caps
$RaceCaps = $race
$race = $RaceCaps.toCharArray()[0].tostring().toUpper() + $RaceCaps.remove(0,1)
$race = $race -replace '^[-]+$', $null
# force first character of location_2 caps
$LocationCaps = $location_2
$location_2 = $LocationCaps.toCharArray()[0].tostring().toUpper() + $LocationCaps.remove(0, 1)
$location_2 = $location_2.trim('')
# force first character of prior_signs_mental_health_issues caps
$prior_signs_mental_health_issuesCaps = $prior_signs_mental_health_issues
$prior_signs_mental_health_issues = $prior_signs_mental_health_issuesCaps.toCharArray()[0].tostring().toUpper() + $prior_signs_mental_health_issuesCaps.remove(0, 1)
$prior_signs_mental_health_issues = $prior_signs_mental_health_issues -replace '^[-]+$', $null
# force first character of weapon_type
$WeaponTypeCaps = $weapon_type
$Weapon_type = $WeaponTypeCaps.toCharArray()[0].tostring().toUpper() + $WeaponTypeCaps.remove(0, 1)
# force first character of gender caps
$GenderCaps = $gender
$gender = $GenderCaps.toCharArray()[0].tostring().toUpper() + $GenderCaps.remove(0,1)
# force first character of weapons_obtained_legally caps
$weapons_obtained_legallyCaps = $weapons_obtained_legally
$weapons_obtained_legally = $weapons_obtained_legallyCaps.toCharArray()[0].tostring().toUpper() + $weapons_obtained_legallyCaps.remove(0,1)
$weapons_obtained_legally = $weapons_obtained_legally -replace '^[-]+$', $null
# force first character of gender caps
$where_obtainedCaps = $where_obtained
$where_obtained = $where_obtainedCaps.toCharArray()[0].tostring().toUpper() + $where_obtainedCaps.remove(0,1)
$where_obtained = $where_obtained -replace '^[-]+$', $null
# force first character of mental_health_details caps
$mental_health_detailsCaps = $mental_health_details
$mental_health_details = $mental_health_detailsCaps.toCharArray()[0].tostring().toUpper() + $mental_health_detailsCaps.remove(0,1)
$mental_health_details = $mental_health_details -replace '^[-]+$', $null
# Removing - from age_of_shooter
$age_of_shooter = $age_of_shooter -replace '^[-]+$', $null
# Removing - from weapon_details
$weapon_details = $weapon_details -replace '^[-]+$', $null
# Removing - from mental_health_sources
$mental_health_sources = $mental_health_sources -replace '^[-]+$', $null
# Removing - from latitude
$latitude = $latitude -replace '^[-]+$', $null
# Removing - from longitude
$longitude = $longitude -replace '^[-]+$', $null
#the gender values are not consistnt, abbreviating the gender value for more consistent data
$GenderValueLength = $gender
if($GenderValueLength -eq 4){
$gender = 'M'
}elseif ($GenderValueLength -eq 6) {
$gender = 'F'
}elseif($GenderValueLength -eq 13){
$gender = 'M/F'
}else{
#Do nothing if the value length is 1. (result already "M", "F" or "M/F")
$gender = $gender
}
# weapon_type values
if ($weapon_type -eq '2 handguns') {
$weapon_type = 'Two handguns'
}
if ($case -eq 'Philidelphia neighborhood shooting') {
$case = "Philadelphia neighborhood shooting"
}
if ($location -like 'Lousiana') {
$location = $location -replace 'Lousiana', 'Louisiana'
}
if ($state -eq 'Lousiana') {
$state = $state -replace 'Lousiana', 'Louisiana'
}
# Final CleanData Array for CH Edition
$Data += [PSCustomObject]@{
case = $case
location = $location
city = $City
state = $State
date = $date
summary = $summary
fatalities = $fatalities
injured = $injured
total_victims = $total_victims
location_2 = $Location_2
age_of_Shooter = $age_of_shooter
prior_signs_mental_health_issues = $prior_signs_mental_health_issues
mental_health_details = $mental_health_details
weapons_obtained_legally = $weapons_obtained_legally
where_obtained = $where_obtained
weapon_type = $weapon_type
weapon_details = $weapon_details
race = $race
gender = $gender
sources = $sources
mental_health_sources = $mental_health_sources
sources_additional_age = $sources_additional_age
latitude = $latitude
longitude = $longitude
type = $type
year = $year
changes = $changes
}
}
## Case changes only
$ModifiedCases = @()
foreach ($item in $Data) {
$item = Edit-Cases -item $item -replacementsPath $replacements
$ModifiedCases += $item
}
# Correct order of columns
$DataFinal = $ModifiedCases | Select-Object -Property case, location, city, state, date, summary, fatalities, injured, total_victims, location_2, age_of_Shooter, prior_signs_mental_health_issues, mental_health_details, weapons_obtained_legally, where_obtained, weapon_type, weapon_details, race, gender, sources, mental_health_sources, sources_additional_age, latitude, longitude, type, year, changes
#Export clean dataset for data.world
try {
$DataFinal | Export-CSV -path $ExportCHEdition -NoTypeInformation
Write-LogInfo -LogPath $LogFilePath -Message "[$(Get-Date)] Exported clean CH Edition dataset for data.world [$ExportCHEdition]" -ToScreen
} catch {
Write-LogError -LogPath $LogFilePath -Message "[$(Get-Date)] Exporting clean CH Edition dataset for data.world [$ExportCHEdition]" -ToScreen
}
# Executing SQLPort and Statistics
if ($null -ne $DataFinal) {
# Push data to SQLite DB
& "$CPSScriptRoot\SQLPort.ps1"
# Generate Statistics.md
& "$CPSScriptRoot\Statistics.ps1"
}
# Import CH Edition to Export DW friendly version
$ImportedCHEdition = Import-Csv -Path $ExportCHEdition
# Iterate through each row and each column to remove single and double quotes
foreach ($row in $ImportedCHEdition) {
foreach ($column in $row.PSObject.Properties.Name) {
$row.$column = $row.$column -replace "'", "" -replace "''", "" -replace '"', ""
}
}
# Export DW friendly version of CH Edition
$ImportedCHEdition | Export-Csv -Path $ExportedCHEditionDW -NoTypeInformation
Write-LogInfo -LogPath $LogFilePath -Message "[$(Get-Date)] Exported DW friendly version of CH Edition [$ExportedCHEditionDW]" -ToScreen
# Export CH Edition to JSON
$ImportedCHEdition | ConvertTo-Json | Out-File -FilePath "$ExportPath\Codeholics - Mass Shootings Database $DatasetYear.json"
# HTML Export of the data. Will popup once script is completed.
try {
New-HTML {
New-HTMLTable -DataTable $Data -Title 'Table of records' -HideFooter -PagingLength 200 -Buttons excelHtml5, searchPanes
} -FilePath $ExportWebView -Online
Write-LogInfo -LogPath $LogFilePath -Message "[$(Get-Date)] Exported WebView [$ExportWebView]" -ToScreen
}catch{
Write-LogError -LogPath $LogFilePath -Message "[$(Get-Date)] Exporting WebView [$ExportWebView]" -ToScreen
}
Stop-Log -LogPath $LogFilePath -NoExit