Finding Out Who You’re Not Following on vLaunchpad

The vLaunchpad site is a great resource for finding the best virtualisation blogs around and in the upcoming months will once again have a voting opportunity where you can register which ones you like best. It also handily includes the Twitter id of the blogger. I was curious to find an easy way to see who I wasn’t following on Twitter from the list.

By using the Invoke-WebRequest cmdlet it’s easy to pull down the Twitter links from the web page.

Line 1 pulls down the information from the web page.

Line 2 breaks down the web page content until we get to the information we need.Firstly, we look at the Links property and filter those that include twitter content. Then for each one we split the URL on the / character and pick the username portion, e.g. from the below

https://twitter.com/username

we take username.

Since some of the URLs are incomplete a few have a blank username so we use the regex pattern ^[a-z] to match anything with at least one letter. Finally, there were some multiple entries, so we pick out the unique ones and sort them into alphabetical order.

$vlaunchpad = Invoke-WebRequest -Uri "http://planet.vsphere-land.com/"
$twitterusers = $vlaunchpad.Links | where {$_.href -match 'twitter.com/'} |foreach {($_.href -split "/")[-1]} | where {$_ -match '^[a-z]'} | select -unique | sort

vLaunchpad01

So now we need to find out who we are already following and then compare the two.

The PowerShell v3 cmdlet Invoke-RestMethod can be used to query the Twitter API. The documentation contains easy to use examples, for instance this one for finding those you are following (friends). So to find out who we are following we can make this query.

$following = Invoke-RestMethod -Uri "https://api.twitter.com/1/friends/ids.json?cursor=-1&screen_name=jonathanmedd"

$following

 

vLaunchpad02

As you can see though, this only returns the Twitter user id, not the name. Since I haven’t yet memorised all of the ids to those I am following, we need to convert them into names. The API documentation also includes a way to lookup these id values and convert them into names.One thing to watch out for is that the maximum number of ids that can be converted per query is 100, so if you are following more than that multiple queries will be required.

Doug Finke has a post on his blog which bundles all of this up into one function, i.e. makes the two Twitter API queries per 100 users, but slightly differently it is focused on those who are following you. To amend this to find those you are following, simply change the line:

$url = “$baseUrl/followers/ids.json?cursor=-1&screen_name=$($screenName)”

to

$url = “$baseUrl/friends/ids.json?cursor=-1&screen_name=$($screenName)”

(although you may also wish to change all references from followers to friends to save any confusion)

I also needed to make one other change, since by chance I was following a figure exactly divisible by 100, i.e. 600. Consequently, the last statement to make a query on the leftover failed with an error since there was nothing to query. So an updated friends based function now looks like this:

function Get-TwitterFriend ($screenName) {

$baseUrl = "https://api.twitter.com/1"
function Get-Friend ($userIds) {
$userIds = @($userIds) -join ","
$url = "$baseUrl/users/lookup.json?user_id=$($userIds)&include_entities=true"
(Invoke-RestMethod $url) |
Select  name, screen_name, location
}

$url = "$baseUrl/friends/ids.json?cursor=-1&screen_name=$($screenName)"
$friends = (Invoke-RestMethod $url).ids

$blocks   = [Math]::Floor($friends.Count / 100)
$leftover = $friends.Count % 100

for($idx=0; $idx -lt $blocks; $idx+=1) {
Get-Friend ($friends | select -First 100 -skip ($idx*100))
}

if ($leftover -ne 0){
Get-Friend ($friends | select -First $leftOver -skip ($idx*100))
}
}

 

We can now make the Twitter query we need and store the results for comparison:

$TwitterFollowing = Get-TwitterFriend jonathanmedd

$TwitterFollowing.Count

 

vLaunchpad03

All that is left is to compare $TwitterFollowing to $TwitterUsers , see who is missing, then go add those you wish to your followers list.

$TwitterUsers | foreach {if ($_ -notin $TwitterFollowing.Screen_Name){$_}}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>