3

Basicly, I've done with Python-requests and Django search feature through Google Books API with single q parameter (as shown in link below)

https://developers.google.com/books/docs/v1/using#WorkingVolumes

and after submiting form I'm getting list of dicts in json as I want with this single parameter, and I'm getting in json data where appers keyword "Hobbit" and URL looks like this

http://127.0.0.1:8000/api?books=hobbit

but when I'm trying to add special keywords provided by Google Books API like, intitle, inauthor, inpublisher, subject, etc.

and trying to search for it I'm getting URL

http://127.0.0.1:8000/api?books=hobbit&intitle=&inauthor=&inpublisher=&isbn=&lccn=&oclc=

which only returns the data of single q parameter, because the correct URL for special keywords in Google Books API looks like this

https://www.googleapis.com/books/v1/volumes?q=flowers+inauthor:keyes+subject:somesubject

So as you see then correct URL got signs

+ against & and : against =

so the Correct URL that I want to get would look like this

http://127.0.0.1:8000/api?books=hobbit+intitle:something+inauthor:something+inpublisher:something+isbn:something+lccn:something+oclc:something

My question is how to change this structure to correct as Google books API require?

Tried to find this in python-requests docs but there are nothing about this

views.py

def api(request):
    books = {
        'intitle': 'intitle',
        'inauthor': 'inauthor',
        'inpublisher': 'inpublisher',
        'subject': 'subject',
        'isbn': 'isbn',
        'lccn': 'lccn',
        'oclc': 'oclc'
    }
    if 'books' in request.GET:
        books = request.GET['books']
        url = 'https://www.googleapis.com/books/v1/volumes?q=%s' % books
        response = requests.get(url)
        books = response.json()
        print(type(books))
        with open("data_file.json", "w") as write_file:
            json.dump(books, write_file)

    return render(request, 'books/api.html', {'books': books})

1 Answer 1

2

You will have to construct the query string manually. Assuming that your request will look like http://127.0.0.1:8000/api?books=hobbit&intitle=a&inauthor=b&inpublisher=c, you can construct the query string like this:

def api(request):
    # ...
    if 'books' in request.GET:
        books = request.GET['books']
        query_dict = request.GET.copy()
        del query_dict['books']
        query = '+'.join([books, *['{}:{}'.format(k, v) for k, v in query_dict.items()]])
        url = 'https://www.googleapis.com/books/v1/volumes?q=' + query
        # ...

The final google query requires books as the first parameter. So, we need to extract the books value from request.GET. Now, to get all other values, we need to delete the books key. But, request.GET is a QueryDict object, which is immutable. To convert it into a mutable object, request.GET.copy() can be used (which creates a mutable copy).

Sign up to request clarification or add additional context in comments.

6 Comments

Hey @Keyur, my request actually right now looks like this what but what I want is to change every sign & to + and every = to :
When you submit a form, you will get url like I mentioned in my answer. Rather than changing the url itself, it is easy to construct to required string using the url.
Ok but can you explain usage of copy? I don't understand it
Added explanation in the answer.
The rest of the code will remain the same. What you've done should work.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.