IronPython and LiveFX: Accessing Profiles

I recently got access to both the Windows Azure and Live Framework CTP programs. Frankly, I’m very interested in Live Mesh, so I decided to start with a simple LiveFX program. Scott (aka ScottIsAFool) at LiveSide posted a “quick and dirty” console app that pulls info from a user’s profile via LiveFx. It’s not Mesh per se, but it does use the same framework and resource model so I decided to port it to IronPython. FYI, this app won’t run unless you’ve been received a LiveFx CTP token and provisioned yourself.

#Add LiveFX References

import sys
sys.path.append('C:\Program Files\Microsoft SDKs\Live Framework SDK\v0.9\Libraries\.Net Library')

import clr
clr.AddReference('Microsoft.LiveFX.Client')
clr.AddReference('Microsoft.LiveFX.ResourceModel')

from Microsoft.LiveFX.Client import LiveOperatingEnvironment
from Microsoft.LiveFX.ResourceModel.ProfileResource import ProfileType
from System.Net import NetworkCredential

from devhawk import linq

#get username and password from the user

uid = raw_input("Enter Windows Live ID: ")
pwd = raw_input("Enter Password: ")
creds = NetworkCredential(uid, pwd, "https://user-ctp.windows.net")

#print out user's info

loe = LiveOperatingEnvironment()
loe.Connect(creds)

general = linq.Single(loe.Profiles.Entries,  
  lambda e: e.Resource.Type == ProfileType.General)

print loe.Mesh.ProvisionedUser.Name     
print loe.Mesh.ProvisionedUser.Email
print general.Resource.ProfileInfo.PersonalStatusMessage
print linq.Count(loe.Contacts.Entries)

I did modify the app slightly, reading the WLID and password off the console – I was sure I would accidently post my personal credentials if I left them embedded in the app. Otherwise, it’s a straight port. First, I add references the LiveFX dlls. Since they’re not local to my script, I add the directory where they’re installed to sys.path, which lets me call clr.AddReference directly. Then I retrieve the user’s ID and password using raw_input (Python’s equivalent to Console.ReadLine). Finally, I connect to the user’s LiveOperatingEnvironment and pull their name, email address, personal status message and the number of contacts they have.

As per the original app, I use LINQ to find the right profile as well as count the number of contacts. I was able to reuse the linq.py file I wrote for my Rock Band song list screen scraper (though I did have to add the Count function since I hadn’t needed it previously). I’ve posted this script on my SkyDrive, and it includes my most recent linq.py file.

BTW, it doesn’t appear that you can set the PersonalStatusMessage programmatically, at least not currently. I was thinking it would be cool to build an app that sets your PSM via Twitter, but the set method of PersonalStatusMessage is marked internal. In fact, all the set methods of all the profile properties I looked at are marked internal. If someone knows how to update LiveFX resource objects in the current CTP, I’d appreciate it if you dropped me a line or left me a comment.

Morning Coffee 169

  • Check out the crowd
    for a the Washington Capitals developmental camp scrimmage last week (My parents are in their somewhere). Standing room only in the practice facility to watch a bunch of kids, most of whom won’t ever make it to the NHL, in July. If you think Washington can’t be a hockey town, you are sorely mistaken.
  • Speaking of the Caps, they are establishing a “spirit squad“? Is that really necessary? (short answer: no). Peerless’ take is hilarious.
  • Seshadri Vijayaraghavan is a tester on the DLR team and he’s been writing quite a bit about the DLR hosting API. He’s got a series of posts about hosting, invoking and redirecting output from IronPython in a C# application.
  • I haven’t seen an official announcement, but mobile access to Live Mesh is available by pointing your phone browser to http://m.mesh.com. It’s mostly a web view of the Live Desktop, though there is a feature to upload photos from your phone. However, for some reason that feature doesn’t work for me right now. I don’t get the “browse” button.
  • ASP.NET MVC Preview 4 is available for download. Phil Haack has a few details that ScottGu didn’t cover. Scott Hanselman shows off some AJAX stuff.
  • Speaking of Scott Hanselman, he highlights the return of Terrarium from Bil Simser. Scott mentions that most Terrarium animal implementations were big collections of nested if statements. I wonder if F# pattern matching would be a cleaner approach?
  • Ted Neward obviously never “even tangentially” touched politics, as I think they have far worse flame wars far more often than we have in the software industry. However, certainly the Scala flame war he’s commenting on seems fairly counterproductive.
  • Brad Wilson runs into a wall trying to convert a string to an arbitrary Nullable<T>.He doesn’t find an answer, but I found reading thru the steps he took to try and find an answer strangely compelling.
  • Jeff Atwood argues that Maybe Normalization isn’t Normal. It’s mostly a collection of information from other places, including a compilation of high-scale database case studies. But it’s a useful collection of info and links, with a little common-sense thrown in for good measure.
  • I have a hard time imagining Pat Helland camping.

