-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSMarvel.psm1
179 lines (157 loc) · 6.22 KB
/
PSMarvel.psm1
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
function New-MarvelTimeSpan {
#API Keys and TS
$global:MarvelTS = New-TimeSpan -End (Get-Date -Year 2018 -Month 1 -Day 1)
[Environment]::GetEnvironmentVariables("Machine") | out-null
$checkVariable = Get-ChildItem Env:MarvelPublicKey -ErrorAction SilentlyContinue
if ($checkVariable.count -eq 0) {
if (!(Test-Path variable:global:MarvelPublicKey)) {
$PublicKeyPrompt = Read-Host -Prompt "Write your public key from the Marvel portal"
[Environment]::SetEnvironmentVariable("MarvelPublicKey", "$PublicKeyPrompt", "Machine")
$global:MarvelPublicKey = $PublicKeyPrompt
$PrivateKeyPrompt = Read-Host -Prompt "Write your private key from the Marvel portal"
[Environment]::SetEnvironmentVariable("MarvelPrivateKey", "$PrivayeKeyPrompt", "Machine")
$global:MarvelPrivateKey = $PrivateKeyPrompt
}
} else {
$global:MarvelPublicKey = $env:MarvelPublicKey
$global:MarvelPrivateKey = $env:MarvelPrivateKey
}
#Form the hash as Marvel requires
$ToHash = $MarvelTS.ToString() + $MarvelPrivateKey.ToString() + $MarvelPublicKey.ToString()
$StringBuilder = New-Object System.Text.StringBuilder
[System.Security.Cryptography.HashAlgorithm]::Create("MD5").ComputeHash([System.Text.Encoding]::UTF8.GetBytes($ToHash)) | % {
[Void]$StringBuilder.Append($_.ToString("x2"))
}
$global:MD5 = $StringBuilder.ToString()
}
function Get-MarvelRandomCharacter {
New-MarvelTimeSpan
$MarvelOffset = Get-Random -Maximum 1490
$Url = "https://gateway.marvel.com:443/v1/public/characters?offset=$MarvelOffset&limit=1&apikey=$MarvelPublicKey&hash=$MD5&ts=$MarvelTS"
$Results = Invoke-WebRequest $Url
$Content = $results.Content
$Output = ConvertFrom-Json $Content
Write-Host "`n## Character name: `n" -ForegroundColor Green
$Name = $Output.data.results.name
Write-host " " $Name
Write-Host "`n## Description: `n" -ForegroundColor Green
$Description = $Output.data.results.description
if ($Description.Length -ge 0) {
Write-host " No description available."
} else {
Write-host $Description
}
Write-Host "`n## Series: `n" -ForegroundColor Green
$Series = $output.data.results.series.items.name
if ($Series -gt 0) {
$Series | % {Write-host " " $_}
} else {
Write-host " N/A"
}
Write-host "`n## Comics: `n" -ForegroundColor Green
$Comics = $output.data.results.comics.items.name
if ($Comics -gt 0) {
$Comics | % {Write-host " " $_}
} else {
Write-host " N/A"
}
}
function Find-MarvelCharacter {
[CmdletBinding()]
param(
[Parameter(Mandatory)][string]$StartWith
)
New-MarvelTimeSpan
$MarvelOffset = Get-Random -Maximum 1490
$Url = "https://gateway.marvel.com:443/v1/public/characters?nameStartsWith=$StartWith&apikey=$MarvelPublicKey&hash=$MD5&ts=$MarvelTS"
$Results = Invoke-WebRequest $Url
$Content = $results.Content
$Output = ConvertFrom-Json $Content
Write-Host "`n## Character names: `n" -ForegroundColor Green
$Output.data.results.name | % {Write-host " " $_}
}
function Get-MarvelCharacter {
[CmdletBinding()]
param(
[Parameter(Mandatory)][string]$Name
)
New-MarvelTimeSpan
$MarvelOffset = Get-Random -Maximum 1490
$NameModified = $Name.Replace(" ","%20")
$Url = "https://gateway.marvel.com:443/v1/public/characters?name=$NameModified&limit=1&apikey=$MarvelPublicKey&hash=$MD5&ts=$MarvelTS"
$Results = Invoke-WebRequest $Url
$Content = $results.Content
$Output = ConvertFrom-Json $Content
Write-Host "`n## Character name: `n" -ForegroundColor Green
$Name = $Output.data.results.name
Write-host " " $Name
Write-Host "`n## Description: `n" -ForegroundColor Green
$Description = $Output.data.results.description
if ($Description.Length -ge 0) {
Write-host " No description available."
} else {
Write-host $Description
}
Write-Host "`n## Series: `n" -ForegroundColor Green
$Series = $output.data.results.series.items.name
if ($Series -gt 0) {
$Series | % {Write-host " " $_}
} else {
Write-host "N/A"
}
Write-host "`n## Comics: `n" -ForegroundColor Green
$Comics = $output.data.results.comics.items.name
if ($Comics -gt 0) {
$Comics | % {Write-host " " $_}
} else {
Write-host "N/A"
}
}
function Find-MarvelComic {
[CmdletBinding()]
param(
[Parameter(Mandatory)][string]$StartWith
)
New-MarvelTimeSpan
$Url = "https://gateway.marvel.com:443/v1/public/comics?titleStartsWith=$StartWith&apikey=$MarvelPublicKey&hash=$MD5&ts=$MarvelTS"
$Results = Invoke-WebRequest $Url
$Content = $results.Content
$Output = ConvertFrom-Json $Content
Write-Host "`n## Comic info: `n" -ForegroundColor Green
$Output.data.results | select title, description, format, pageCount
}
function Get-MarvelComic {
[CmdletBinding()]
param(
[Parameter(Mandatory)][string]$Title
)
New-MarvelTimeSpan
$issueNumber = $Title.Split("#")[1]
$startYear = $Title.Split("(")[1].Split(")")[0]
$Title = $Title.split("(")[0] -replace ".$"
$Url = "https://gateway.marvel.com:443/v1/public/comics?title=$Title&issueNumber=$issueNumber&startYear=$startYear&apikey=$MarvelPublicKey&hash=$MD5&ts=$MarvelTS"
$Results = Invoke-WebRequest $Url
$Content = $results.Content
$Output = ConvertFrom-Json $Content
Write-Host "`n## Comic title: `n" -ForegroundColor Green
$Name = $Output.data.results.title
Write-host " " $Name
Write-Host "`n## Description: `n" -ForegroundColor Green
$Description = $Output.data.results.description
if ($Description.Length -eq 0) {
Write-host " No description available."
} else {
Write-host $Description
}
Write-Host "`n## Series: `n" -ForegroundColor Green
$Series = $Output.data.results.series.name
if ($Series -ne 0) {
$Series | % {Write-host " " $_}
} else {
Write-host "N/A"
}
Write-host "`n## Format: `n" -ForegroundColor Green
Write-host " " $Output.data.results.format
Write-host "`n## Creators: `n" -ForegroundColor Green
$Output.data.results.creators.items | % {Write-host " " $_.name "/" $_.role}
}