Skip to content

Commit

Permalink
Add book module (#11)
Browse files Browse the repository at this point in the history
* Add book module

* Fix typo, remove unused import

* Add test for book

* Add attribution to Goodreads
  • Loading branch information
havanagrawal authored and swapagarwal committed Apr 23, 2016
1 parent 656727f commit 91d7039
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ACCESS_TOKEN = '<ACCESS_TOKEN>'
VERIFY_TOKEN = '<VERIFY_TOKEN>'
WIT_AI_ACCESS_TOKEN = 'IKJJJYYVR3X672DHFVS7U7C4L2MQSS2P'
GOODREADS_ACCESS_TOKEN = '<GOODREADS_ACCESS_TOKEN>'
1 change: 1 addition & 0 deletions modules/src/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
__all__ = [
'book',
'dictionary',
'hello',
'help',
Expand Down
29 changes: 29 additions & 0 deletions modules/src/book.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import requests
import config
import os
from xml.etree import ElementTree

GOODREADS_ACCESS_TOKEN = os.environ.get('GOODREADS_ACCESS_TOKEN', config.GOODREADS_ACCESS_TOKEN)

def process(input, entities=None):
output = {}
try:
book_title = entities['book'][0]['value']
response = requests.get('https://www.goodreads.com/book/title.xml?key='+GOODREADS_ACCESS_TOKEN+'&title=' + book_title)
data = ElementTree.fromstring(response.content)

book_node = data.find('book')
title = book_node.find('title').text
description = book_node.find('description').text
link = book_node.find('link').text
average_rating = book_node.find('average_rating').text
goodreads_attribute = "This data is powered by Goodreads"

output_data = (title, description, average_rating, link, goodreads_attribute)

output['input'] = input
output['output'] = "Title: %s<br/>Description: %s<br/>Average Rating: %s<br/> Book link: %s<br/><br/>%s" % output_data
output['success'] = True
except:
output['success'] = False
return output
7 changes: 7 additions & 0 deletions modules/tests/test_book.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import modules

def test_book():
assert('book' == modules.process_query('book timeline')[0])
assert('book' == modules.process_query('have you read harry potter?')[0])
assert('book' == modules.process_query('little women book rating')[0])
assert('book' != modules.process_query('harry potter movie')[0])

0 comments on commit 91d7039

Please sign in to comment.