#---------------------------------------------------------------------------- # The contents of this file are subject to the "END USER LICENSE AGREEMENT # FOR F5 Software Development Kit for iControl"; you may not use this file # except in compliance with the License. The License is included in the # iControl Software Development Kit. # # Software distributed under the License is distributed on an "AS IS" # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See # the License for the specific language governing rights and limitations # under the License. # # The Original Code is iControl Code and related documentation # distributed by F5. # # The Initial Developer of the Original Code is F5 Networks, # Inc. Seattle, WA, USA. Portions created by F5 are Copyright (C) 1996-2009 # F5 Networks, Inc. All Rights Reserved. iControl (TM) is a registered # trademark of F5 Networks, Inc. # # Alternatively, the contents of this file may be used under the terms # of the GNU General Public License (the "GPL"), in which case the # provisions of GPL are applicable instead of those above. If you wish # to allow use of your version of this file only under the terms of the # GPL and not to allow others to use your version of this file under the # License, indicate your decision by deleting the provisions above and # replace them with the notice and other provisions required by the GPL. # If you do not delete the provisions above, a recipient may use your # version of this file under either the License or the GPL. #---------------------------------------------------------------------------- [System.Reflection.Assembly]::LoadWithPartialName("System.Web") | out-null; ############################################################################# # # FOURSQUARE API # ############################################################################# #============================================================================ # Credentials #============================================================================ #---------------------------------------------------------------------------- function Set-FourSquare.Credentials() # # Set the credentials for your FourSquare.com account. # # Parameters # # user - The Foursquare.com username. # pass - The Foursquare.com password. #---------------------------------------------------------------------------- { param([string]$user = $null, [string]$pass = $null); if ( $user -and $pass ) { $script:g_FourSquarecreds = New-Object System.Net.NetworkCredential -argumentList ($user, $pass); } else { $creds = Get-FourSquare.Credentials; } } #---------------------------------------------------------------------------- function Get-FourSquare.Credentials() # # Get the credentials for your FourSquare.com account. # # Parameters # # none #---------------------------------------------------------------------------- { if ( $null -eq $g_FourSquarecreds ) { trap { Write-Error "ERROR: You must enter your FourSquare credentials for Posh-FourSquare to work!"; continue; } $c = Get-Credential if ( $c ) { $user = $c.GetNetworkCredential().Username; $pass = $c.GetNetworkCredential().Password; $script:g_FourSquarecreds = New-Object System.Net.NetworkCredential -argumentList ($user, $pass); } } $script:g_FourSquarecreds; } #============================================================================ # GEO METHODS #============================================================================ #---------------------------------------------------------------------------- function Get-FourSquare.Cities() # # Returns a list of currently active cities # # Parameters # # raw - if raw is supplied, the raw HTTP response is displayed, otherwise # it is cast to an xml document and the appropriate subelement is returned. #---------------------------------------------------------------------------- { param([switch]$raw = $false); $request = "$(Get-FourSquareAPIBase)/cities"; $results = Execute-HTTPGetCommand -url $request; if ( $raw ) { $results; } else { [xml]$x = $results; $x.cities.city; } } #---------------------------------------------------------------------------- function Check-FourSquare.City() # # When given a lat/long, returns the closest foursquare city. # # Parameters # # geolat - latitiude # geolong - longitude # raw - if raw is supplied, the raw HTTP response is displayed, # otherwise it is cast to an xml document and the appropriate # subelement is returned. #---------------------------------------------------------------------------- { param( [string]$geolat = $(Throw-ParameterRequired -name "geolat"), [string]$geolong = $(Throw-ParameterRequired -name "geolong"), [switch]$raw = $false ); $request = "$(Get-FourSquareAPIBase)/checkcity"; $request += "?geolat=$geolat&geolong=$geolong"; $results = Execute-HTTPGetCommand -url $request; if ( $raw ) { $results; } else { [xml]$x = $results; $x.city; } } #---------------------------------------------------------------------------- function Switch-FourSquare.City() # # When given a valid foursquare cityid, changes the user's default city. # # Parameters: # # cityid - a foursquare cityid to which you want to switch. # raw - if raw is supplied, the raw HTTP response is displayed, # otherwise it is cast to an xml document and the appropriate # subelement is returned. #---------------------------------------------------------------------------- { param( [string]$cityid = $(Throw-ParameterRequired -name "cityid"), [switch]$raw = $false ); $request = "$(Get-FourSquareAPIBase)/switchcity"; $args += "cityid=$cityid"; $results = Execute-HTTPPostCommand -url $request -data $args; if ( $raw ) { $results; } else { [xml]$x = $results; $x; } } #============================================================================ # CHECKIN METHODS #============================================================================ #---------------------------------------------------------------------------- function Get-FourSquare.Checkins() # # Returns a list of recent checkins from friends. # Some notes on how to parse each block: # # * if exists, it's a check-in to a proper place. # * if and exist, it's a check-in to a proper place with a shout. # * if only exists, it's a shout (no check-in). shouts are like callouts # or tweets to your network. they need not be tied down to a particular place. # it's useful for sending messages like: "hey who's up for hanging out later tonight?". # * if no or exists, then it's a silent check-in # ("off-the-grid" as we like to say). this shows up in the timeline so that # you know the person is out and about (to make it easy to meet up after # they are done with whatever they are doing. it's useful for stuff like dates, # business meetings, etc). # # Parameters: # # cityid - a foursquare cityid to which you want to switch. # raw - if raw is supplied, the raw HTTP response is displayed, # otherwise it is cast to an xml document and the appropriate # subelement is returned. #---------------------------------------------------------------------------- { param( [string]$cityid = "", [switch]$raw = $false ); $request = "$(Get-FourSquareAPIBase)/checkins"; if ( $cityid.Length -gt 0 ) { $request += "?cityid=$cityid"; } $results = Execute-HTTPGetCommand -url $request; if ( $raw ) { $results; } else { [xml]$x = $results; $x.checkins.checkin; } } #---------------------------------------------------------------------------- function New-FourSquare.Checkin() # # Allows you to check-in to a place # # Parameters # # vid - (optional, not necessary if you are 'shouting' or have a venue name). # ID of the venue where you want to check-in. # venue - (optional, not necessary if you are 'shouting' or have a vid) # if you don't have a venue ID, pass the venue name as a string # using this parameter. foursquare will attempt to match it on the server-side # shout - (optional) a message about your check-in # private - (optional, defaults to the user's setting). "1" means "don't # show your friends". "0" means "show everyone" # twitter - (optional, default to the user's setting). "1" means # "send to twitter". "0" means "don't send to twitter" # geolat - (optional, but recommended) # geolong - (optional, but recommended) # raw - if raw is supplied, the raw HTTP response is displayed, # otherwise it is cast to an xml document and the appropriate # subelement is returned. #---------------------------------------------------------------------------- { param( [string]$vid = "", [string]$venue = "", [string]$shout = "", [bool]$private = $false, [bool]$twitter = $false, [string]$geolat = "", [string]$geolong = "", [switch]$raw = $false ); if ( ($vid.Length -eq 0) -and ($venue.Length -eq 0) ) { Throw-ParameterRequired "vid or venue"; } $request = "$(Get-FourSquareAPIBase)/checkin"; $request += "?private=$(Get-TrueFalseString $private)&twitter=$(Get-TrueFalseString $twitter)"; if ( $vid.Length -gt 0 ) { $request += "&vid=$vid"; } if ( $venue.Length -gt 0 ) { $request += "&venue=$([System.Web.HttpUtility]::UrlEncode($venue))"; } if ( $shout.Length -gt 0 ) { $request += "&shout=$([System.Web.HttpUtility]::UrlEncode($shout))"; } if ( $geolat.Length -gt 0 ) { $request += "&geolat=$geolat"; } if ( $geolong.Length -gt 0 ) { $request += "&geolong=$geolong"; } $results = Execute-HTTPGetCommand -url $request; if ( $raw ) { $results; } else { [xml]$x = $results; $x.checkin; } } #---------------------------------------------------------------------------- function Get-FourSquare.History() # # Returns a history of checkins for the authenticated user (across all cities). # # Some notes on how to parse each block: # # * if exists, it's a check-in to a proper place. # * if and exist, it's a check-in to a proper place with a shout. # * if only exists, it's a shout (no check-in). # # Parameters # # limit - (optional, default: 20). number of checkins to return # raw - if raw is supplied, the raw HTTP response is displayed, # otherwise it is cast to an xml document and the appropriate # subelement is returned. #---------------------------------------------------------------------------- { param( [int]$limit = 20, [switch]$raw = $false ); $request = "$(Get-FourSquareAPIBase)/history"; if ( $limit -gt 0 ) { $request += "?limit=$limit"; } $results = Execute-HTTPGetCommand -url $request; if ( $raw ) { $results; } else { [xml]$x = $results; $x.checkins.checkin; } } #============================================================================ # USER METHODS #============================================================================ #---------------------------------------------------------------------------- function Get-FourSquare.User() # # Returns profile information (badges, etc) for a given user. If the user has # recent check-in data, this data will be returned as well. If the user # requested is 'self' (ie, the authenticating user), a block with # defaults will be returned. This settings block includes on/off values like # "send-to-twitter?" and the user's RSS/KML private feeds key. # # Parameters # # uid - userid for the user whose information you want to retrieve. # if you do not specify a 'uid', the authenticated user's # profile data will be returned. # badges - (optional, default: false) set to true ("1") to also show # badges for this user # mayor - (optional, default: false) set to true ("1") to also show # venues for which this user is a mayor # raw - if raw is supplied, the raw HTTP response is displayed, # otherwise it is cast to an xml document and the appropriate # subelement is returned. #---------------------------------------------------------------------------- { param( [int]$uid = 0, [switch]$badges = $false, [switch]$mayor = $false, [switch]$raw = $false ); $request = "$(Get-FourSquareAPIBase)/user"; $request += "?badges=$(Get-TrueFalseString $badges)&mayor=$(Get-TrueFalseString $mayor)"; if ( $uid -ne 0 ) { $request += "&uid=$uid"; } $results = Execute-HTTPGetCommand -url $request; if ( $raw ) { $results; } else { [xml]$x = $results; $x.user; } } #---------------------------------------------------------------------------- function Get-FourSquare.Friends() # # Returns a list of the authenticated user's friends. # # Parameters # # raw - if raw is supplied, the raw HTTP response is displayed, # otherwise it is cast to an xml document and the appropriate # subelement is returned. #---------------------------------------------------------------------------- { param( [switch]$raw = $false ); $request = "$(Get-FourSquareAPIBase)/friends"; $results = Execute-HTTPGetCommand -url $request; if ( $raw ) { $results; } else { [xml]$x = $results; $x.friends.friend; } } #============================================================================ # VENUE METHODS #============================================================================ #---------------------------------------------------------------------------- function Get-FourSquare.Venues() # # Returns a list of venues near the area specified or that match the search # term. (The distance returned is in miles). If you authenticate, the method # will return venue meta-data related to you and your friends. If you do not # authenticate, you will not get this data. # # Parameters # # geolat - latitude (required) # geolong - longitude (required) # r - radius in miles (optional) # l - limit of results (optional, default 10) # q - keyword search (optional) # raw - if raw is supplied, the raw HTTP response is displayed, # otherwise it is cast to an xml document and the appropriate # subelement is returned. #---------------------------------------------------------------------------- { param( [string]$geolat = $(Throw-ParameterRequired -name "geolat"), [string]$geolong = $(Throw-ParameterRequired -name "geolong"), [int]$r = -1, [int]$l = 10, [int]$q = "", [switch]$raw = $false ); $request = "$(Get-FourSquareAPIBase)/venues"; $request += "?geolat=$geolat&geolong=$geolong&l=$l"; if ( $r -ne -1 ) { $request += "&r=$r"; } if ( $q.Length -gt 0 ) { $request += "&q=$([System.Web.HttpUtility]::UrlEncode($q))"; } $results = Execute-HTTPGetCommand -url $request; if ( $raw ) { $results; } else { [xml]$x = $results; #$x; foreach ($g in $x.venues.group) { "--------------------------------"; $g.type; "--------------------------------"; foreach ($v in $g.venue) { $v; $v.stats; } } } } #---------------------------------------------------------------------------- function Get-FourSquare.Venue() # # Returns venue data, including mayorship, tips/to-dos and tags. # # Parmeters # # vid - the ID for the venue for which you want information. # raw - if raw is supplied, the raw HTTP response is displayed, # otherwise it is cast to an xml document and the appropriate # subelement is returned. #---------------------------------------------------------------------------- { param( [int]$vid = $(Throw-ParameterRequired -name "vid"), [switch]$raw = $false ); $request = "$(Get-FourSquareAPIBase)/venue"; $request += "?vid=$vid"; $results = Execute-HTTPGetCommand -url $request; if ( $raw ) { $results; } else { [xml]$x = $results; $x.venue; $x.venue.stats; } } #---------------------------------------------------------------------------- function Add-FourSquare.Venue() # # Allows you to add a new venue. # # Parameters # # name - the name of the venue # address - the address of the venue (e.g., "202 1st Avenue") # crossstreet - the cross streets (e.g., "btw Grand & Broome") # city - the city name where this venue is # state - the state where the city is # zip - (optional) the ZIP code for the venue # cityid - (required) the foursquare cityid where the venue is # phone - (optional) the phone number for the venue # raw - if raw is supplied, the raw HTTP response is displayed, # otherwise it is cast to an xml document and the appropriate # subelement is returned. #---------------------------------------------------------------------------- { param( [string]$name = $(Throw-ParameterRequired -name "name"), [string]$address = $(Throw-ParameterRequired -name "address"), [string]$crossstreet = "", [string]$city = $(Throw-ParameterRequired -name "city"), [string]$state = $(Throw-ParameterRequired -name "state"), [int]$zip = 0, [string]$cityid = $(Throw-ParameterRequired -name "cityid"), [string]$phone = "", [switch]$raw = $false ); $request = "$(Get-FourSquareAPIBase)/addvenue"; $request += "?name=$([System.Web.HttpUtility]::UrlEncode($name))"; $request += "&address=$([System.Web.HttpUtility]::UrlEncode($address))"; $request += "&city=$([System.Web.HttpUtility]::UrlEncode($city))"; $request += "&state=$state"; $request += "&cityid=$cityid"; if ( $crossstreet.length -gt 0 ) { $request += "&crossstreet=$([System.Web.HttpUtility]::UrlEncode($crossstreet))"; } if ( $zip -gt 0 ) { $request += "&zip=$zip"; } if ( $phone.Length -gt 0 ) { $request += "&phone=$phone"; } $results = Execute-HTTPGetCommand -url $request; if ( $raw ) { $results; } else { [xml]$x = $results; $x.venue; $x.venue.stats; $x.venue.tips; } } #============================================================================ # TIP METHODS #============================================================================ #---------------------------------------------------------------------------- function Get-FourSquare.Tips() # # Returns a list of tips near the area specified. (The distance returned is in miles). # # Parameters # # geolat - latitude (optional) # geolong - longitude (optional) # l - limit of results (optional, default 30) # raw - if raw is supplied, the raw HTTP response is displayed, # otherwise it is cast to an xml document and the appropriate # subelement is returned. #---------------------------------------------------------------------------- { param( [string]$geolat = $(Throw-ParameterRequired -name "geolat"), [string]$geolong = $(Throw-ParameterRequired -name "geolong"), [int]$l = 30, [switch]$raw = $false ); $request = "$(Get-FourSquareAPIBase)/tips"; $request += "?geolat=$geolat&geolong=$geolong&l=$l"; if ( $r -ne -1 ) { $request += "&r=$r"; } $results = Execute-HTTPGetCommand -url $request; if ( $raw ) { $results; } else { [xml]$x = $results; foreach ($g in $x.tips.group) { $g.type; $g.tip; } } } #---------------------------------------------------------------------------- function Add-FourSquare.Tip() # # Allows you to add a new tip or to-do at a venue. # # Parameters # # vid - the venue where you want to add this tip (required) # text - the text of the tip or to-do item (required) # type - specify one of 'tip' or 'todo' (optional, default: tip) # raw - if raw is supplied, the raw HTTP response is displayed, # otherwise it is cast to an xml document and the appropriate # subelement is returned. #---------------------------------------------------------------------------- { param( [string]$vid = $(Throw-ParameterRequired -name "vid"), [string]$text = $(Throw-ParameterRequired -name "text"), [string]$type = "", [switch]$raw = $false ); $request = "$(Get-FourSquareAPIBase)/addtip"; $request += "?vid=$vid&text=$([System.Web.HttpUtility]::UrlEncode($text))"; if ( $type.Length -gt 0 ) { $request += "&type=$([System.Web.HttpUtility]::UrlEncode($type))"; } $results = Execute-HTTPGetCommand -url $request; if ( $raw ) { $results; } else { [xml]$x = $results; $x.tip; $x.tip.user; } } #============================================================================ # OTHER METHODS #============================================================================ #---------------------------------------------------------------------------- function Get-FourSquare.Status() # # Returns the string "ok". # # Parameters # # raw - if raw is supplied, the raw HTTP response is displayed, # otherwise it is cast to an xml document and the appropriate # subelement is returned. #---------------------------------------------------------------------------- { param( [switch]$raw = $false ); $request = "$(Get-FourSquareAPIBase)/test"; $results = Execute-HTTPGetCommand -url $request; if ( $raw ) { $results; } else { [xml]$x = $results; $x.response; } } ############################################################################# # # INTERNAL UTILITY FUNCTIONS # ############################################################################# $script:g_FourSquarecreds = $null; $script:G_FOURSQUAREAPIBASE_V1 = "http://api.foursquare.com/v1"; #---------------------------------------------------------------------------- # function Get-FourSquareAPIBase() #---------------------------------------------------------------------------- function Get-FourSquareAPIBase() { $script:G_FOURSQUAREAPIBASE_V1; } #---------------------------------------------------------------------------- # function Execute-HTTPGetCommand() #---------------------------------------------------------------------------- function Execute-HTTPGetCommand() { param([string] $url = $null); if ( $url ) { [System.Net.WebClient]$webClient = New-Object System.Net.WebClient #$webClient.Credentials = Get-FourSquare.Credentials $creds = Get-FourSquare.Credentials; $user = $creds.username; $pass = $creds.password; $encheader = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("${user}:${pass}")) $webClient.Headers.Add("Authorization", "Basic $encheader"); [System.IO.Stream]$stream = $webClient.OpenRead($url); [System.IO.StreamReader]$sr = New-Object System.IO.StreamReader -argumentList $stream; [string]$results = $sr.ReadToEnd(); $results; } } #---------------------------------------------------------------------------- # function Execute-HTTPPostCommand #---------------------------------------------------------------------------- function Execute-HTTPPostCommand() { param([string] $url = $null, [string] $data = $null); if ( $url -and $data ) { [System.Net.WebRequest]$webRequest = [System.Net.WebRequest]::Create($url); $webRequest.ServicePoint.Expect100Continue = $false; $creds = Get-FourSquare.Credentials; $user = $creds.username; $pass = $creds.password; $encheader = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("${user}:${pass}")) $webRequest.Headers.Add("Authorization", "Basic $encheader"); #$webRequest.Credentials = Get-FourSquare.Credentials; #$webRequest.PreAuthenticate = $true; $webRequest.ContentType = "application/x-www-form-urlencoded"; $webRequest.Method = "POST"; [byte[]]$bytes = [System.Text.Encoding]::UTF8.GetBytes($data); $webRequest.ContentLength = $bytes.Length; [System.IO.Stream]$reqStream = $webRequest.GetRequestStream(); $reqStream.Write($bytes, 0, $bytes.Length); $reqStream.Flush(); [System.Net.WebResponse]$resp = $webRequest.GetResponse(); $rs = $resp.GetResponseStream(); [System.IO.StreamReader]$sr = New-Object System.IO.StreamReader -argumentList $rs; [string]$results = $sr.ReadToEnd(); $results; } } #---------------------------------------------------------------------------- # function Throw-ParameterRequired #---------------------------------------------------------------------------- function Throw-ParameterRequired() { param([string]$name); throw "Parameter '$name' is required!"; } function Get-TrueFalseString() { param([bool]$truefalse); if ( $truefalse ) { return 1; } else { return 0; } }