Ejemplo n.º 1
0
        public String GetJSONData(String Intent, List<Entity> Entities)
        {
            String Results = null;
            try
            {
                // To determine what kind of search we need to do, look at the Intent
                String ISBNUriStr = string.Empty;
                TextInfo Info = new CultureInfo("en-US", false).TextInfo;
                if (Intent == LUISConstants.SearchByAuthorForBooks)
                {
                    foreach (var Item in Entities)
                    {
                        if (Item.type == LUISConstants.Author)
                        {
                            ISBNUriStr = String.Format("https://api.isbndb.com/author/{0}", Info.ToTitleCase(Item.entity));
                            break;
                        }
                    }
                }
                else if (Intent == LUISConstants.SearchByBookForAuthor || Intent == LUISConstants.SearchByBookForSynopsis)
                {
                    foreach (var Item in Entities)
                    {
                        if (Item.type == LUISConstants.Book)
                        {
                            ISBNUriStr = String.Format("https://api.isbndb.com/books/{0}", Info.ToTitleCase(Item.entity));
                            break;
                        }
                    }
                }

                // Format the webrequest for the ISBN database.
                Uri Uri = new Uri(ISBNUriStr);

                WebRequest Http = WebRequest.Create(Uri);
                Http.Method = "GET";
                Http.ContentType = "application/json";
                Http.Headers["X-API-KEY"] = Keys.ISBNdBAccessKey;
                WebResponse Response = Http.GetResponse();
                Stream Stream = Response.GetResponseStream();

                StreamReader Reader = new StreamReader(Stream);
                Results = Reader.ReadToEnd();

                // For some unknown reason, when searching for books written by an author, some of the books will have synopsis or overview
                // details available, but when a book is searched these details are always absent.  So if the intent is SearchByBookForSynopsis
                // there must be another database search.
                // This seems to be an issue with the database itself, and the way it chooses to return data.
                if (Intent == LUISConstants.SearchByBookForSynopsis)
                {
                    // Grab the author of the book.
                    DataParser Data = new DataParser(Results);
                    Data.DBParse(Intent);

                    // Have to do this because the author data can be incomplete on some books.
                    foreach (var Book in Data.BooksReturned.books)
                    {
                        if (Book.authors.Count != 0)
                        {
                            ISBNUriStr = String.Format("https://api.isbndb.com/author/{0}", Book.authors[0]);
                            break;
                        }
                    }

                    // Now do the search for the author.
                    Uri = new Uri(ISBNUriStr);

                    Http = WebRequest.Create(Uri);
                    Http.Method = "GET";
                    Http.ContentType = "application/json";
                    Http.Headers["X-API-KEY"] = Keys.ISBNdBAccessKey;
                    Response = Http.GetResponse();
                    Stream = Response.GetResponseStream();

                    Reader = new StreamReader(Stream);
                    Results = Reader.ReadToEnd();
                }
                return Results;
            }
            catch (Exception e) { string exception = e.Message; return exception; }
        }
Ejemplo n.º 2
0
        public async Task PostBookCard(IDialogContext Context, List <Entity> Entities, DataParser ISBNData, TextInfo Info)
        {
            // First get the book name.
            string BookName = GrabEntity(Entities, LUISConstants.Book, Info);

            // Now look through the returned books for the searched book.
            if (ISBNData.BooksReturned != null)
            {
                Book FoundBook = new Book();
                foreach (var Book in ISBNData.BooksReturned.books)
                {
                    // Must make the cases identical to ensure accurate comparison
                    if (Info.ToTitleCase(Book.title_long).Contains(BookName))
                    {
                        // Here we found the book, so lets grab the necessary info.
                        FoundBook = Book;
                        break;
                    }
                }

                // Make sure we found the book. Don't have to clean the title here.
                if (FoundBook.title_long != null)
                {
                    // Now that we have the info, lets make a card to post.
                    var Hero = new HeroCard
                    {
                        Title    = FoundBook.title_long,
                        Subtitle = String.Format("Written by {0}", FoundBook.authors[0]),
                        Images   = new List <CardImage> {
                            new CardImage(FoundBook.image)
                        },
                        Buttons = new List <CardAction> {
                            new CardAction(ActionTypes.ImBack, "Book details.", null, String.Format("Details of {0}", FoundBook.title_long), null, null),
                            new CardAction(ActionTypes.ImBack, "More books by this author.", null, String.Format("Books by {0}", FoundBook.authors[0]), null, null)
                        }
                    };
                    // Now we post the card.
                    await PostCard(Context, Hero);
                }
                else
                {
                    await Context.PostAsync("Sorry I could not find that book in my database.");
                }
            }
            else
            {
                await Context.PostAsync("Sorry that book does not exist in my database.");
            }
        }
Ejemplo n.º 3
0
        public async Task PostSynopsisCard(IDialogContext Context, List <Entity> Entities, DataParser ISBNData, TextInfo Info)
        {
            // First get the book name.
            string BookSearch = GrabEntity(Entities, LUISConstants.Book, Info);

            // Search for the book in the Author data.
            string BookName = string.Empty;

            if (ISBNData.BooksReturned != null)
            {
                foreach (var Book in ISBNData.BooksReturned.books)
                {
                    // Clean up the title first since they always have '-' and '_' in them.
                    BookName = Book.CleanTitle(Info);

                    // Try to get the synopsis, but it isn't always available.
                    // Otherwise grab the overview.
                    if (BookName.Contains(BookSearch))
                    {
                        string PlotText = string.Empty;
                        if (Book.synopsys != null)
                        {
                            PlotText = ISBNData.StripHTML(Book.synopsys);
                        }
                        else if (Book.overview != null)
                        {
                            PlotText = ISBNData.StripHTML(Book.overview);
                        }
                        else
                        {
                            await Context.PostAsync("Sorry the database does not have info for that book.");

                            break;
                        }

                        // Now lets make a card to return the info.
                        var Hero = new HeroCard
                        {
                            Title  = BookName,
                            Images = new List <CardImage> {
                                new CardImage(Book.image)
                            },
                            Text = PlotText
                        };

                        // Post the card.
                        await PostCard(Context, Hero);

                        break;
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public async Task PostAuthorCard(IDialogContext Context, List <Entity> Entities, DataParser ISBNData, TextInfo Info)
        {
            // Grab the Author name.
            string Author    = GrabEntity(Entities, LUISConstants.Author, Info);
            string CardTitle = String.Format("Books by {0}", Author);

            // Grab all the titles the author has written
            List <CardAction> CardActions = new List <CardAction>();
            List <string>     BookCheck   = new List <string>();

            if (ISBNData.AuthorsReturned[0].books.Count > 0)
            {
                foreach (var Book in ISBNData.AuthorsReturned[0].books)
                {
                    // Have to clean up the title since all books in the database have '-' and '_' in them.
                    string Title = Book.CleanTitle(Info);

                    // Check to make sure its not a repeat.
                    if (!BookCheck.Contains(Title))
                    {
                        // Add the book title to a list.
                        BookCheck.Add(Title);

                        // Now lets make a card action.
                        if (Book.image != null)
                        {
                            var Action = new CardAction(ActionTypes.ImBack, Title, Book.image, "details of " + Title, null, null);
                            CardActions.Add(Action);
                        }
                    }
                }

                // Now that we have the list of books by the author, lets make the response card.
                var Hero = new HeroCard
                {
                    Title   = CardTitle,
                    Text    = "Click on a button for more information.",
                    Buttons = CardActions
                };

                // Now we post the card.
                await PostCard(Context, Hero);
            }
            else
            {
                await Context.PostAsync("Sorry that author does not exist in my database.");
            }
        }