@@ -0,0 +1,1308 @@
+import requests%0Aimport xmltodict%0A%0Afrom secret import GOODREADS_API_KEY%0A%0Agoodreads_api_key = GOODREADS_API_KEY%0A%0A%0Adef get_first_search_result(search_term):%0A    api_url = %22https://www.goodreads.com/search/index.xml?key=%7B0%7D&q=%7B1%7D&search%5Bfield%5D=title%22%0A    r = requests.get(api_url.format(search_term, goodreads_api_key))%0A    try:%0A        book_data = xmltodict.parse(r.content)%5B'GoodreadsResponse'%5D%5B'search'%5D%0A        book_id = book_data%5B'results'%5D%5B'work'%5D%5B0%5D%5B'best_book'%5D%5B'id'%5D%5B'#text'%5D%0A        return book_id%0A    except (TypeError, KeyError):%0A        return False%0A%0A%0Adef get_book_details_by_id(goodreads_id):%0A    api_url = 'http://goodreads.com/book/show/%7B0%7D?format=xml&key=%7B1%7D'%0A    r = requests.get(api_url.format(goodreads_id, goodreads_api_key))%0A    try:%0A        book_data = xmltodict.parse(r.content)%5B'GoodreadsResponse'%5D%5B'book'%5D%0A    except (TypeError, KeyError):%0A        return False%0A    keys = %5B'title', 'average_rating', 'ratings_count', 'description', 'url'%5D%0A    book = %7B%7D%0A    for k in keys:%0A        book%5Bk%5D = book_data%5Bk%5D%0A    if type(book_data%5B'authors'%5D%5B'author'%5D) == list:%0A        authors = %5Bauthor%5B'name'%5D for author in book_data%5B'authors'%5D%5B'author'%5D%5D%0A        authors = ', '.join(authors)%0A    else:%0A        authors = book_data%5B'authors'%5D%5B'author'%5D%5B'name'%5D%0A    book%5B'authors'%5D = authors%0A    return book%0A

Add Goodreads API