Skip to content

Instantly share code, notes, and snippets.

@VIRUXE
Created May 14, 2024 21:43
Show Gist options
  • Save VIRUXE/737752c90d2f066d96806e2ff50a81d4 to your computer and use it in GitHub Desktop.
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
<#
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