Exemple #1
0
        /// <summary>
        /// Populates a publication object from an OCLC number
        /// </summary>
        /// <param name="oclcNumber">The OCLC number representing the material</param>
        /// <returns>A publication object</returns>
        async Task <Publication> FetchPublicationFromOCLCNumber(string oclcNumber)
        {
            var baseUri  = @"http://www.worldcat.org/webservices/catalog/content/";
            var queryURI = baseUri + oclcNumber + "?wskey=" + wsKey;

            var request = WebRequest.CreateHttp(queryURI);

            using (var response = await request.GetResponseAsync()) {
                var doc = XDocument.Load(response.GetResponseStream());
                var pub = Publication.FromXML(doc);
                pub.OCLCNumber = oclcNumber;

                try {
                    var imageUriTasks = from num in await FetchAllOCLCNumbers(oclcNumber)
                                        select GetOCLCCoverImageUriAsync(num);

                    foreach (var task in imageUriTasks)
                    {
                        var uris = await task;
                        foreach (var uri in uris)
                        {
                            try {
                                var image = await GetBitmapImage(uri);

                                if (image != null)
                                {
                                    pub.CoverImage = image;
                                    return(pub);
                                }
                            } catch (WebException ex) {
                                Console.WriteLine(ex.Message);
                                Console.WriteLine(ex.StackTrace);
                            }
                        }
                    }

                    foreach (var uri in await GetOCLCCoverImageUriAsync(oclcNumber))
                    {
                        try {
                            var image = await GetBitmapImage(uri);

                            if (image != null)
                            {
                                pub.CoverImage = image;
                                return(pub);
                            }
                        } catch (WebException ex) {
                            Console.WriteLine(ex.Message);
                            Console.WriteLine(ex.StackTrace);
                        }
                    }
                } catch (Exception ex) {
                    Console.WriteLine(ex.Message);
                    Console.WriteLine(ex.StackTrace);
                }

                pub.CoverImage = DrawPublicationImage(pub.Title, String.Join(", ", pub.Authors));
                return(pub);
            }
        }