Morning Coffee 167

  • If you’re a gamer, you’re probably already well aware that E3 is this week. The Too Human demo has already been released. I have a friend who’s been working on “something” that will be announced today (I think).
  • Live Mesh folks pushed out an update Friday. Among the new features is the ability to sync folders among peers but NOT up to the cloud. This is cool because it means I can sync my many many GB of pictures and music on my home machine backed up with Carbonite. This means I can sync them without blowing thru my 5GB Mesh storage limit.
  • It looks like there’s a new F# drop – 1.9.4.19but as usual there is no announcement or details as to what’s new. Release notes guys, look into it.  Update: Don Syme blogged the release, and it’s pretty minor. a .NET FX 3.5 SP1 bug fix, a fix for Mono, and they removed WebRequest.GetResponseAsync to make F# work on Silverlight. And the release notes are in the readme. My bad.
  • Speaking of F#, it was “partially inspired” by OCaml, so when I see papers related to OCaml, I immediately wonder if I an apply the described techniques to F#. “Catch me if you can, Towards type-safe, hierarchical, lightweight, polymorphic and efficient error management in OCaml” is one such paper. (via LtU)
  • Speaking of functional programming, Matthew Podwysocki posted a bunch of FP links as well as a Code Gallery Sample on FP in C#. Good stuff.
  • As per Scott Guthrie, it looks like there’s a new ASP.NET MVC drop coming this week.
  • Based on posts by Ted Neward, Dare Obasanjo and Steve Vinoski, Google Protocol Buffers sounds like it’s going to be a dud. Note, I haven’t looked at it depth personally, I’m just passing on opinions of some folks I read and trust.
  • Speaking of Dare, both he and James Hamilton take a look at Cassandra and come away impressed. I wonder how easy it is to code against from Python and/or .NET?
  • Bart de Smet has a cool sample of calling out to PowerShell from IronRuby via the backtick command. Pretty cool, but it would even cooler to show how to call out to PS and return .NET objects to Ruby (though that would probably not be spec compliant for the backtick command).
  • Here’s a MS code name I had never heard before – Zermatt. It’s “a framework for implementing claims-based identity in your applications.” (via Steve Gilham)

Morning Coffee 164

  • Big news since my last Morning Coffee post was the announcement of Live Mesh. I’ve been running it for about a month, and I’m really digging it. Make sure you check out the team blog and watch the developer tour video (be on the lookout for IPy about half way thru the video)

ALT.NET

  • I had a great time @ the ALT.NET open space conference last weekend. I was somewhat distracted on Saturday as due to a family communication mixup, I had to bring my son Patrick with me. Jeffrey Palermo shot a cute video of him (3 minutes in) where he explains that he’s at the conference “to be with my dad”. Having a five year old is a little distracting, but everyone was amazingly cool with having him around. When he gets a little older I have no doubt he’ll be attending conferences and leading open sessions.
  • I did a session on F#, but it felt kinda all over the place. I hadn’t touched F# in a few months and it showed IMO. Matt Podwysocki was there to help keep the session from devolving into mass chaos. Thanks Matt.
  • My favorite session of the conference was Scott Hanselman’s “Are We Innovating?” talk, which I think originated from a question I asked him: There are many examples of large OSS projects in other dev communities that get ported to .NET (NHibernate, NAnt, MonoRail, etc). Can you name one that’s gone the other way? I can’t.
  • I took Matt’s advice and joined the local ALT.NET Seattle group.

