Basics of connecting python to the web and validating user input

142 views Asked by At

I'm relatively new, and I'm just at a loss as to where to start. I don't expect detailed step-by-step responses (though, of course, those are more than welcome), but any nudges in the right direction would be greatly appreciated.

I want to use the Gutenberg python library to select a text based on a user's input.

Right now I have the code:

from gutenberg.acquire import load_etext
from gutenberg.cleanup import strip_headers

text = strip_headers(load_etext(11)).strip()

where the number represents the text (in this case 11 = Alice in Wonderland).

Then I have a bunch of code about what to do with the text, but I don't think that's relevant here. (If it is let me know and I can add it).

Basically, instead of just selecting a text, I want to let the user do that. I want to ask the user for their choice of author, and if Project Gutenberg (PG) has pieces by that author, have them then select from the list of book titles (if PG doesn't have anything by that author, return some response along the lines of "sorry, don't have anything by $author_name, pick someone else." And then once the user has decided on a book, have the number corresponding to that book be entered into the code.

I just have no idea where to start in this process. I know how to handle user input, but I don't know how to take that input and search for something online using it.

Ideally, I'd be able to handle things like spelling mistakes too, but that may be down the line.

I really appreciate any help anyone has the time to give. Thanks!

1

There are 1 answers

5
larsks On BEST ANSWER

The gutenberg module includes facilities for searching for a text by metadata, such as author. The example from the docs is:

from gutenberg.query import get_etexts
from gutenberg.query import get_metadata

print(get_metadata('title', 2701))  # prints frozenset([u'Moby Dick; Or, The Whale'])
print(get_metadata('author', 2701)) # prints frozenset([u'Melville, Hermann'])

print(get_etexts('title', 'Moby Dick; Or, The Whale'))  # prints frozenset([2701, ...])
print(get_etexts('author', 'Melville, Hermann'))        # prints frozenset([2701, ...])

It sounds as if you already know how to read a value from the user into a variable, and replacing the literal author in the above would be as simple as doing something like:

author_name = my_get_input_from_user_function()
texts = get_etexts('author', author_name)

Note the following note from the same section:

Before you use one of the gutenberg.query functions you must populate the local metadata cache. This one-off process will take quite a while to complete (18 hours on my machine) but once it is done, any subsequent calls to get_etexts or get_metadata will be very fast. If you fail to populate the cache, the calls will raise an exception.

With that in mind, I haven't tried the code I've presented in this answer because I'm still waiting for my local cache to populate.