Skip to content

Commit

Permalink
feat: Add endpoints for BB entities
Browse files Browse the repository at this point in the history
  • Loading branch information
anshg1214 committed Sep 2, 2022
1 parent f7f436b commit 236a862
Show file tree
Hide file tree
Showing 9 changed files with 806 additions and 0 deletions.
8 changes: 8 additions & 0 deletions critiquebrainz/ws/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ def _register_blueprints(app):
from critiquebrainz.ws.recording.views import recording_bp
from critiquebrainz.ws.release_group.views import release_group_bp
from critiquebrainz.ws.work.views import work_bp
from critiquebrainz.ws.bb_author.views import author_bp
from critiquebrainz.ws.bb_edition_group.views import edition_group_bp
from critiquebrainz.ws.bb_literary_work.views import literary_work_bp
from critiquebrainz.ws.bb_series.views import series_bp
app.register_blueprint(oauth_bp, url_prefix="/oauth")
app.register_blueprint(review_bp, url_prefix="/review")
app.register_blueprint(user_bp, url_prefix="/user")
Expand All @@ -127,4 +131,8 @@ def _register_blueprints(app):
app.register_blueprint(recording_bp, url_prefix="/recording")
app.register_blueprint(release_group_bp, url_prefix="/release-group")
app.register_blueprint(work_bp, url_prefix="/work")
app.register_blueprint(author_bp, url_prefix="/author")
app.register_blueprint(edition_group_bp, url_prefix="/edition-group")
app.register_blueprint(literary_work_bp, url_prefix="/literary-work")
app.register_blueprint(series_bp, url_prefix="/series")

Empty file.
213 changes: 213 additions & 0 deletions critiquebrainz/ws/bb_author/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
from flask import Blueprint, jsonify
import critiquebrainz.db.review as db_review
import critiquebrainz.db.rating_stats as db_rating_stats
from critiquebrainz.frontend.external.bookbrainz_db import author as db_author
from critiquebrainz.decorators import crossdomain
from critiquebrainz.ws.exceptions import NotFound
from critiquebrainz.ws.parser import Parser
from critiquebrainz.ws import REVIEWS_LIMIT

author_bp = Blueprint('ws_author', __name__)


