Пример #1
0
        static void Main(string[] args)
        {
            var gr = new FindBooks();

            Console.Write("skriv nåt: ");
            var q = Console.ReadLine();
            //    var data = gr.FindRaw(q);
            ////System.IO.File.WriteAllText(@"C:\BOOKDATA\" + q.Replace(" ", "") + ".txt", data);


            //var path = @"C:\BOOKDATA\HMVC.txt";
            //var text = System.IO.File.ReadAllText(path);
            //var gbList = FromXmlToGoogleBookList(text);

            //foreach (var gb in gbList)
            //{

            //    Console.WriteLine("ti:  " + gb.Title);
            //    if (gb.Categories != null && gb.Categories.Length > 0)
            //    {
            //        Console.WriteLine("cat: "  + gb.Categories[0]);
            //    }

            //}

            var result = gr.Retrieve(q);

            if (result.Success)
            {
                var serBooks = ConvertToXml(result.GoogleBooks);
                System.IO.File.WriteAllText(@"C:\BOOKDATA\" + q.Replace(" ", "") + ".txt", serBooks);

                foreach (var book in result.GoogleBooks)
                {
                    Console.WriteLine(book.Title);
                }
            }
            else
            {
                Console.WriteLine(result.ErrorMessage);
            }

            //var arr = JArray.Parse(@"[ 'kuk', 'fitta' ]");

            //foreach (var a in arr)
            //{
            //    Console.WriteLine(a);
            //}

            //     dynamic jsonData = Newtonsoft.Json.JsonConvert.DeserializeObject(data);


            Console.ReadLine();
        }
Пример #2
0
        private static void LoadData(string q, IBookRepository bookRepo, IErrorlogRepository errorlogRepository)
        {
            var result = new FindBooks().Retrieve(q);

            if (!result.Success && string.IsNullOrEmpty(result.ErrorMessage) || result.GoogleBooks.Count() == 0)
            {
                errorlogRepository.AddLog(new Errorlog {
                    Message = result.ErrorMessage, Stacktrace = "", Created = DateTime.Now
                });
            }

            foreach (var gb in result.GoogleBooks)
            {
                Book book = null;

                try
                {
                    book = new Book
                    {
                        Title               = gb.Title ?? "",
                        ItemId              = gb.ItemId ?? "",
                        SubTitle            = gb.SubTitle ?? "",
                        Publisher           = gb.Publisher ?? "",
                        PublishedDate       = gb.PublishedDate ?? "",
                        Description         = gb.Description ?? "",
                        PageCount           = gb.PageCount ?? "",
                        PrintType           = gb.PrintType ?? "",
                        AverageRating       = gb.AverageRating ?? "",
                        ThumbNail           = gb.ThumbNail ?? "",
                        Language            = gb.Language ?? "",
                        CanonicalVolumeLink = gb.CanonicalVolumeLink ?? "",
                        WebReaderLink       = gb.WebReaderLink ?? "",
                        ISBN10              = gb.ISBN10 ?? "",
                        ISBN13              = gb.ISBN13 ?? "",
                        Created             = DateTime.Now,
                        Updated             = DateTime.Now
                    };
                }
                catch (Exception exception)
                {
                    errorlogRepository.AddLog(exception);
                }


                try
                {
                    var authors = gb.Authors;

                    if (authors != null && authors.Length > 0)
                    {
                        foreach (var author in authors.Select(a => new Author
                        {
                            Name = a,
                            Created = DateTime.Now,
                            Updated = DateTime.Now
                        }))
                        {
                            book.AddAuthor(author);
                        }
                    }
                }
                catch (Exception exception)
                {
                    errorlogRepository.AddLog(exception);
                }


                try
                {
                    var categories = gb.Categories;

                    if (categories != null && categories.Length > 0)
                    {
                        foreach (var category in categories.Select(c => new Category
                        {
                            Name = c,
                            Created = DateTime.Now,
                            Updated = DateTime.Now
                        }))
                        {
                            book.AddCategory(category);
                        }
                    }
                }
                catch (Exception exception)
                {
                    errorlogRepository.AddLog(exception);
                }


                if (book != null)
                {
                    bookRepo.AddOrUpdate(book);
                }
            }
        }