Exemplo n.º 1
0
        public BookApiInfo ExecuteByIsbn(string isbn)
        {
            var url     = string.Format("http://iss.ndl.go.jp/api/opensearch?isbn={0}", isbn);
            var xmlText = WebApiUtil.GetResponseText(url);

            BookApiInfo info = new BookApiInfo
            {
                ApiName          = "NationalDietLibraryApi",
                ResponseText     = xmlText,
                ResponseDatetime = DateTime.Now
            };

            if (!string.IsNullOrEmpty(xmlText))
            {
                var doc = new XmlDocument();
                doc.LoadXml(xmlText);

                var items = doc.GetElementsByTagName("item");

                foreach (XmlNode item in items)
                {
                    var bookItem = new BookApiInfo.Item();
                    foreach (XmlNode node in item.ChildNodes)
                    {
                        switch (node.Name)
                        {
                        case "dc:title":
                            bookItem.Title = node.InnerText;
                            break;

                        case "dcndl:volume":
                            bookItem.Subtitle = node.InnerText;
                            break;

                        case "dc:publisher":
                            bookItem.Publisher = node.InnerText;
                            break;

                        case "dc:identifier":
                            foreach (XmlAttribute attr in node.Attributes)
                            {
                                if (attr.InnerText == "dcndl:ISBN")
                                {
                                    bookItem.Isbn13 = node.InnerText;
                                }
                            }
                            break;

                        case "dc:creator":
                            bookItem.Authors = node.InnerText;
                            break;

                        case "pubDate":
                            bookItem.PublishedDate = node.InnerText;
                            break;

                        case "dc:subject":
                            if (node.Attributes.Count == 0)
                            {
                                bookItem.Categories = node.InnerText;
                            }
                            break;
                        }
                    }
                    info.BookItems.Add(bookItem);
                }
            }
            return(info);
        }
Exemplo n.º 2
0
        public BookApiInfo ExecuteByIsbn(string isbn)
        {
            var url  = string.Format("https://www.googleapis.com/books/v1/volumes?q=isbn:{0}", isbn);
            var json = WebApiUtil.GetResponseText(url);

            BookApiInfo info = new BookApiInfo
            {
                ApiName          = "GoogleBooksApi",
                ResponseText     = json,
                ResponseDatetime = DateTime.Now,
            };
            var jres = JObject.Parse(json);

            info.ItemCount = jres["totalItems"].Value <int>();
            if (info.ItemCount > 0)
            {
                foreach (var item in jres["items"])
                {
                    BookApiInfo.Item bookItem = new BookApiInfo.Item();

                    var volumeItems = (JObject)item["volumeInfo"];
                    foreach (var kvp in volumeItems)
                    {
                        switch (kvp.Key)
                        {
                        case "title":
                            bookItem.Title = kvp.Value.Value <string>();
                            break;

                        case "subtitle":
                            bookItem.Subtitle = kvp.Value.Value <string>();
                            break;

                        case "publisher":
                            bookItem.Publisher = kvp.Value.Value <string>();
                            break;

                        case "authors":
                            bookItem.Authors = string.Join(" ,", kvp.Value.Values <string>());
                            break;

                        case "publishedDate":
                            bookItem.PublishedDate = kvp.Value.Value <string>();
                            break;

                        case "description":
                            bookItem.Description = kvp.Value.Value <string>();
                            break;

                        case "industryIdentifiers":
                            var industryIdentifiers = (JArray)kvp.Value;
                            foreach (var ind in industryIdentifiers)
                            {
                                var val = ind["identifier"].Value <string>();
                                switch (ind["type"].Value <string>())
                                {
                                case "ISBN_13":
                                    bookItem.Isbn13 = val;
                                    break;

                                case "ISBN_10":
                                    bookItem.Isbn10 = val;
                                    break;
                                }
                            }
                            break;

                        case "pageCount":
                            bookItem.PageCount = kvp.Value.Value <string>();
                            break;

                        case "language":
                            bookItem.Language = kvp.Value.Value <string>();
                            break;

                        case "infoLink":
                            bookItem.InfoLink = kvp.Value.Value <string>();
                            break;

                        case "printType":
                            bookItem.PrintType = kvp.Value.Value <string>();
                            break;

                        case "categories":
                            bookItem.Categories = string.Join(", ", kvp.Value.Values <string>());
                            break;
                        }
                    }
                    info.BookItems.Add(bookItem);
                }
            }
            return(info);
        }