Get a random 9GAG item in your browser using PowerShell

Brighten up your day when starting a new PowerShell session by… Getting a 9GAG item (Picture/movie clip) in your browser 🙂 I was working on parameter sets and got side-tracked into this script. Start it, and you will receive a random category 9GAG post in your browser unless it’s NSFW because you wouldn’t want that .. Right? 😉

How the script works

It’s a script that you can call with and without parameters. It will show a random category item without a parameter, but you can specify Funny, Random, or WTF to get something from that category. It connects to https://9gagrss.xyz and reads a JSON file from there, 9GAG has its JSON that you could use, but they stopped that on the 27th of February 2021. Luckily, someone started this website and lists RSS and JSON feeds on that and just a simple viewer. The script pulls 100 items, but it doesn’t seem to allow more, and picks a random number and uses that number to get a random item from the JSON file.

The total get-help Get-9GAG.ps1 output:

NAME
    C:\Data\GitHub\Powershellisfun\Random 9GAG Item In Your Browser\Get_9GAG.ps1
    
SYNOPSIS
    Gets a random picture or movieclip from 9Gag using PowerShell and launches it in your browser (Except when NSFW ;) )
    
    
SYNTAX
    C:\Data\GitHub\Powershellisfun\Random 9GAG Item In Your Browser\Get_9GAG.ps1 [<CommonParameters>]
    
    C:\Data\GitHub\Powershellisfun\Random 9GAG Item In Your Browser\Get_9GAG.ps1 [-Funny] [<CommonParameters>]

    C:\Data\GitHub\Powershellisfun\Random 9GAG Item In Your Browser\Get_9GAG.ps1 [-Meme] [<CommonParameters>]

    C:\Data\GitHub\Powershellisfun\Random 9GAG Item In Your Browser\Get_9GAG.ps1 [-WTF] [<CommonParameters>]


DESCRIPTION
    Gets a random picture or movieclip from 9Gag using PowerShell and launches it in your browser (Except when NSFW ;) )


PARAMETERS
    -Funny [<SwitchParameter>]
        Get a random Funny category item in your browser

        Required?                    false
        Position?                    named
        Default value                False
        Accept pipeline input?       false
        Accept wildcard characters?  false

    -Meme [<SwitchParameter>]
        Get a random Meme category item in your browser

        Required?                    false
        Position?                    named
        Default value                False
        Accept pipeline input?       false
        Accept wildcard characters?  false

    -WTF [<SwitchParameter>]
        Get a random WTF category item in your browser

        Required?                    false
        Position?                    named
        Default value                False
        Accept pipeline input?       false
        Accept wildcard characters?  false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer, PipelineVariable, and OutVariable. For more information, see
        about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).

INPUTS
    Defaults to Random unless Funny, Random or WTF parameter was specified


OUTPUTS
    Random 9Gag item in your browser


    -------------------------- EXAMPLE 1 --------------------------

    PS>Get_Meme.ps1

    Get a 9Gag Random category item in your browser




    -------------------------- EXAMPLE 2 --------------------------

    PS>Get_Meme.ps1 -WTF

    Get a 9Gaf WTF category item in your browser





RELATED LINKS
    https://powershellisfun.com

Adding the script to your PowerShell Profile

To run the script when starting PowerShell automatically, save it to a location (d:\temp, for example), and then you can add it to your profile like this:

Start PowerShell
notepad $profile
add d:\temp\Get_9GAG.ps1 -WTF to the end of the profile and save/quit
Start a new PowerShell session

(You can also run it whenever you want without adding it to your profile, of course )

How does it look?

If the script doesn’t find an NSFW item, then it will look like this in PowerShell:

And in your browser:

But when it finds an NSFW item, it will just tell you that it did and not launch it:

The script

<#
.SYNOPSIS
  
Gets a random picture or movieclip from 9Gag using PowerShell and launches it in your browser (Except when NSFW ;) )
  
.DESCRIPTION
  
Gets a random picture or movieclip from 9Gag using PowerShell and launches it in your browser (Except when NSFW ;) )
  
.PARAMETER Funny
Get a random Funny category item in your browser
  
.PARAMETER Meme
Get a random Meme category item in your browser
 
.PARAMETER Random
Get a random Random category item in your browser
 
.PARAMETER WTF
Get a random WTF category item in your browser
 
.INPUTS
  
Defaults to Random unless Funny, Random or WTF parameter was specified
  
.OUTPUTS
  
Random 9Gag item in your browser
  
.EXAMPLE
  
PS> Get_Meme.ps1
Get a 9Gag Random category item in your browser
  
.EXAMPLE
  
PS> Get_Meme.ps1 -WTF
Get a 9Gaf WTF category item in your browser
  
.LINK
  
https://powershellisfun.com
  
#>
  
#Parameters
[CmdletBinding(DefaultParameterSetName = "Random")]
param (
    [Parameter(Mandatory = $False, HelpMessage = "Get a 9GAG random Funny category item in your browser", ParameterSetName = "Funny")][Switch]$Funny,
    [Parameter(Mandatory = $false, HelpMessage = "Get a random 9GAG Meme category item in your browser", ParameterSetName = "Meme")][Switch]$Meme,
    [Parameter(Mandatory = $false, HelpMessage = "Get a 9GAG random WTF category item in your browser", ParameterSetName = "WTF")][Switch]$WTF
)
$selection = $PSCmdlet.ParameterSetName
$ProgressPreference = "SilentlyContinue"
$contents = Invoke-WebRequest -Uri "https://9gagrss.xyz/json.php?channel=$($selection)&limit=100"
$number = Get-Random -Minimum 1 -Maximum 101
$9gag = ($contents | convertfrom-json).data.posts[$number]
 
if ($9gag.nsfw -eq '1' ) {
    Write-Host ("Get_9GAG found a NSFW Picture/Movieclip, skipping because NSFW ;)") -ForegroundColor Yellow
    exit
}
 
write-host ("Get_9GAG found '{0}', launching now..." -f $9gag.title) -ForegroundColor Green
 
try {
    Start-Process $9gag.content_url
}
catch {
    Write-Warning ("Error loading, please try again...")
}

Not responsible

And… 9GAG can have strange/weird/anti-social pictures and movie clips, and I’m not responsible for the item being picked 😀

Download the script(s) from GitHub here

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.