#!/usr/bin/env python

import re
from xml.parsers.expat import ExpatError

import requests
import xmltodict

from settings import goodreads_api_key


def get_goodreads_ids(comment_msg):
    # receives goodreads url
    # returns the id using regex
    regex = r'goodreads.com/book/show/(\d+)'
    return set(re.findall(regex, comment_msg))


def get_book_details_by_id(goodreads_id):
    api_url = 'http://goodreads.com/book/show/{0}?format=xml&key={1}'
    r = requests.get(api_url.format(goodreads_id, goodreads_api_key))
    try:
        book_data = xmltodict.parse(r.content)['GoodreadsResponse']['book']
    except (TypeError, KeyError, ExpatError):
        return False
    keys = ['title', 'average_rating', 'ratings_count', 'description',
            'num_pages']
    book = {}
    for k in keys:
        book[k] = book_data.get(k)
    try:
        work = book_data['work']
        book['publication_year'] = work['original_publication_year']['#text']
    except KeyError:
        book['publication_year'] = book_data.get('publication_year')

    if type(book_data['authors']['author']) == list:
        authors = [author['name'] for author in book_data['authors']['author']]
        authors = ', '.join(authors)
    else:
        authors = book_data['authors']['author']['name']
    book['authors'] = authors
    return book

#!/usr/bin/env python

import re
from xml.parsers.expat import ExpatError

import requests
import xmltodict

from settings import goodreads_api_key


def get_goodreads_ids(comment_msg):
    # receives goodreads url
    # returns the id using regex
    regex = r'goodreads.com/book/show/(\d+)'
    return set(re.findall(regex, comment_msg))


def get_book_details_by_id(goodreads_id):
    api_url = 'http://goodreads.com/book/show/{0}?format=xml&key={1}'
    r = requests.get(api_url.format(goodreads_id, goodreads_api_key))
    try:
        book_data = xmltodict.parse(r.content)['GoodreadsResponse']['book']
    except (TypeError, KeyError, ExpatError):
        return False
    keys = ['title', 'average_rating', 'ratings_count', 'description',
            'num_pages', 'publication_year']
    book = {}
    for k in keys:
        book[k] = book_data.get(k)
    if type(book_data['authors']['author']) == list:
        authors = [author['name'] for author in book_data['authors']['author']]
        authors = ', '.join(authors)
    else:
        authors = book_data['authors']['author']['name']
    book['authors'] = authors
    return book

--- 
+++ 
@@ -24,10 +24,16 @@
     except (TypeError, KeyError, ExpatError):
         return False
     keys = ['title', 'average_rating', 'ratings_count', 'description',
-            'num_pages', 'publication_year']
+            'num_pages']
     book = {}
     for k in keys:
         book[k] = book_data.get(k)
+    try:
+        work = book_data['work']
+        book['publication_year'] = work['original_publication_year']['#text']
+    except KeyError:
+        book['publication_year'] = book_data.get('publication_year')
+
     if type(book_data['authors']['author']) == list:
         authors = [author['name'] for author in book_data['authors']['author']]
         authors = ', '.join(authors)
Update goodreads API to `show original_publication_year`