@author_bp.route('/<uuid:author_bbid>', methods=['GET', 'OPTIONS'])
@crossdomain(headers="Authorization, Content-Type")
def author_entity_handler(author_bbid):
"""Get list of reviews.
**Request Example:**
.. code-block:: bash
$ curl "https://critiquebrainz.org/ws/1/author/e5c4e68b-bfce-4c77-9ca2-0f0a2d4d09f0" \\
-X GET
**Response Example:**
.. code-block:: json
{
"author": {
"area_id": null,
"area_info": [],
"author_type": "Person",
"bbid": "e5c4e68b-bfce-4c77-9ca2-0f0a2d4d09f0",
"begin_area_id": 221,
"begin_day": 31,
"begin_month": 7,
"begin_year": 1965,
"disambiguation": null,
"end_area_id": null,
"end_day": null,
"end_month": null,
"end_year": null,
"ended": false,
"gender": "Female",
"identifier_set_id": 7401,
"identifiers": [
{
"icon": "wikidata-16.png",
"name": "Wikidata ID",
"url": "https://www.wikidata.org/wiki/Q34660",
"value": "Q34660"
},
{
"icon": "viaf-16.png",
"name": "VIAF",
"url": "https://viaf.org/viaf/116796842",
"value": "116796842"
},
{
"icon": "wikidata-16.png",
"name": "Wikidata ID",
"url": "https://www.wikidata.org/wiki/Q1190608",
"value": "Q1190608"
}
],
"name": "J. K. Rowling",
"relationship_set_id": 151715,
"sort_name": "Rowling, J. K."
},
"average_rating": 5.0,
"latest_reviews": [
{
"created": "Tue, 16 Aug 2022 11:25:44 GMT",
"edits": 0,
"entity_id": "e5c4e68b-bfce-4c77-9ca2-0f0a2d4d09f0",
"entity_type": "bb_author",
"full_name": "Creative Commons Attribution-ShareAlike 3.0 Unported",
"id": "326e702a-020f-40e7-b369-9142a7af4315",
"info_url": "https://creativecommons.org/licenses/by-sa/3.0/",
"is_draft": false,
"is_hidden": false,
"language": "en",
"last_revision": {
"id": 11785,
"rating": 5,
"review_id": "326e702a-020f-40e7-b369-9142a7af4315",
"text": null,
"timestamp": "Fri, 02 Sep 2022 09:54:13 GMT"
},
"last_updated": "Fri, 02 Sep 2022 09:54:13 GMT",
"license_id": "CC BY-SA 3.0",
"popularity": 0,
"published_on": "Tue, 16 Aug 2022 11:25:44 GMT",
"rating": 5,
"source": null,
"source_url": null,
"text": null,
"user": {
"created": "Tue, 18 Jan 2022 09:53:49 GMT",
"display_name": "Ansh Goyal",
"id": "11a1160e-d607-4882-8a82-e2e800f664fe",
"karma": 0,
"user_type": "Noob"
},
"votes_negative_count": 0,
"votes_positive_count": 0
}
],
"ratings_stats": {
"1": 0,
"2": 0,
"3": 0,
"4": 0,
"5": 1
},
"reviews_count": 1,
"top_reviews": [
{
"created": "Tue, 16 Aug 2022 11:25:44 GMT",
"edits": 0,
"entity_id": "e5c4e68b-bfce-4c77-9ca2-0f0a2d4d09f0",
"entity_type": "bb_author",
"full_name": "Creative Commons Attribution-ShareAlike 3.0 Unported",
"id": "326e702a-020f-40e7-b369-9142a7af4315",
"info_url": "https://creativecommons.org/licenses/by-sa/3.0/",
"is_draft": false,
"is_hidden": false,
"language": "en",
"last_revision": {
"id": 11785,
"rating": 5,
"review_id": "326e702a-020f-40e7-b369-9142a7af4315",
"text": null,
"timestamp": "Fri, 02 Sep 2022 09:54:13 GMT"
},
"last_updated": "Fri, 02 Sep 2022 09:54:13 GMT",
"license_id": "CC BY-SA 3.0",
"popularity": 0,
"published_on": "Tue, 16 Aug 2022 11:25:44 GMT",
"rating": 5,
"source": null,
"source_url": null,
"text": null,
"user": {
"created": "Tue, 18 Jan 2022 09:53:49 GMT",
"display_name": "Ansh Goyal",
"id": "11a1160e-d607-4882-8a82-e2e800f664fe",
"karma": 0,
"user_type": "Noob"
},
"votes_negative_count": 0,
"votes_positive_count": 0
}
]
}
:statuscode 200: no error
:statuscode 404: author not found
:resheader Content-Type: *application/json*
"""

author = db_author.get_author_by_bbid(str(author_bbid))

if not author:
raise NotFound("Can't find an author with ID: {author_bbid}".format(author_bbid=author_bbid))

user_id = Parser.uuid('uri', 'user_id', optional=True)
if user_id:
user_review, _ = db_review.list_reviews(
entity_id=author['bbid'],
entity_type='bb_author',
user_id=user_id
)
if user_review:
user_review = db_review.to_dict(user_review[0])
else:
user_review = None

ratings_stats, average_rating = db_rating_stats.get_stats(author_bbid, "bb_author")

top_reviews, reviews_count = db_review.list_reviews(
entity_id=author['bbid'],
entity_type='bb_author',
sort='popularity',
limit=REVIEWS_LIMIT,
offset=0,
)

latest_reviews, reviews_count = db_review.list_reviews(
entity_id=author['bbid'],
entity_type='bb_author',
sort='published_on',
limit=REVIEWS_LIMIT,
offset=0,
)

top_reviews = [db_review.to_dict(review) for review in top_reviews]
latest_reviews = [db_review.to_dict(review) for review in latest_reviews]

result = {
"author": author,
"average_rating": average_rating,
"ratings_stats": ratings_stats,
"reviews_count": reviews_count,
"top_reviews": top_reviews,
"latest_reviews": latest_reviews
}
if user_id:
result['user_review'] = user_review

return jsonify(**result)
Empty file.
Loading

0 comments on commit 236a862

Please sign in to comment.