Advertisement
Guest User

Untitled

a guest
Dec 14th, 2023
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 5.30 KB | Software | 0 0
  1. # code by Matcha or IMineMantis
  2. # place code into a ".ps1" file in the same directory as "waifu_solitaire.exe"
  3. # when the code is run you can input any tag and it will scrape images from gelbooru
  4. # the images are in the "waifus" folder and another with the name of the tag you used
  5.  
  6.  
  7. $waifu_chosen = $false
  8.  
  9. # choose waifu
  10. while (-not $waifu_chosen)
  11. {
  12.  
  13.     # prompt for input and replaces spaces with underscores
  14.     $waifu = Read-Host -Prompt "tag"
  15.     $waifu = $waifu -replace " ", "_"
  16.  
  17.     $auto_url = "https://gelbooru.com/index.php?page=autocomplete2&term=$waifu"
  18.  
  19.     # send request to tag auto complete and handles the output
  20.     $responce = Invoke-WebRequest -Uri $auto_url -SessionVariable session
  21.     $output = $responce | ConvertFrom-Json | ForEach-Object -Process {$_.value}
  22.  
  23.     # check for return (if there are no tags it returns nothing)
  24.     if ($output.Length -ge 1)
  25.     {
  26.         # list all outputs
  27.         for($i = 1; $i -le $output.Length; $i++)
  28.         {
  29.             Write-Host "${i}: " $output[$i - 1]
  30.         }
  31.  
  32.         $chosen = $false
  33.  
  34.         # force valid choice
  35.         while( -not $chosen)
  36.         {
  37.             # prompt input and make value
  38.             $num = Read-Host -Prompt "select (blank to go back)"
  39.             $num = $num - "0"
  40.  
  41.             # break if 0(default value) allowing to go back
  42.             if ( $num -eq 0)
  43.             {
  44.                 break
  45.             }
  46.  
  47.             #check it is a number, not higher than the amount listed, and more than one
  48.             if($num -match "\d" -and $num -le $output.Length -and $num -ge 1)
  49.             {
  50.                 # set value to chosen and change variables
  51.                 $waifu = $output[$num - 1]
  52.                 $waifu_chosen = $true
  53.                 $chosen = $true
  54.                        
  55.                 Write-Host "-------------"
  56.                 Write-Host "nice"
  57.                 Write-Host "-------------"
  58.             }
  59.             else
  60.             {
  61.                 Write-Host "invalid input"
  62.             }
  63.         }
  64.     }
  65.  
  66. }
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. if ($true)
  74. {
  75.     # set path, if it doesnt exist make it
  76.     $path = "waifus\$waifu"
  77.     if (-not (Test-Path $path)){
  78.         mkdir "waifus\$waifu" | Out-Null
  79.     }
  80.  
  81.    
  82.     $num_images = 0
  83.     $chosen = $false
  84.     # loops till chosen
  85.     while (-not $chosen)
  86.     {
  87.         # prompt
  88.         $num_images = Read-Host -Prompt "how many imaged (default 52)"
  89.        
  90.         # if blank set to 52 for full deck
  91.         if ( $num_images -eq "" )
  92.         {
  93.             $num_images = 52
  94.         }
  95.         # if its a valid number and its more than one then continue
  96.         elseif ($num_images -match "\d" -and $num_images -ge 1) {
  97.             $chosen = $true
  98.         }
  99.         else{
  100.             Write-Host "invalid number"
  101.         }
  102.     }
  103.    
  104.     # setting cookies to allow fringe content (doesnt work currently not sure why)
  105.     $cookie = [system.net.cookie]::new("fringeBenefits","yup")
  106.     $session.Cookies.Add( "https://gelbooru.com/index.php" ,$cookie)
  107.    
  108.     $full = $true
  109.     $match_count = 0
  110.     $page = 0
  111.     $last_url = ""
  112.  
  113.     while($full)
  114.     {
  115.         # go through multiples of 42 (42 images per page)
  116.         $start_page = $page * 42
  117.  
  118.         $url_base = "https://gelbooru.com/index.php?page=post&s=list&tags=$waifu+rating%3aexplicit+sort%3ascore%3adesc+-animated_gif+-animated+-webm+solo&pid=$start_page"
  119.         # $url_base = "https://gelbooru.com/index.php?page=post&s=list&tags=$waifu+rating%3ageneral+sort%3ascore%3adesc+-animated_gif+-animated+-webm+solo&pid=$start_page"
  120.        
  121.         # gets a list of the images from the site
  122.         $responce = Invoke-WebRequest -Uri $url_base -SessionVariable $session
  123.  
  124.         # Set-Content -value $responce -Path ".\waifus\out.html"
  125.  
  126.         Write-Host "--------------"
  127.         # find all the matches for id and href and then do something to each of them
  128.         ($responce | Select-String -Pattern 'id="p.\d*" href="(.*?)"' -AllMatches).Matches.Value |
  129.         ForEach-Object {
  130.             # finds matches again so that $matches is populated
  131.             $_ -match 'id="p.\d*" href="(.*?)"' | Out-Null
  132.  
  133.             $match_count = ($match_count + 1)
  134.             # fill url with image href
  135.             $url = ($matches[1] -replace "amp;","")
  136.  
  137.             # prevents looping on last image if there arent enough images
  138.             if ($last_url -like $url) {
  139.                 write-host "------------"
  140.                 write-host "out of images"
  141.                 write-host "------------"
  142.                 break
  143.             }
  144.            
  145.             $last_url = $url
  146.  
  147.             $responce = Invoke-WebRequest -Uri $url -SessionVariable $session
  148.             # find the href that maches to how gelbooru stores images
  149.             $responce -match 'href="(https://img3.gelbooru.com/images/.*?\.(.*?))"'
  150.            
  151.             Write-Host $Matches[1]
  152.  
  153.             # dl the images and store it
  154.             $out_path = "$path\$match_count." + $Matches[2]
  155.             $img = Invoke-WebRequest $matches[1] -OutFile $out_path -SessionVariable $session
  156.  
  157.             # break when reached number of images
  158.             if ($match_count -ge $num_images)
  159.             {
  160.                 break
  161.             }
  162.         }
  163.         # cycle 1 page after gone through all 42 iamges
  164.         $page++
  165.  
  166.     }
  167. }
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement