Advertisement
upz

FanGirlFiction

upz
May 3rd, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Get-FanLinks
  2. {
  3. Param
  4. (
  5.     [int]$StartPage = 1,
  6.     [int]$EndPage,
  7.     [parameter(mandatory=$true,HelpMessage="Url to show you wish to parse ex. 'https://archiveofourown.org/tags/Assassin's%20Creed%20-%20All%20Media%20Types/works'")]
  8.     [string]$url,
  9.     [switch]$ExportCsv
  10. )
  11.  
  12.     $PostUri = 'https://archiveofourown.org'
  13.     $UrlList = New-Object -TypeName System.Collections.Generic.List[PsObject]
  14.    
  15.     do
  16.     {
  17.         $SearchRootUri = "$($url)?page=$StartPage"
  18.         try
  19.         {
  20.             $WebPage = Invoke-WebRequest -Uri $SearchRootUri
  21.         }
  22.         catch
  23.         {
  24.             Write-Host -ForegroundColor Red "$SearchRootUri is not a valid webpage"
  25.             Write-Host -ForegroundColor Red " is not a valid webpage"
  26.             break;
  27.         }
  28.        
  29.         $links = $null
  30.         $links = $WebPage.Links |
  31.                     where {$_.href -match '\d{1,}$' -and
  32.                     ($_.href.ToCharArray() | where {$_ -eq '/'}).count -eq 2} |
  33.                     select outerText,href -Unique
  34.        
  35.         foreach ($link in $links)
  36.         {
  37.             $hash = @{
  38.             'Page' = $StartPage
  39.             'WebPage' = $SearchRootUri
  40.             'Titel' = $link.outerText
  41.             'Url' = "$PostUri$($link.href)"
  42.             }
  43.    
  44.             $obj = New-Object -TypeName PsObject -Property $hash
  45.             $UrlList.Add($obj)
  46.         }                
  47.         $StartPage++
  48.  
  49.     } until($links -eq $null -or $StartPage -eq $EndPage)
  50.  
  51.     if ($ExportCsv)
  52.     {
  53.         $UrlList | Export-Csv -Path "$env:USERPROFILE\desktop\Links.csv" -Delimiter ';' -Encoding Default -NoTypeInformation -Force
  54.     }
  55.     return $UrlList
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement