Netflix Queue Sorter in C#/WinForms

Roughly three years ago, I coded my first OAuth / RESTful application.  It was a utility for Netflix that would sort the “Instant” queue of movies that I had in my list. I did this because my obsessive compulsive roommate would actually go through and manually sort the Queue and I felt bad for him doing it :).  Before I do a shallow dive into my app, you can download the sources to my netflix app.

The following screenshot shows the queue sorter running:

My Netflix queue sorter app running on Windows.

If the app is configured correctly (I removed my key and shared secret from the sources), clicking the Authenticate button lets you authenticate with Netflix and then sort your queue by clicking the A-Z and Z-A buttons.

How does it work?

First, the authenticate method will authenticate against the Netflix OAuth API to retrieve the tokens for getting a user.  The following code does this:

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);
        }

Next, you will have the user tokens and the sort buttons get enabled.  After the user (my roommate) clicks the now active A-Z button, a simple sort is run against the user’s instant queue using a comparer and the default Array sort method (probably uses a quicksort or heapsort under the hood).  The following code shows how this is done:

        // Helper for Sorting A-Z by title
        private class sortTitleAtozHelper : IComparer
        {
            int IComparer.Compare(object a, object b)
            {
                QueueItem left = (QueueItem)a;
                QueueItem right = (QueueItem)b;
                return left.Title.CompareTo(right.Title);
            }
        }

        public static IComparer sortTitleAZ()
        {
            return (IComparer)new sortTitleAtozHelper();
        }

I had issues with Netflix letting me directly insert the list so, instead, I clear the list and reinsert the users’ movies back to the queue as shown in the following snippet.

        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);
                Thread.Sleep(300);
            }

            // 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);
                Console.WriteLine(instantQueue[i].Title);
                Thread.Sleep(300);
            }

        }

So there you have it, one old app, a hundred lines of C#, and days of my roommate’s life returned 🙂