#
Find-Gif Powershell Module
#
Just an idea 👾
So yesterday was Sunday, some people think Sunday is like the most boring day of the week, or I certainly did when I was younger. Nothing ever interesting seemed to happen on a Sunday.
You like the subtle demos I am chucking in
😁
Fast-Forward a number of years and Sunday is probably my favourite day, as I do like to just chill on a Sunday and try to relax (with 4 daughters). As it turned out this Sunday was pretty busy, with only 4 hours sleep 🥱 I hit the car boot in the morning with my family to see what bargins we could 🔍 find. Came home with some 🧸 toys for my girls. Had a mate come over to catch-up on in the week, and for some good cup of ☕ Yorkshire Tea. Then I mowed the lawn, weeded the front and back garden :male_farmer:, 🧽 washed my car 🚘, checked all my car levels 🛢️, went to the garage to check tyre pressure and give it a good hoover. Came back home took the kids and dogs 🐕🦺 out for a good hour 🚶 walk. Helped the wife with preparing dinner, then thought I need to write the blog for the ChartImage module. It was whilst writing this blog I got thinking abut the cool GIF images you can insert on tweets, and well I thought it would be something else cool to add to the website. So I went on the Powershell gallery, but to my suprise I could not find one. So even though I might have been feeling like a 🧟 I still wanted to 🥳
Okay for me this was a calling to find an API which would allow me to make a function that I could turn into a module and publish to the Powershell gallery. One quick duckduckgo search later I just selected the top website which was tenor.com it seemed to contain all the information I needed to get coding.
I saw that in the example they provided, you just needed to parameters, one parameter would be the search term and the second parameter would allow the number of results to be returned for the search term specified. Armed with this information I just opened ISE (because it loads quicker than VS Code on my crappy x86 laptop) and got writing the function. As I have learnt the art of multi-tasking having 4 daughters, I carried on writing the blog and developing the function.
Now I know how APIs work and how to use them within Powershell. However the URL provided in the example just seemed to return me the GIF image hosted on he actual tenor.com site. This got me thinking that if I was to use that URL it would most likely just get redirected to that whole website page. All I was really interested in was the actual raw GIF file and where that was hosted, so I could embed that image on my website. One of the many great things about Powershell is it fjust makes sense. Like the way it is written and the cmdlets used, it's almost like you are talking (well to me anyway) secondly is the ability to pipe things along the command-line to join commands together. This allowed me to convert the JSON data returned and obtain the raw GIF image I was after.
#
Function Code 💾
<#
.Synopsis
Finds a number of urls containing the gif you search for
.DESCRIPTION
This uses tenor.com API to find the gif you are looking for, you have two parameters, one to search for the gif something like a keyword "excited" and the number of results to bring back whic will be a number something like 9
.EXAMPLE
Find-Gif -SearchPhrase "Happy" -NumberOfResults 2
Will return 2 URL links to the raw GIF file hosted for this search phrase
.EXAMPLE
Find-Gif "Excited"
Will return the default of 8 results for the search phrase specified
#>
function Find-Gif
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0,
HelpMessage="Just type a keyword for the type of gif you are looking for such as one of the following funny,excited,scared")]
$SearchPhrase,
[Parameter(HelpMessage="Use an integer value to bring back the number of URLS you want returned, each URL is a GIF of what you searched for")]
[ValidatePattern("[0-9]")]
[int]
$NumberOfResults = 8
)
Begin
{
Write-Host -ForegroundColor Green "Finding Gifs related to $SearchPhrase please wait for results."
}
Process
{
try
{
((Invoke-WebRequest -Uri "https://g.tenor.com/v1/search?q=$SearchPhrase&key=LIVDSRZULELA&limit=$NumberOfResults" -ErrorAction Stop | Select-Object content).content | ConvertFrom-Json).Results | select -ExpandProperty url -OutVariable urls
foreach ($url in $urls)
{
Write-Host -ForegroundColor Green "Actual link to just the Gif file"
(Invoke-WebRequest "$url" | select images).Images | Where-Object {$_.src -match "c.tenor.com"} | Select -ExpandProperty src
}
}
catch
{
$bad = $_
Write-Warning "Whoops something went wrong $bad"
}
}
End
{
Write-Host -ForegroundColor Green "Enjoy your Gifs"
}
}
#
How did you turn it into a Module? 😵
If you been doing Powershell for a little while you have probably got yourself into functions, and how cool they. But how do you turn or make a Powershell function into a module? I got to say it took me a long, long time to approach this cross-road. Mainly due to the Powershell I was writing was pretty unique to the given problem I was solving. Secondly it just seemed daunting, to be creating a module, like how did it all work, was I going to have to buy another book to learn how to make modules? Well no, the short and simple is for this particular module I created and empty folder called Find-Gif I then did a few lines of code, turned that into a function, but instead of saving as a .PS1 file I saved it as a Find-Gif.PSM1 then in the powershell console I navigated to the Find-Gif folder (as you can dot source items into the main module psd1 file), then used the New-ModuleManifest cmdlet to then create the module
New-ModuleManifest -Path .\Find-Gif.psd1 -RootModule .\Find-Gif.psm1 -Author "Adam Bacon" -Description "Find Gifs on the web" -Guid $(new-guid)
Boom your done, you got a module. You just now need to copy that Find-Gif folder (or rather your module named folder) into one of the module paths
easy way to find you Powershell module paths
($env:PSModulePath) -split ";"
Thanks for reading this latest blog post, I know this particular module maybe won't be very useful for well probably the majority of the Powershell community but I thought it would be cool to use Gifs on my static website to bring some motion to the static-ness. Hopefully this might even encourage you to delve into APIs 💡 as now you seen how useful they can be 😄