Programatically Accessing Netflix in C#
When I lived with a few roommates, we all shared my Netflix account and pretty much exclusively used it to add movies to instant which were then streamed using the Netflix app for my Xbox 360. Since there were 3 of us, this meant that we had a ton of random movies constantly being shoveled into the queue and as a result, my queue got really messy… like 300 movies in no particular order messy … which made it particularly difficult to find a movie that one of us added.
To solve this dilemma, one of my roommates would painstakingly reorder our entire Netflix queue one item at a time to alphabetize them. He didn’t mind doing it while commercials were on TV and he was an auditor by trade so there weren’t any errors in the ordering. However, this seemed like about the worst possible way to do this, so I checked the Netflix site and discovered, ”Hey, there’s an API.” Even more awesomely, there is a C# library for Netflix that some generous developer decided to share on CodePlex which greatly simplifies development.
Library in hand, I eagerly produced a simple C# Netflix app that lets you sort your Netflix queue. This is probably the least optimal implementation, but it works!
The application works only in 3 steps:
- Authenticate
- Clear the instant queue
- Add entries to the queue in the order you want them in
The following code shows how authentication works.
/* The following came straight from the Netflix signup / API page
* and are used by OAUTH
*
* Key: <removed>
* Application:<removed>
* Key: <removed>
* Shared Secret: <removed>
* Status: active Registered: 2 seconds ago Rate Limits
* 4 Queries per second
* 5,000 Queries per day
*
* Update your app.config with:
* ConsumerKey as a string set to the "Key" field
* ConsumerSecret as a string set to the "Shared Secret" field
* UserId should be created as a string, but not set
* UserAccessToken should be created as a string, but not set
* UserAccessTokenSecret should be created as a string, but not set
*/
NetflixConnection netflixConnection;
AccessToken userAccessToken;
User user;
String NetflixAccessToken = "";
int attempts = 0;
int maxAttempts = 5;
private void authenticate()
{
attempts++;
netflixConnection = new NetflixConnection(Properties.Settings.Default.ConsumerKey, Properties.Settings.Default.ConsumerSecret);
try
{
if (String.IsNullOrEmpty(NetflixAccessToken))
{
RequestToken currentRequestToken = netflixConnection.GenerateRequestToken();
Process.Start(currentRequestToken.PermissionUrl);
// Race condition: If you call ConvertToAccessToken() before the previous process finishes,
// the token is invalidated. Blocking using a messagebox.
//
// TODO: Figure out better way to solve this issue
MessageBox.Show("After you have enabled permissions from Netflix, click OK.");
userAccessToken = currentRequestToken.ConvertToAccessToken();
Properties.Settings.Default.UserId = userAccessToken.UserId;
Properties.Settings.Default.UserAccessToken = userAccessToken.Token;
Properties.Settings.Default.UserAccessTokenSecret = userAccessToken.TokenSecret;
Properties.Settings.Default.Save();
// TODO: Persist the UserId / UserAccessToken / UserAccessTokenSecret
// so the user doesn't need to do this every time
// Turn off auth button, turn on sort buttons
authenticateButton.IsEnabled = false;
sortAZ.IsEnabled = true;
sortZA.IsEnabled = true;
}
}
catch (Exception e)
{
MessageBox.Show("Something went wrong, trying again." + e.InnerException.ToString());
if (attempts < maxAttempts)
{
authenticate();
}
}
user = new User(userAccessToken, netflixConnection);
}
The following code shows how I retrieve the instant queue which will then be removed one entry at a time.
private void sortAZ_Click(object sender, RoutedEventArgs e)
{
user.InstantQueue.Refresh();
QueueItem[] instantQueue = user.InstantQueue.ToArray();
Console.WriteLine("Unsorted Queue");
for (int i = 0; i < instantQueue.Length; i++)
{
Console.WriteLine(instantQueue[i].Title);
}
Array.Sort(instantQueue,sortTitleAZ());
Console.WriteLine("-- Sorted A-Z");
for (int i = 0; i < instantQueue.Length; i++)
{
user.InstantQueue.Remove(instantQueue[i]);
Console.WriteLine(instantQueue[i].Title);
}
The following code shows how the entries are then added back into the instant queue in order.
// Add all entries in order
Console.WriteLine("Adding entries in order");
for (int i = 0; i < instantQueue.Length; i++)
{
user.InstantQueue.Add(instantQueue[i].Id);
// 250 ms / query = 4 queries / second
// limit is 4 queries / second
// using 300 for good measure
Console.WriteLine(instantQueue[i].Title);
}
}
A rather simple application, but a true time saver for my poor roommate. If you’re interested in hacking up my application, feel free to grab it from my fileshare. The only thing you need to do to get it working is get a developer key/secret from Netflix, double click the “Settings.settings” file in the project from CodePlex, and update the ConsumerKey and ConsumerSecret before compiling.



