My personal script:
http://2011sg.poshcode.org/1528
Average Rating: 3.50 by 2 users.
(Download it)
#
#
# 2011 Scripting Games Advanced Event 7: Map User's Names and Twitter Names with PowerShell
#
# by F.Richard 2011-04
#
#
#Requires -Version 2.0
[CmdletBinding()]
Param(
[Parameter(Mandatory = $false, Position = 0, ValueFromPipeLine = $true, ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[String] $url = "http://www.sqlsaturday.com/70/networking.aspx",
[Parameter(Mandatory = $false, Position = 1, ValueFromPipeLine = $False, ValueFromPipelineByPropertyName = $False)]
[ValidateNotNullOrEmpty()]
[String] $output = "TwitterName.csv"
)
# Get-WebPage
# Windows PowerShell, Invalid Certificates, and Automated Downloading
#http://blogs.technet.com/b/heyscriptingguy/archive/2010/07/25/windows-powershell-invalid-certificates-and-automated-downloading.aspx
# + some personal modifications
Function Get-WebPage {
<#
.Synopsis
Gets the content at a specified http url
.Parameter url
Url that returns content
.Parameter file
Optional parameter that redirects download of content to a file. If left out
content is returned as a string
.Parameter useragent
to define user agent
.Parameter user
user to permit connection
.Parameter password
password to permit connection
.Parameter domain
user's domain to permit connection
.Parameter proxyurl
proxy Url to permit connection
.Parameter proxyurl
proxy port to permit connection
.Parameter force
Forces the acceptance of content from an untrusted source (eg. invalid certificate)
.EXAMPLE
Get-WebPage -url "http://www.mysite.com"
return web page mysite.com
.EXAMPLE
Get-WebPage -url "http://www.mysite.com" -user "usr" -password "pass" -domain "mydom"
return web page mysite.com using particular user
.EXAMPLE
Get-WebPage -url "http://www.mysite.com" -file "contentsite.htm" -proxyurl "proxy.comp.net" -proxyport "8080"
return web page mysite.com to file "contentsite.htm" using proxy proxy.comp.net:8080
.EXAMPLE
Get-WebPage -url "http://www.mysite.com" -useragent "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"
return web page mysite.com using specific user agent
#>
Param(
[string] $url,
[string] $file = "",
[string] $useragent = "",
[string] $user = "",
[string] $password = "",
[string] $domain = "",
[string] $proxyurl = "",
[string] $proxyport = "",
[switch] $force
)
if($force) {
[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
}
$webclient = New-Object system.net.webclient
#Proxy required
if ($proxyurl -ne "" -or !$webClient.Proxy.IsBypassed($url)) {
$proxy = New-Object System.Net.WebProxy($proxyurl, $proxyport)
#$proxy.Credentials = (Get-Credential).GetNetworkCredential()
$webclient.Proxy = $proxy
}
if ($useragent -ne "") {
$webclient.Headers.Add("user-agent", $useragent) # :ex "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"
}
if ($user -ne "") {
$webclient.Credentials = New-Object System.Net.NetworkCredential($user, $password, $domain)
}
$webClient.UseDefaultCredentials = $true
if ($file -eq "") {
return $webclient.DownloadString($url)
} else {
$webclient.DownloadFile($url, $file)
}
}
Function Get-RegexName {
<#
.Synopsis
return array of captured regex group
.Parameter content
content to capture
.Parameter regex
regular expression with groupto capture
#>
Param(
[string] $content,
[string] $regex,
[string] $regexaction = "match",
[string] $regexoptions = "IgnoreCase"
)
$arrName = @( )
$matches = [regex]::Matches($content, $regex, $regexoptions)
foreach($match in $matches) {
if ($match.Groups["name"].Value.Trim().length -gt 0) {
$arrName += $match.Groups["name"].Value.Trim()
}
}
return $arrName
}
Function Get-TwitterToUser {
<#
.Synopsis
return user name from twitter name using csv file
.Parameter file
csv file
#>
Param(
[string] $file
)
$hashTwitterToUser = @{ }
Import-Csv $output | ForEach-Object { $hashTwitterToUser[$_.twitname] = $_.username }
return $hashTwitterToUser
}
Function Get-UserToTwitter {
<#
.Synopsis
return twitter name from user name using csv file
.Parameter file
csv file
#>
Param(
[string] $file
)
$hashUserToTwitter = @{ }
Import-Csv $output | ForEach-Object { $hashUserToTwitter[$_.username] = $_.twitname }
return $hashUserToTwitter
}
Function Get-TwitterName {
<#
.Synopsis
write all twitter name from a web page to a .csv file
.Parameter url
url to examine default: "http://www.sqlsaturday.com/70/networking.aspx"
.Parameter output
outup filename default: "TwitterName.csv"
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $false, Position = 0, ValueFromPipeLine = $true, ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[String] $url = "http://www.sqlsaturday.com/70/networking.aspx",
[Parameter(Mandatory = $false, Position = 1, ValueFromPipeLine = $False, ValueFromPipelineByPropertyName = $False)]
[ValidateNotNullOrEmpty()]
[String] $output = "TwitterName.csv"
)
# Get web page content to file
$strCurDir = $(if ($strCurDir) {$strCurDir} else {if ($MyInvocation.MyCommand.CommandType -eq "Function") {(Get-Location).Path} else {Split-Path -parent $MyInvocation.MyCommand.Path} })
$content = Get-WebPage -url $url -file "$strCurDir\temp.txt" -force
$content = Get-Content "$strCurDir\temp.txt"
Remove-Item "$strCurDir\temp.txt"
[Array] $arrTwitUserName = @( )
# get all twitter name
# must use (?!:) trick to do not have line like this one
# a href="http://www.twitter.com/http://twitter.com/sqlvariant
$regex = "]*>(? ".*?)
$arrTD = Get-RegexName -content $content -regex $regex
foreach ($td in $arrTD) {
# get only user name and a href ref
$td_simplified = [regex]::Replace($td, "<[/]?(font|span|img|[ovwxp]:\w+)[^>]*?>", "$1", "IgnoreCase")
# Get Twitter name
$regex = "twitter.com/(?[^?/ ]*)\b(?!:)"
$twitname = Get-RegexName -content $td_simplified -regex $regex
if ($twitname) {
# Get username
$username = [regex]::Replace($td_simplified, "<[/]?(a|[ovwxp]\w+)[^>]*?>", "$1", "IgnoreCase")
# create new object
$obj = New-Object PSObject
$obj | Add-Member -MemberType noteproperty -Name "twitname" -Value $twitname.Trim()
$obj | Add-Member -MemberType noteproperty -Name "username" -Value $username.Trim()
$arrTwitUserName += $obj
}
}
# Write file
$arrTwitUserName | Export-Csv $output -NoTypeInformation
}
# main
Get-TwitterName -url $url -output $output
# test to user name from twitter name from csv file
$hashTwitterToUser = Get-TwitterToUser -file $output
Write-host "test Get-TwitterToUser: 'gunnyek' real name is"$hashTwitterToUser["gunneyk"]
# test to return user name from twitter name from csv file
$hashUserToTwitter = Get-UserToTwitter -file $output
Write-host "test Get-TwitterToUser: 'David Taylor' twitter name is"$hashUserToTwitter["David Taylor"]
No comments:
Post a Comment