Exemple #1
0
        private void SearchForMovies(string query, int maxResults)
        {
            results = new List <NetFlixDbResult>();

            string queryUrl = getSignedUrl("http://api.netflix.com/catalog/titles?term=" + query + "&max_results=10");


            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(queryUrl);

            // execute the request
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                // we will read data via the response stream
                using (Stream resStream = response.GetResponseStream())
                {
                    XmlTextReader reader = new XmlTextReader(resStream);

                    while (reader.Read())
                    {
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            if (reader.Name == "catalog_title")
                            {
                                NetFlixDbResult title = GetTitleFromMovieNode(reader);
                                if (title != null)
                                {
                                    results.Add(title);
                                }
                                if (results.Count >= maxResults)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            // load up all the titles with images
            foreach (NetFlixDbResult title in results)
            {
                if (title.ImageUrlThumb != null)
                {
                    title.Title.FrontCoverPath = title.ImageUrlThumb;
                }
                //DownloadImage(title.Title, title.ImageUrlThumb);
            }
        }
        private NetFlixDbResult GetTitleFromMovieNode(XmlTextReader reader)
        {
            bool isMovie = true;
            NetFlixDbResult result = new NetFlixDbResult();

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    switch (reader.Name)
                    {
                        case "id":
                            try { result.Id =int.Parse( FindFirstSubstring(GetElementValue(reader), "[0-9]+$", true)); }
                            catch { }
                            break;

                        case "title":
                            result.Title.Name = GetElementValue(reader, "attribute", "regular");
                            break;

                        // Synopsis and other data comes from other api links
                        case "link":
                            string attrTitle = null;
                            string attrUrl = null;
                            string attrRel = null;

                            if (reader.HasAttributes)
                            {
                                while (reader.MoveToNextAttribute())
                                {
                                    switch (reader.Name)
                                    {
                                        case "href":
                                            attrUrl = reader.Value;
                                            break;
                                        case "rel":
                                            attrRel = reader.Value;
                                            break;
                                        case "title":
                                            attrTitle = reader.Value;
                                            break;
                                    }
                                }
                                // Move the reader back to the element node.
                                reader.MoveToElement();
                            }

                            // Find out which item we have here
                            string attrSignedUrl = getSignedUrl(attrUrl);
                            switch (attrTitle)
                            {
                                case "synopsis":
                                    try { result.Title.Synopsis = getNonHTML(getExternalData(attrUrl, attrTitle)); }
                                    catch { result.Title.Synopsis = ""; }
                                    break;
                                case "cast":
                                    try
                                    {
                                        // Cast separated by:  :::
                                        string cast = getNonHTML(getExternalData(attrUrl, attrTitle));
                                        string[] actors = Regex.Split(cast, ":::");
                                        for (int i = 0; i < actors.Length; i++)
                                        {
                                            result.Title.AddActingRole(actors[i], string.Empty);
                                        }
                                    }
                                    catch {  }
                                    break;
                                case "directors":
                                    try
                                    {
                                        // Cast separated by:  :::
                                        string directors = getNonHTML(getExternalData(attrUrl, attrTitle));
                                        string[] dirs = Regex.Split(directors, ":::");
                                        for (int i = 0; i < dirs.Length; i++)
                                        {
                                            result.Title.AddDirector(new OMLSDKPerson(dirs[i]));
                                        }
                                    }
                                    catch {  }
                                    break;
                                case "web page":
                                    result.Title.OfficialWebsiteURL = attrUrl;
                                    break;
                            }
                            break;

                        case "release_year":
                            try { result.Title.ReleaseDate = new DateTime(int.Parse(GetElementValue(reader)), 1, 1); }
                            catch { }
                            break;

                        case "box_art":
                            result.ImageUrl = GetElementValue(reader, "attribute", "large");
                            result.ImageUrlThumb = result.ImageUrl;
                            break;

                        case "runtime":
                            result.Title.Runtime = int.Parse(GetElementValue(reader))/60;
                            break;

                        case "category":
                            string scheme = GetElementValue(reader, "attribute", "scheme", false);
                            string label = GetElementValue(reader, "attribute", "label");
                            if (scheme == "http://api.netflix.com/categories/genres")
                            {
                                result.Title.AddGenre(label);
                            }
                            else if (scheme == "http://api.netflix.com/categories/mpaa_ratings")
                            {
                                result.Title.ParentalRatingReason = label;
                            }
                            break;
                    }
                }
                else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "catalog_title") break;

                // See if Television is in the genre list to determine if its a movie or not
                // if we're not a movie let's move on
                if (!isMovie) break;
            }

            return (!isMovie) ? null : result;
        }
