gdunc

List-Ignored - tests ignored models for invalid URLs

Jan 3rd, 2021 (edited)
898
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2.   List-Ignored.ps1 -file <file.json> [-delay <seconds>]
  3.  
  4.   Where: file.json = CTBRec config file, (usually ctbrec.json or server.json)
  5.          seconds   = Delay between each Web Request, (avoid possible error 503), valid range 0.1-90
  6.  
  7.   example: List-Ignored.ps1 -file server.json
  8.            List-Ignored.ps1 -file ctbrec.json -delay 3
  9.  
  10.   List ignored models with pages that don't resolve, (error 404, etc), or CB models that are banned.
  11.  
  12.   Output will be to Ignored-Model-Status.txt in the same dir as List-Ignored.ps1.
  13.   File will be overwritten in consecutive runs.
  14.  
  15.   Tested on PowerShell 7, Windows 10 and DietPi 6.33.3, (RasPi).
  16.  
  17.   *** NOTE: Only tested with a few models on CB because I only monitor 1-3 models there, so if
  18.             no-one tells me anything I'll assume it works on all sites.
  19. #>
  20.  
  21. param (
  22.     [Parameter(Mandatory=$true)][string]$file,
  23.     [ValidateRange(0.1,90)][decimal]$delay
  24. )
  25.  
  26. function Test-Url ([string] $Url) {
  27.   $req = [system.Net.HttpWebRequest]::Create($url)
  28.  
  29.   try {
  30.     $res = $req.GetResponse()
  31.   }
  32.   catch [System.Net.WebException] {
  33.     $res = $_.Exception.Response
  34.   }
  35.   $res.Close()
  36.   return $res
  37. }
  38.  
  39. function Test-Models ([string] $infile) {
  40.   $json = (Get-Content $infile | ConvertFrom-Json)
  41.   for ($i = 0; $i -lt $json.modelsIgnored.Count; $i++) {
  42.     $statusCode = [int](Test-Url $json.modelsIgnored[$i].url).StatusCode
  43.     "$($i) - $($json.modelsIgnored[$i].name) - $($statusCode)"
  44.     if ($statusCode -gt 399) {
  45.       "$($json.modelsIgnored[$i].name) - Error $($statusCode)" | Out-File -FilePath Ignored-Model-Status.txt -Append
  46.     } else {
  47.       if ($json.modelsIgnored[$i].type -match 'chaturbate') {
  48.         $data = Invoke-WebRequest -Uri $json.modelsIgnored[$i].url
  49.         if ($data -match 'This room has been banned') {
  50.           "$($json.modelsIgnored[$i].name) - Banned" | Out-File -FilePath Ignored-Model-Status.txt -Append
  51.         }
  52.       }
  53.     }
  54.     if ($delay -gt 0) {
  55.       Start-Sleep -Seconds $delay
  56.     }
  57.   }
  58. }
  59.  
  60. if (Test-Path ./Ignored-Model-Status.txt) {
  61.   Remove-Item ./Ignored-Model-Status.txt -Force
  62. }
  63.  
  64. Test-Models $file
  65.  
Add Comment
Please, Sign In to add comment