DyLang Stuff

  • Martin Maly posts about how dynamic method dispatches are cached in three different layers by the DLR. You shouldn’t care about this stuff if you’re a DLR language user, but you will certainly care about it if you’re a DLR language builder.
  • I’m really excited to see Phil Haack (whom I met F2F @ ALT.NET) is experimenting with IronRuby & ASP.NET MVC. True, I’d rather it was IPy, but his Routes.LoadFromRuby would work with Python with very little code change.
  • Note to self, take a deeper look at Twining, the IPy database DSL by David Seruyange.
  • Daily Michael Foord – Ironclad 0.2 Released. Ironclad is a project to implement Python’s C extension API in C# so that IronPython could load standard Python C modules like SciPy and NumPy. So far, they’re able to load the bz2 module.

Other Stuff

  • Congrats to Brad and Jim for shipping xUnit.net 1.0.
  • Everyone seems to be jumping on the functional C# coding bandwagon. Bart De Smet’s series on pattern matching in C# is currently at eight posts. Now Luca Bolognese is in on the action, with three posts so far on functional code in C#. I like how Luca keeps writing that the C# syntax is “not terrible” for functional programming. Again, why suffer thru the “not terrible” syntax when you could be using F# instead? (via Charlie Calvert)
  • I need to take a look at VLinq. Charlie and Scott Hanselman both mentioned it recently.
  • I would like to have been in the conversation with Ted Neward, Neal Ford, Venkat Subramaniam, Don Box and Amanda Silver.
  • I haven’t had any time to play with XNA of late, which means the great list of GDC videos Dave Weller posted on the XNA team blog will remain beyond my ability to invest time for now.
  • There’s a new drop of Spec# from MS Research. IronRuby is using Spec# heavily as I recall.

Morning Coffee 151

  • Unity’s first CTP was just over two weeks ago, but according to Grigori Melnik, it’s shipping just over two weeks from now. That seems pretty speedy to me. By the time I get a change to take a closer look at Unity, it’ll probably have shipped.
  • I discovered Matthew Podwysocki blog via DNK. I don’t typically subscribe to blogs that I discover via DNK, but Matthew has written about IoC/Unity, F# and DLR lately so I’m thinking I should be a regular reader.
  • Corporate VP David Treadwell has an extensive post on updates to the Windows Live Platform Services that are being unveiled at MIX next week. The updates include the new WL Messenger Library, a new SDK for WL ID Delegated Authentication, a new WL Photo API, a new CTP of WL Tools, standardized support for AtomPub, updates to WL Contacts API and Sivlerlight Streaming and a new “experimental” service called Application Based Storage that “allows application developers to store a small amount of state/configuration data in the WL data centers on behalf of a user”. I’m sure there’ll be more WL news at the MIX conference proper, but that’s quite a good chunk of features to start digging into. Personally, I’m particularly interested in WL Delegated Auth, esp. how it deals with phishing, something I don’t think OAuth handles very well.
  • Windows Live isn’t the only group making announcements in advance of MIX. Adobe announced a research project that allows “cross-compiling existing code from C, C++, Java, Python, and Ruby to ActionScript.” This seems pretty obviously a response to Silverlight 2.0′s embedded CLR, announced last year @ MIX. Support for C++ is very interesting – Adobe evangelist Ted Patrick claims they were even able to cross-compile Quake 1 to Flash. Interesting, but this is an internal research project @ Adobe with no projected release date while Silverlight 2.0 goes into beta next week.