Exemple #3
0
        private NetFlixDbResult GetTitleFromMovieNode(XmlTextReader reader)
        {
            bool            isMovie = true;
            NetFlixDbResult result  = new NetFlixDbResult();

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    switch (reader.Name)
                    {
                    case "id":
                        try { result.Id = int.Parse(FindFirstSubstring(GetElementValue(reader), "[0-9]+$", true)); }
                        catch { }
                        break;

                    case "title":
                        result.Title.Name = GetElementValue(reader, "attribute", "regular");
                        break;

                    // Synopsis and other data comes from other api links
                    case "link":
                        string attrTitle = null;
                        string attrUrl   = null;
                        string attrRel   = null;

                        if (reader.HasAttributes)
                        {
                            while (reader.MoveToNextAttribute())
                            {
                                switch (reader.Name)
                                {
                                case "href":
                                    attrUrl = reader.Value;
                                    break;

                                case "rel":
                                    attrRel = reader.Value;
                                    break;

                                case "title":
                                    attrTitle = reader.Value;
                                    break;
                                }
                            }
                            // Move the reader back to the element node.
                            reader.MoveToElement();
                        }

                        // Find out which item we have here
                        string attrSignedUrl = getSignedUrl(attrUrl);
                        switch (attrTitle)
                        {
                        case "synopsis":
                            try { result.Title.Synopsis = getNonHTML(getExternalData(attrUrl, attrTitle)); }
                            catch { result.Title.Synopsis = ""; }
                            break;

                        case "cast":
                            try
                            {
                                // Cast separated by:  :::
                                string   cast   = getNonHTML(getExternalData(attrUrl, attrTitle));
                                string[] actors = Regex.Split(cast, ":::");
                                for (int i = 0; i < actors.Length; i++)
                                {
                                    result.Title.AddActingRole(actors[i], string.Empty);
                                }
                            }
                            catch {  }
                            break;

                        case "directors":
                            try
                            {
                                // Cast separated by:  :::
                                string   directors = getNonHTML(getExternalData(attrUrl, attrTitle));
                                string[] dirs      = Regex.Split(directors, ":::");
                                for (int i = 0; i < dirs.Length; i++)
                                {
                                    result.Title.AddDirector(new OMLSDKPerson(dirs[i]));
                                }
                            }
                            catch {  }
                            break;

                        case "web page":
                            result.Title.OfficialWebsiteURL = attrUrl;
                            break;
                        }
                        break;

                    case "release_year":
                        try { result.Title.ReleaseDate = new DateTime(int.Parse(GetElementValue(reader)), 1, 1); }
                        catch { }
                        break;

                    case "box_art":
                        result.ImageUrl      = GetElementValue(reader, "attribute", "large");
                        result.ImageUrlThumb = result.ImageUrl;
                        break;

                    case "runtime":
                        result.Title.Runtime = int.Parse(GetElementValue(reader)) / 60;
                        break;

                    case "category":
                        string scheme = GetElementValue(reader, "attribute", "scheme", false);
                        string label  = GetElementValue(reader, "attribute", "label");
                        if (scheme == "http://api.netflix.com/categories/genres")
                        {
                            result.Title.AddGenre(label);
                        }
                        else if (scheme == "http://api.netflix.com/categories/mpaa_ratings")
                        {
                            result.Title.ParentalRatingReason = label;
                        }
                        break;
                    }
                }
                else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "catalog_title")
                {
                    break;
                }

                // See if Television is in the genre list to determine if its a movie or not
                // if we're not a movie let's move on
                if (!isMovie)
                {
                    break;
                }
            }

            return((!isMovie) ? null : result);
        }