Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # code by Matcha or IMineMantis
- # place code into a ".ps1" file in the same directory as "waifu_solitaire.exe"
- # when the code is run you can input any tag and it will scrape images from gelbooru
- # the images are in the "waifus" folder and another with the name of the tag you used
- $waifu_chosen = $false
- # choose waifu
- while (-not $waifu_chosen)
- {
- # prompt for input and replaces spaces with underscores
- $waifu = Read-Host -Prompt "tag"
- $waifu = $waifu -replace " ", "_"
- $auto_url = "https://gelbooru.com/index.php?page=autocomplete2&term=$waifu"
- # send request to tag auto complete and handles the output
- $responce = Invoke-WebRequest -Uri $auto_url -SessionVariable session
- $output = $responce | ConvertFrom-Json | ForEach-Object -Process {$_.value}
- # check for return (if there are no tags it returns nothing)
- if ($output.Length -ge 1)
- {
- # list all outputs
- for($i = 1; $i -le $output.Length; $i++)
- {
- Write-Host "${i}: " $output[$i - 1]
- }
- $chosen = $false
- # force valid choice
- while( -not $chosen)
- {
- # prompt input and make value
- $num = Read-Host -Prompt "select (blank to go back)"
- $num = $num - "0"
- # break if 0(default value) allowing to go back
- if ( $num -eq 0)
- {
- break
- }
- #check it is a number, not higher than the amount listed, and more than one
- if($num -match "\d" -and $num -le $output.Length -and $num -ge 1)
- {
- # set value to chosen and change variables
- $waifu = $output[$num - 1]
- $waifu_chosen = $true
- $chosen = $true
- Write-Host "-------------"
- Write-Host "nice"
- Write-Host "-------------"
- }
- else
- {
- Write-Host "invalid input"
- }
- }
- }
- }
- if ($true)
- {
- # set path, if it doesnt exist make it
- $path = "waifus\$waifu"
- if (-not (Test-Path $path)){
- mkdir "waifus\$waifu" | Out-Null
- }
- $num_images = 0
- $chosen = $false
- # loops till chosen
- while (-not $chosen)
- {
- # prompt
- $num_images = Read-Host -Prompt "how many imaged (default 52)"
- # if blank set to 52 for full deck
- if ( $num_images -eq "" )
- {
- $num_images = 52
- }
- # if its a valid number and its more than one then continue
- elseif ($num_images -match "\d" -and $num_images -ge 1) {
- $chosen = $true
- }
- else{
- Write-Host "invalid number"
- }
- }
- # setting cookies to allow fringe content (doesnt work currently not sure why)
- $cookie = [system.net.cookie]::new("fringeBenefits","yup")
- $session.Cookies.Add( "https://gelbooru.com/index.php" ,$cookie)
- $full = $true
- $match_count = 0
- $page = 0
- $last_url = ""
- while($full)
- {
- # go through multiples of 42 (42 images per page)
- $start_page = $page * 42
- $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"
- # $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"
- # gets a list of the images from the site
- $responce = Invoke-WebRequest -Uri $url_base -SessionVariable $session
- # Set-Content -value $responce -Path ".\waifus\out.html"
- Write-Host "--------------"
- # find all the matches for id and href and then do something to each of them
- ($responce | Select-String -Pattern 'id="p.\d*" href="(.*?)"' -AllMatches).Matches.Value |
- ForEach-Object {
- # finds matches again so that $matches is populated
- $_ -match 'id="p.\d*" href="(.*?)"' | Out-Null
- $match_count = ($match_count + 1)
- # fill url with image href
- $url = ($matches[1] -replace "amp;","")
- # prevents looping on last image if there arent enough images
- if ($last_url -like $url) {
- write-host "------------"
- write-host "out of images"
- write-host "------------"
- break
- }
- $last_url = $url
- $responce = Invoke-WebRequest -Uri $url -SessionVariable $session
- # find the href that maches to how gelbooru stores images
- $responce -match 'href="(https://img3.gelbooru.com/images/.*?\.(.*?))"'
- Write-Host $Matches[1]
- # dl the images and store it
- $out_path = "$path\$match_count." + $Matches[2]
- $img = Invoke-WebRequest $matches[1] -OutFile $out_path -SessionVariable $session
- # break when reached number of images
- if ($match_count -ge $num_images)
- {
- break
- }
- }
- # cycle 1 page after gone through all 42 iamges
- $page++
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement