-
Notifications
You must be signed in to change notification settings - Fork 3
/
Get-FlagsfromMagelo.ps1
147 lines (136 loc) · 4.22 KB
/
Get-FlagsfromMagelo.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
##Get's charater flag data from ProjectEQ Magelo and exports to CSV
##if anything changes on Magelo, it will likely break this script
##############Paste toon names here##############
$toons = @"
Toon1
Toon2
Toon3
Toon4
"@
$toons = ($toons -split "`n").Trim()
$FlagsOrderedFull = @()
Clear-Host
if (($ToonCSV = Read-Host -Prompt "Would you like one CSV per toon name [Y/N]") -eq "Y")
{
$ToonCSV = $true
}
if (($CSVSavePath = Read-Host -Prompt "Enter CSV save Path [or press ENTER for $env:TEMP] (Make sure the folder exists)") -eq "")
{
$CSVSavePath = $env:TEMP
}
$CSVFileName = 'CharacterFlags.csv'
$CSVSavePathFull = Join-Path $CSVSavePath $CSVFileName
Write-Host 'Please make sure all flag CSV files are closed before continuing' -ForegroundColor Cyan
pause
foreach ($toon in $toons)
{
Write-Host "Beginning parse of $toon" -ForegroundColor Cyan
$HTML = Invoke-WebRequest "http://projecteq.net/magelo/index.php?page=flags&char=$toon"
$outer = $HTML.ParsedHtml.body.outerHTML
$inner = $HTML.ParsedHtml.body.innerText
$outerContent = $outer -split "`n"
$innerContent = ($inner -split "`n").trim()
$trimmedInnerContent = $innerContent[50 .. 222] #Starts on line 50 of Magelo - needs to be update if/when Magelo is updated
$a = 1
$ArrayForEach = @(foreach ($line in $outerContent) #Add Line numbers
{
"$a `t" + $line
$a++
})
$b = 1
$ArrayForEachInner = @(foreach ($line in $innerContent) #Add Line numbers
{
"$b `t" + $line
$b++
})
$checkMatches = ($ArrayForEach | Where-Object { $_ -match 'check' }) #Get lines where that conatain the word check
$Flags = @(foreach ($check in $checkMatches) #Massage data
{
$CheckNumber = ($check).split("`t")[0].Trim()
$Checked = ($check).split("`t")[1]
if ($checked -match 'check1')
{
$checked = $true
}
else
{
$Checked = $false
}
$NewCheckNumber = [int]$CheckNumber + 1 #Added [int] to force integer else the number was just appended to the end of a string
$FlagName = $ArrayForEach | Where-Object { $_ -match "^" + [regex]::escape($NewCheckNumber) } #https://stackoverflow.com/questions/10400035/match-strings-stored-in-variables-using-powershell
$FlagName = ($FlagName -split '<TD vAlign=middle width="100%" noWrap>')
$FlagName = ($FlagName[1] -split '</TD>')[0]
if ($FlagName -match ' ') #Gets rid of HTML code
{
$FlagName = ($FlagName -split ' ')[1]
$FlagName = ($FlagName -split '...</A>')[0]
}
if ($FlagName -match '&') #Changes & to &
{
$FlagName = ($FlagName -replace '&', '&')
}
[PSCustomObject] @{
Name = $toon
Flagged = $Checked
FlagName = $FlagName
}
})
$FlagsOrdered = @(foreach ($t in $trimmedInnerContent) #Sort and order data
{
$switch = 0
foreach ($Flag in $Flags)
{
if (($t -eq $Flag.FlagName) -and ($switch -eq 0)) #adding the switch avoids duplicate entries
{
$switch = 1
[PSCustomObject] @{
Name = $Flag.Name
Flagged = $Flag.Flagged
FlagName = $Flag.FlagName
}
}
}
if ($t -eq '')
{
[PSCustomObject] @{
Name = ''
Flagged = ''
FlagName = ''
}
}
})
foreach ($t in $trimmedInnerContent) #Sort and order data. Store all flags in $FlagsOrderedFull array.
{
$switch = 0
foreach ($Flag in $Flags)
{
if (($t -eq $Flag.FlagName) -and ($switch -eq 0)) #adding the switch avoids duplicate entries
{
$switch = 1
$FlagsOrderedFull += [PSCustomObject] @{
Name = $Flag.Name
Flagged = $Flag.Flagged
FlagName = $Flag.FlagName
}
}
}
if ($t -eq '')
{
$FlagsOrderedFull += [PSCustomObject] @{
Name = ''
Flagged = ''
FlagName = ''
}
}
}
Start-Sleep -Seconds 1
if ($ToonCSV -eq 'y')
{
$toonflags = $toon + 'Flags'
Write-Host "Writing $toon's flag data to $CSVSavePath\$toonflags.csv..." -ForegroundColor Yellow
$FlagsOrdered | Export-Csv -Path "$CSVSavePath\$toonflags.csv" -NoTypeInformation
}
}
Write-Host "Saving all flag data to $CSVSavePathFull..." -ForegroundColor Yellow
$FlagsOrderedFull | Export-Csv -Path $CSVSavePathFull -NoTypeInformation
Invoke-Item $CSVSavePath