Created
February 11, 2021 06:52
-
-
Save mentix02/7853bece66f549d1c8e72afdd98e08e1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Dict, List, Union | |
from django.conf import settings | |
from requests import get | |
from urllib.parse import urlencode | |
BASE_URL = 'https://www.googleapis.com/books/v1/volumes' | |
DEFAULT_THUMBNAIL = {'thumbnail': 'https://bit.ly/2NG0A8J'} | |
def build_url(q: str) -> str: | |
GET_params = urlencode({ | |
"q": f'intitle:{q}', | |
"key": settings.BOOKS_API_SECRET_KEY, | |
}) | |
return f'{BASE_URL}?{GET_params}' | |
def search(q: str) -> List[Dict[str, Union[str, List[str]]]]: | |
volumes = [] | |
items = get(build_url(q)).json()['items'] | |
for item in items: | |
info = item['volumeInfo'] | |
volumes.append({ | |
'title': info['title'], | |
'authors': info.get('authors', []), | |
'image': info.get('imageLinks', DEFAULT_THUMBNAIL)['thumbnail'], | |
}) | |
return volumes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment