Do you need to automate a task to upload images to a free host with gallery functionality? Then this script could be your solution.
This script uses following tools:
- cURL, http://curl.haxx.se
- HtmlAgilityPack, http://htmlagilitypack.codeplex.com
- PowerShell 2.0, http://support.microsoft.com/kb/968929
- ImageBam API, http://code.google.com/p/imagebam-api/ (To access API, register here: http://www.imagebam.com/sys/API/clients)
This script takes care of OAuth authentication and authorization (the dirty way, without user consent, lol)
- $basedrive ="C:"
- $basedir = "imagebam/"
- $basepath = ($basedrive + "/" + $basedir)
- Add-Type -Assembly System.ServiceModel.Web,System.Runtime.Serialization
- Add-Type -AssemblyName System.Web
- add-type -Path ($basepath+"\HtmlAgilityPack.dll")
- $responsefile = "response.txt"
- $headerfile = "headers.txt"
- $ib_nick = "MyImageBamNick"
- $ib_pw = "MyImageBamPW"
- $api_key = "FILL_IN"
- $api_secret = "FILL_IN"
- $oauth_token = "FILL_IN_AFTER_AUHTORIZED"
- $oauth_token_secret = "FILL_IN_AFTER_AUHTORIZED"
- $oauth_nonce = 0
- $oauth_timestamp = 0
- $oauth_signature_method = "MD5";
- $random = New-Object -type Random
- $htmldoc = New-Object HtmlAgilityPack.HtmlDocument
- & $basedrive
- cd\
- cd $basepath
- cls
- function MD5($inputstring)
- {
- #Borrowed this snippet from Daniel Damen http://www.out-web.net/?p=334
- $result = ""
- $cryptoServiceProvider = [System.Security.Cryptography.MD5CryptoServiceProvider];
- $hashAlgorithm = new-object $cryptoServiceProvider
- $hashByteArray = $hashAlgorithm.ComputeHash([Char[]]$inputString);
- foreach ($byte in $hashByteArray) { $result += “{0:X2}” -f $byte}
- return $result.ToLower()
- }
- function GetUnixTimeStamp()
- {
- #Borrowed this piece of code from: http://dev.healthx.com/Home/powershell-NET
- $epoch = New-Object -type DateTime(1970, 1, 1)
- $unixts = [int] ([System.DateTime]::Now.ToUniversalTime() - $epoch).TotalSeconds
- return $unixts.ToString()
- }
- function Login()
- {
- $login = "action=true&nick=" + $ib_nick + "&pw=" + $ib_pw
- & curl -L -c cookie.txt -D loginheaders.txt -o login.html -d $login http://www.imagebam.com/login
- }
- function Authenticate()
- {
- $url = "http://www.imagebam.com/sys/oauth/request_token"
- $dataresponse = ""
- $data = ""
- $oauth_timestamp = GetUnixTimeStamp
- $oauth_nonce = $random.Next().ToString()
- #build signature string
- $oauth_signature_string = $api_key + $api_secret + $oauth_timestamp + $oauth_nonce
- #write-host "oauth_signature_string: " -nonewline
- #write-host $oauth_signature_string -foregroundcolor Blue
- #compute md5 hash of signature string
- $oauth_signature = MD5($oauth_signature_string)
- #write-host "oauth_signature: " -nonewline
- #write-host $oauth_signature -foregroundcolor Blue
- #build http POST data string
- $data = "oauth_consumer_key=" + $api_key
- $data += "&oauth_signature_method=" + $oauth_signature_method
- $data += "&oauth_signature=" + $oauth_signature
- $data += "&oauth_timestamp=" + $oauth_timestamp
- $data += "&oauth_nonce=" + $oauth_nonce
- #write-host "auth_params: " -nonewline
- #write-host $data -foregroundcolor Blue
- #delete exisiting response files
- if( Test-path ($basepath + $responsefile) ) { del $responsefile }
- if( Test-path ($basepath + $headerfile) ) { del $headerfile }
- #send request
- & curl -s -d $data -D $headerfile -o $responsefile $url
- #read response data
- $dataresponse = (Get-Content $responsefile)
- $httpresponse = (Get-Content $headerfile)[0].Replace("HTTP/1.1 ","").Substring(0,3)
- #write-host $httpresponse
- #write-host "oauth response: " -nonewline
- #write-host $response -foregroundcolor Blue
- #parse response data: variable1=value1&variable2=value2
- $authdata = [regex]::split($dataresponse, "&")
- $ot = [regex]::split($authdata[0], "=")[1]
- $ots = [regex]::split($authdata[1], "=")[1]
- Set-Variable -Name oauth_token -Value $ot -Scope global
- Set-Variable -Name oauth_token_secret -Value $ots -Scope global
- write-host "oauth_token: " -nonewline
- write-host $oauth_token -foregroundcolor Blue
- #write-host "oauth_token_secret: " -nonewline
- #write-host $oauth_token_secret -foregroundcolor Blue
- }
- function Authorize()
- {
- $url = "http://www.imagebam.com/sys/oauth/authorize_token"
- $urla = ($url + "?oauth_token=" + $oauth_token)
- #login to get cookies
- Login
- #Give this script access to your ImageBam account
- & curl -s -b cookie.txt -c cookie.txt -o "authorize.html" $urla
- $htmldoc.Load(($basepath + "authorize.html"))
- $oa = $htmldoc.DocumentNode.SelectSingleNode("//input[@name='oauth_token']").Attributes["value"].Value
- $oas = $htmldoc.DocumentNode.SelectSingleNode("//input[@name='token_auth_sec']").Attributes["value"].Value
- $data = "submit=yes&authorize=true&oauth_token=" + $oa + "&token_auth_sec=" + $oas
- & curl -s -b cookie.txt -c cookie.txt -D $headerfile -d $data -o "authorizepin.html" $url
- $htmldoc.Load(($basepath + "authorizepin.html"))
- $oauth_verifier = $htmldoc.DocumentNode.SelectSingleNode("//div[@style='font-size:22px; margin-top:20px;']").InnerHtml.Trim()
- write-host "shared secret: " $oauth_verifier
- $oauth_timestamp = GetUnixTimeStamp
- $oauth_nonce = $random.Next().ToString()
- $oauth_signature_string = $api_key + $api_secret + $oauth_timestamp + $oauth_nonce + $oauth_token + $oauth_token_secret
- #write-host "oauth_signature_string: " -nonewline
- #write-host $oauth_signature_string -foregroundcolor Blue
- #compute md5 hash of signature string
- $oauth_signature = MD5($oauth_signature_string)
- #write-host "oauth_signature: " -nonewline
- #write-host $oauth_signature -foregroundcolor Blue
- #build http POST data string
- $data = " oauth_consumer_key=" + $api_key
- $data += "&oauth_token=" + $oauth_token
- $data += "&oauth_signature_method=" + $oauth_signature_method
- $data += "&oauth_signature=" + $oauth_signature
- $data += "&oauth_timestamp=" + $oauth_timestamp
- $data += "&oauth_nonce=" + $oauth_nonce
- $data += "&oauth_verifier=" + $oauth_verifier
- $url = "http://www.imagebam.com/sys/oauth/request_access_token"
- #send request
- & curl -s -b cookie.txt -c cookie.txt -d $data -D $headerfile -o $responsefile $url
- #read response data
- $dataresponse = (Get-Content $responsefile)
- $httpresponse = (Get-Content $headerfile)[0].Replace("HTTP/1.1 ","").Substring(0,3)
- #write-host $httpresponse
- #write-host "oauth response: " -nonewline
- #write-host $response -foregroundcolor Blue
- #parse response data: variable1=value1&variable2=value2
- $authdata = [regex]::split($dataresponse, "&")
- $ot = [regex]::split($authdata[0], "=")[1]
- $ots = [regex]::split($authdata[1], "=")[1]
- Set-Variable -Name oauth_token -Value $ot -Scope global
- Set-Variable -Name oauth_token_secret -Value $ots -Scope global
- write-host "new oauth_token: " -nonewline
- write-host $oauth_token -foregroundcolor Blue
- write-host "oauth_token_secret: " -nonewline
- write-host $oauth_token_secret -foregroundcolor Blue
- }
- function UploadImage([string]$imagepath, [string]$contenttype, [string]$galleryid)
- {
- $url = "http://www.imagebam.com/sys/API/resource/upload_image"
- $dataresponse = ""
- $data = ""
- $oauth_timestamp = GetUnixTimeStamp
- $oauth_nonce = $random.Next().ToString()
- #build signature string
- $oauth_signature_string = $api_key + $api_secret + $oauth_timestamp + $oauth_nonce + $oauth_token + $oauth_token_secret
- #write-host "oauth_signature_string: " -nonewline
- #write-host $oauth_signature_string -foregroundcolor Blue
- #compute md5 hash of signature string
- $oauth_signature = MD5($oauth_signature_string)
- #write-host "oauth_signature: " -nonewline
- #write-host $oauth_signature -foregroundcolor Blue
- #build http POST data string
- #authentication params
- $d1 = "oauth_consumer_key=" + $api_key
- $d2 = "oauth_signature_method=" + $oauth_signature_method
- $d3 = "oauth_signature=" + $oauth_signature
- $d4 = "oauth_timestamp=" + $oauth_timestamp
- $d5 = "oauth_nonce=" + $oauth_nonce
- $d6 = "oauth_token=" + $oauth_token
- #image params
- $ct = ""
- if($imagepath.ToLower().EndsWith("jpg")){ $ct = ";type=image/jpeg"}
- elseif($imagepath.ToLower().EndsWith("png")) { $ct = ";type=image/png" }
- elseif($imagepath.ToLower().EndsWith("gif")) { $ct = ";type=image/gif" }
- if ($contenttype -eq "") { $contenttype = "family"}
- $d7 = "content_type=" + $contenttype
- $d8 = "gallery_id=" + $galleryid
- $d9 = "image=@" + $imagepath + $ct
- #delete exisiting response files
- if( Test-path ($basepath + $responsefile) ) { del $responsefile }
- if( Test-path ($basepath + $headerfile) ) { del $headerfile }
- if( Test-path ($basepath + "trace.log") ) { del "trace.log" }
- #send multipart form post request, disable Expect 100 continue header
- & curl -s -H "Expect:" -F $d1 -F $d2 -F $d3 -F $d4 -F $d5 -F $d6 -F $d7 -F $d8 -F $d9 -D $headerfile --trace-ascii trace.log -o $responsefile $url
- #read response data
- $dataresponse = (Get-Content $responsefile) -join "`n"
- $httpresponse = (Get-Content $headerfile)[0].Replace("HTTP/1.1 ","").Substring(0,3)
- if ($httpresponse -ne "200")
- {
- write-host "Error: " -nonewline -foregroundcolor Black
- write-host $dataresponse -foregroundcolor Red
- $dataresponse = ""
- }
- #$jsondata = (Get-Content ($basepath + $responsefile)) -join "`n"
- return Convert-JsonToXml $dataresponse
- #return $dataresponse
- }
- function CreateGallery([string]$title, [string]$description)
- {
- $url = "http://www.imagebam.com/sys/API/resource/create_gallery"
- $dataresponse = ""
- $data = ""
- $oauth_timestamp = GetUnixTimeStamp
- $oauth_nonce = $random.Next().ToString()
- #build signature string
- $oauth_signature_string = $api_key + $api_secret + $oauth_timestamp + $oauth_nonce + $oauth_token + $oauth_token_secret
- #write-host "oauth_signature_string: " -nonewline
- #write-host $oauth_signature_string -foregroundcolor Blue
- #compute md5 hash of signature string
- $oauth_signature = MD5($oauth_signature_string)
- #write-host "oauth_signature: " -nonewline
- #write-host $oauth_signature -foregroundcolor Blue
- #build http POST data string
- #authentication params
- $data = "oauth_consumer_key=" + $api_key
- $data += "&oauth_signature_method=" + $oauth_signature_method
- $data += "&oauth_signature=" + $oauth_signature
- $data += "&oauth_timestamp=" + $oauth_timestamp
- $data += "&oauth_nonce=" + $oauth_nonce
- $data += "&oauth_token=" + $oauth_token
- #gallery params
- $data += "&title=" + [System.Uri]::EscapeDataString($title)
- $data += "&description=" + [System.Uri]::EscapeDataString($description)
- #delete exisiting response files
- if( Test-path ($basepath + $responsefile) ) { del $responsefile }
- if( Test-path ($basepath + $headerfile) ) { del $headerfile }
- if( Test-path ($basepath + "trace.log") ) { del "trace.log" }
- #send request, disable Expect 100 continue header
- & curl -s -H "Expect:" -d $data -D $headerfile --trace-ascii trace.log -o $responsefile $url
- #read response data
- $dataresponse = (Get-Content $responsefile) -join "`n"
- $httpresponse = (Get-Content $headerfile)[0].Replace("HTTP/1.1 ","").Substring(0,3)
- if ($httpresponse -ne "200")
- {
- write-host "Error: " -nonewline -foregroundcolor Black
- write-host $dataresponse -foregroundcolor Red
- $dataresponse = ""
- }
- #$jsondata = (Get-Content ($basepath + $responsefile)) -join "`n"
- return Convert-JsonToXml $dataresponse
- #return $dataresponse
- }
- function Convert-JsonToXml([string]$json)
- {
- #Thanks to Joel Bennett: http://huddledmasses.org/json-from-powershell-but-why/
- $bytes = [byte[]][char[]]$json
- $quotas = [System.Xml.XmlDictionaryReaderQuotas]::Max
- $jsonReader = [System.Runtime.Serialization.Json.JsonReaderWriterFactory]::CreateJsonReader($bytes,$quotas)
- try
- {
- $xml = new-object System.Xml.XmlDocument
- $xml.Load($jsonReader)
- $xml
- }
- finally
- {
- $jsonReader.Close()
- }
- }
- #These are only needed first time, but you may have to re-authenticate if the authentication tokens exprire
- #Authenticate
- #Authorize
- #Create gallery ID, optional
- $gallryxml = CreateGallery "My First Gallery"
- $galleryid = $gallryxml.SelectSingleNode("//GID").InnerXml
- write-host "Gallery ID:" $galleryid
- $contenttype = "family" #family/adult
- $files = dir C:\mypics | Select-Object FullName, Directory, Name, Length | Sort-Object Name
- foreach($file in $files)
- {
- write-host "Uploading " $file.FullName
- $xml = UploadImage $file.FullName $contenttype $galleryid
- $imagelink = $xml.SelectSingleNode("//URL").InnerXml
- $xml = $null
- write-host "Link: " $imagelink
- #[diagnostics.process]::start($imagelink)
- }