Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds Author advanced_search (w/ facets on birth + death date) #92

Closed
wants to merge 1 commit into from

Conversation

mekarpeles
Copy link
Member

This PR closes #91

Description:

Even though an Author search method exists...

i.e. ol = OpenLibrary();ol.Author.search(...)

It uses the autocomplete API which doesn't appear to support query by birth + death date. For now, to unblock @salman-bhai, this PR adds an Author.advanced_search which directly hits the Author json API endpoint and supports these additional facets.

params = {'name': name, 'birth_date': birth_date, 'death_date': death_date}
paramstr = '+'.join(['%s:%s' % (k, v) for (k, v) in params.items() if v])
url = cls.OL.base_url + '/search/authors.json?limit=%s&q=%s' % (limit, paramstr)
r = requests.get(url)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the params arg with requests.get:

requests.get(cls.OL.base_url + '/search/authors.json', params={'limit': limit, ...})

This will also encode the url parameters.

[u'OL26320A]'

Returns:
List of Author olid from API if results, else None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: this doesn't return None

e.g. "\"January 5\""
You can also do something like "*1982*"
(without quotes) to partial match on, e.g. year
date_date (str) - The death date of the Author; same conditions
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: date_date

r = requests.get(url)
try:
authors = r.json().get('docs', [])
except requests.JSONDecodeError as e:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a case when this could be thrown?

birth_date (str) - The birth date of the Author exactly as it
appears in OL (e.g. "January 5, 2013").
Must be in quotes if the date is mult-terms,
e.g. "\"January 5\""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use single quote here so you don't have to escape the double quotes.

List of Author olid from API if results, else None
"""
params = {'name': name, 'birth_date': birth_date, 'death_date': death_date}
paramstr = '+'.join(['%s:%s' % (k, v) for (k, v) in params.items() if v])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

query might be a better name for this; it'll get run pretty much directly on solr.

@@ -582,6 +582,41 @@ def get(cls, olid):
bio=OpenLibrary.get_text_value(data.pop('bio', None)),
**data)

@classmethod
def advanced_search(cls, name=None, birth_date=None, death_date=None, limit=5):
"""Searches for an Open Library authors using author API endpoint
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: grammar

@mekarpeles mekarpeles closed this Sep 17, 2018
@sbshah97
Copy link
Contributor

Can this be worked on to correct it based on @cdrini 's review?

@cclauss cclauss deleted the feature/add-author-search branch May 31, 2023 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update Author Search to use official Author endpoint?
3 participants