Created
May 14, 2024 21:43
-
-
Save VIRUXE/737752c90d2f066d96806e2ff50a81d4 to your computer and use it in GitHub Desktop.
Gets a list of Scavenge and Survive servers, from scavengesurvive.com/servers and api.open.mp/servers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
Both functions produce a list of objects with the following format: | |
Server { | |
Name, | |
Address, | |
Port | |
} | |
#> | |
function Get-OpenMpServers { | |
$openMpApi = "https://api.open.mp/servers/" | |
Write-Host "Reading $openMpApi..." | |
try { | |
$json = Invoke-WebRequest -Uri $openMpApi | |
$servers = $json.Content | ConvertFrom-Json | |
$serverList = @() | |
foreach ($server in $servers) { | |
if ($server.gm -match "(?i)scavenge") { | |
$address = $server.ip -split ':' | |
$serverList += [PSCustomObject]@{ | |
Name = $server.hn | |
Address = $address[0] | |
Port = $address[1] | |
} | |
} | |
} | |
return $serverList | |
} catch { | |
Write-Error "Error retrieving server list from 'open.mp'. Details: $($_.Exception.Message)" | |
return $null | |
} | |
} | |
<# | |
We'll expect that the server list layout did not change | |
and that everyone is adhering to the same convention | |
which is to use the samp:// protocol for the ahref in the first column | |
and a clear name for the second column | |
#> | |
function Get-ScavengeSurviveComServers { | |
$scavengeSurviveCom = "https://scavengesurvive.com/servers/" | |
Write-Host "Reading $scavengeSurviveCom..." | |
try { | |
$html = Invoke-WebRequest -Uri $scavengeSurviveCom | |
# We'll get only the tbody from the table | |
$tableBody = ([regex]::Match($html, "<tbody[^>]*>(.*?)</tbody>", "Singleline")).Groups[1].Value | |
$serverList = @() | |
foreach ($row in [regex]::Matches($tableBody, "<tr>(.*?)</tr>", "Singleline")) { | |
if ($row.Groups[1].Value -match "<td[^>]*><a[^>]*>(.*?)</a></td><td[^>]*>(.*?)</td>") { | |
$serverAddressPort = $matches[1] | |
$serverName = $matches[2] | |
# Split the address and port | |
if ($serverAddressPort -match "([^:]+)(:(\d+))?") { | |
$serverList += [PSCustomObject]@{ | |
Name = $serverName | |
Address = $matches[1] | |
Port = if ($matches[3]) { $matches[3].TrimStart(':') } else { "7777" } | |
} | |
} | |
} | |
} | |
return $serverList | |
} catch { | |
Write-Error "Error retrieving server list from 'scavengesurvive.com'. Details: $($_.Exception.Message)" | |
return $null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment