PoshCode Archive  Artifact Content

Artifact ca89a606699fde90bb99515bd3b2fb2adb3e81c3dcc59141223c3dd753b05d85:

  • File Search-Twitter.ps1 — part of check-in [9af81476f3] at 2018-06-10 13:07:12 on branch trunk — From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes (user: Lee Holmes size: 1774)

# encoding: ascii
# api: powershell
# title: Search-Twitter.ps1
# description: From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
# version: 0.1
# type: class
# author: Lee Holmes
# license: CC0
# x-poshcode-id: 2211
# x-archived: 2016-03-19T02:52:16
# x-published: 2011-09-09T21:42:00
#
#
##############################################################################
##
## Search-Twitter
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
##############################################################################

<#

.SYNOPSIS

Search Twitter for recent mentions of a search term

.EXAMPLE

Search-Twitter PowerShell
Searches Twitter for the term "PowerShell"

#>

param(
    ## The term to search for
    $Pattern = "PowerShell"
)

Set-StrictMode -Version Latest

## Create the URL that contains the Twitter search results
Add-Type -Assembly System.Web
$queryUrl = 'http://integratedsearch.twitter.com/search.html?q={0}'
$queryUrl = $queryUrl -f ([System.Web.HttpUtility]::UrlEncode($pattern))

## Download the web page
$wc = New-Object System.Net.WebClient
$wc.Encoding = [System.Text.Encoding]::UTF8
$results = $wc.DownloadString($queryUrl)

## Extract the text of the messages, which are contained in
## segments that look like "<div class='msg'>...</div>"
$matches = $results |
    Select-String -Pattern '(?s)<div[^>]*msg[^>]*>.*?</div>' -AllMatches

foreach($match in $matches.Matches)
{
    ## Replace anything in angle brackets with an empty string,
    ## leaving just plain text remaining.
    $tweet = $match.Value -replace '<[^>]*>', ''

    ## Output the text
    [System.Web.HttpUtility]::HtmlDecode($tweet.Trim()) + "`n"
}